Skip to content

Commit 590ffed

Browse files
author
codehouseindia
authored
Merge pull request codehouseindia#8 from KDChess-2813/patch-2
Amazing Tic Tac Toe Game!!
2 parents 123ad5a + a169a16 commit 590ffed

1 file changed

Lines changed: 188 additions & 0 deletions

File tree

Tic Tac Toe Game.py

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
#Code House Channel Subsrcibed
2+
# --------- Global Variables -----------
3+
4+
# Will hold our game board data
5+
board = ["-", "-", "-",
6+
"-", "-", "-",
7+
"-", "-", "-"]
8+
9+
# Lets us know if the game is over yet
10+
game_still_going = True
11+
12+
# Tells us who the winner is
13+
winner = None
14+
15+
# Tells us who the current player is (X goes first)
16+
current_player = "X"
17+
18+
19+
# ------------- Functions ---------------
20+
21+
# Play a game of tic tac toe
22+
def play_game():
23+
24+
# Show the initial game board
25+
display_board()
26+
27+
# Loop until the game stops (winner or tie)
28+
while game_still_going:
29+
30+
# Handle a turn
31+
handle_turn(current_player)
32+
33+
# Check if the game is over
34+
check_if_game_over()
35+
36+
# Flip to the other player
37+
flip_player()
38+
39+
# Since the game is over, print the winner or tie
40+
if winner == "X" or winner == "O":
41+
print(winner + " won.")
42+
elif winner == None:
43+
print("Tie.")
44+
45+
46+
# Display the game board to the screen
47+
def display_board():
48+
print("\n")
49+
print(board[0] + " | " + board[1] + " | " + board[2] + " 1 | 2 | 3")
50+
print(board[3] + " | " + board[4] + " | " + board[5] + " 4 | 5 | 6")
51+
print(board[6] + " | " + board[7] + " | " + board[8] + " 7 | 8 | 9")
52+
print("\n")
53+
54+
55+
# Handle a turn for an arbitrary player
56+
def handle_turn(player):
57+
58+
# Get position from player
59+
print(player + "'s turn.")
60+
position = input("Choose a position from 1-9: ")
61+
62+
# Whatever the user inputs, make sure it is a valid input, and the spot is open
63+
valid = False
64+
while not valid:
65+
66+
# Make sure the input is valid
67+
while position not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
68+
position = input("Choose a position from 1-9: ")
69+
70+
# Get correct index in our board list
71+
position = int(position) - 1
72+
73+
# Then also make sure the spot is available on the board
74+
if board[position] == "-":
75+
valid = True
76+
else:
77+
print("You can't go there. Go again.")
78+
79+
# Put the game piece on the board
80+
board[position] = player
81+
82+
# Show the game board
83+
display_board()
84+
85+
86+
# Check if the game is over
87+
def check_if_game_over():
88+
check_for_winner()
89+
check_for_tie()
90+
91+
92+
# Check to see if somebody has won
93+
def check_for_winner():
94+
# Set global variables
95+
global winner
96+
# Check if there was a winner anywhere
97+
row_winner = check_rows()
98+
column_winner = check_columns()
99+
diagonal_winner = check_diagonals()
100+
# Get the winner
101+
if row_winner:
102+
winner = row_winner
103+
elif column_winner:
104+
winner = column_winner
105+
elif diagonal_winner:
106+
winner = diagonal_winner
107+
else:
108+
winner = None
109+
110+
111+
# Check the rows for a win
112+
def check_rows():
113+
# Set global variables
114+
global game_still_going
115+
# Check if any of the rows have all the same value (and is not empty)
116+
row_1 = board[0] == board[1] == board[2] != "-"
117+
row_2 = board[3] == board[4] == board[5] != "-"
118+
row_3 = board[6] == board[7] == board[8] != "-"
119+
# If any row does have a match, flag that there is a win
120+
if row_1 or row_2 or row_3:
121+
game_still_going = False
122+
# Return the winner
123+
if row_1:
124+
return board[0]
125+
elif row_2:
126+
return board[3]
127+
elif row_3:
128+
return board[6]
129+
# Or return None if there was no winner
130+
else:
131+
return None
132+
133+
134+
# Check the columns for a win
135+
def check_columns():
136+
# Set global variables
137+
global game_still_going
138+
# Check if any of the columns have all the same value (and is not empty)
139+
column_1 = board[0] == board[3] == board[6] != "-"
140+
column_2 = board[1] == board[4] == board[7] != "-"
141+
column_3 = board[2] == board[5] == board[8] != "-"
142+
# If any row does have a match, flag that there is a win
143+
if column_1 or column_2 or column_3:
144+
game_still_going = False
145+
# Return the winner
146+
if column_1:
147+
return board[0]
148+
elif column_2:
149+
return board[1]
150+
elif column_3:
151+
return board[2]
152+
# Or return None if there was no winner
153+
else:
154+
return None
155+
156+
157+
# Check the diagonals for a win
158+
def check_diagonals():
159+
# Set global variables
160+
global game_still_going
161+
# Check if any of the columns have all the same value (and is not empty)
162+
diagonal_1 = board[0] == board[4] == board[8] != "-"
163+
diagonal_2 = board[2] == board[4] == board[6] != "-"
164+
# If any row does have a match, flag that there is a win
165+
if diagonal_1 or diagonal_2:
166+
game_still_going = False
167+
# Return the winner
168+
if diagonal_1:
169+
return board[0]
170+
elif diagonal_2:
171+
return board[2]
172+
# Or return None if there was no winner
173+
else:
174+
return None
175+
176+
177+
# Check if there is a tie
178+
def check_for_tie():
179+
# Set global variables
180+
global game_still_going
181+
# If board is full
182+
if "-" not in board:
183+
game_still_going = False
184+
return True
185+
# Else there is no tie
186+
else:
187+
return False
188+

0 commit comments

Comments
 (0)