-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic_tac_toe.py
313 lines (243 loc) · 9.53 KB
/
tic_tac_toe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
"""Tic Tac Toe in Python."""
def player_marker(player1, player2):
"""Take both players input for the markers."""
# check if player1 is 'X' or 'O'
while player1 != "X" and player1 != "O":
try:
player1 = input("Please pick a marker ('X' or 'O'): ").upper()
except ValueError:
print("This is not a valid marker, please try again!")
print()
print()
if player1 == "X":
player2 = "O"
print(
f"Player 1 is '{player1}'."
f"\nPlayer 2 is '{player2}'."
"\n\nNow you will have to choose from 1 - 9 where '1' is the "
"left lower corner and '9' is the right upper corner."
)
input("Press Enter to continue. ")
elif player1 == "O":
player2 = "X"
print(
f"Player 1 is '{player1}'."
f"\nPlayer 2 is '{player2}'."
"\n\nNow you will have to choose from 1 - 9 where '1' is the "
"left lower corner and '9' is the right upper corner."
)
input("Press Enter to continue. ")
# return both variables to reassign then locally on the game_logic() function
return (player1, player2)
def p1_marker_loc(p1_input, board_list, player1):
"""Take the location of the marker for Player 1."""
# verify if the input is not in range or in range but in a already taken spot
while p1_input not in range(1, 10) or (
p1_input in range(1, 10) and board_list[p1_input] != " "
):
try:
p1_input = int(
input("Player 1: Where would you like to place the marker (1 - 9)? ")
)
# if a marker is already placed on that board location, display a message
# warning player 1 and ask for their input again
if board_list[p1_input] != " ":
print(
"There is already a marker there, please choose another location."
)
input("Press Enter to continue. ")
print()
# input the player for another location for the marker
continue
except ValueError:
print("This is not a number, please try again!")
print()
print(f"Player 1 is placing {player1} in position {p1_input}.")
# return the variable to reassign it locally on the game_logic() function
return p1_input
def p2_marker_loc(p2_input, board_list, player2):
"""Take the location of the marker for Player 2."""
# verify if the input is not in range or in range but in a already taken spot
while p2_input not in range(1, 10) or (
p2_input in range(1, 10) and board_list[p2_input] != " "
):
try:
p2_input = int(
input("Player 2: Where would you like to place the marker (1 - 9)? ")
)
# if a marker is already placed on that board location, display a message
# warning player 2 and ask for their input again
if board_list[p2_input] != " ":
print(
"There is already a marker there, please choose another location."
)
input("Press Enter to continue. ")
print()
# input the player for another location for the marker
continue
except ValueError:
print("This is not a number, please try again!")
print()
print(f"Player 2 is placing {player2} in position {p2_input}.")
# return the variable to reassign it locally on the game_logic() function
return p2_input
def board(board_list):
"""Draw the board on the screen with the welcoming message."""
print(
f" {board_list[7]} | {board_list[8]} | {board_list[9]} "
f"\n {'-' * 17} \n"
f" {board_list[4]} | {board_list[5]} | {board_list[6]} "
f"\n {'-' * 17} \n"
f" {board_list[1]} | {board_list[2]} | {board_list[3]} "
)
def x_win_condition(board_list):
"""Condition for 'X' to win."""
return (
(board_list[1] == "X" and board_list[2] == "X" and board_list[3] == "X")
or (board_list[1] == "X" and board_list[4] == "X" and board_list[7] == "X")
or (board_list[1] == "X" and board_list[5] == "X" and board_list[9] == "X")
or (board_list[2] == "X" and board_list[5] == "X" and board_list[8] == "X")
or (board_list[3] == "X" and board_list[6] == "X" and board_list[9] == "X")
or (board_list[3] == "X" and board_list[5] == "X" and board_list[7] == "X")
or (board_list[4] == "X" and board_list[5] == "X" and board_list[6] == "X")
or (board_list[7] == "X" and board_list[8] == "X" and board_list[9] == "X")
)
def o_win_condition(board_list):
"""Condition for 'O' to win."""
return (
(board_list[1] == "O" and board_list[2] == "O" and board_list[3] == "O")
or (board_list[1] == "O" and board_list[4] == "O" and board_list[7] == "O")
or (board_list[1] == "O" and board_list[5] == "O" and board_list[9] == "O")
or (board_list[2] == "O" and board_list[5] == "O" and board_list[8] == "O")
or (board_list[3] == "O" and board_list[6] == "O" and board_list[9] == "O")
or (board_list[3] == "O" and board_list[5] == "O" and board_list[7] == "O")
or (board_list[4] == "O" and board_list[5] == "O" and board_list[6] == "O")
or (board_list[7] == "O" and board_list[8] == "O" and board_list[9] == "O")
)
def tie_condition(board_list):
"""Condition for a tie to happen."""
return " " not in board_list[1:10]
def p1_won():
"""Declare Player 1 as the winner."""
print("PLAYER 1 WON!")
print("CONGRATULATIONS!")
def p2_won():
"""Declare Player 2 as the winner."""
print("PLAYER 2 WON!")
print("CONGRATULATIONS!")
def tie_message():
"""Declare the game as a tie."""
print("IT'S A TIE!")
def game_logic():
"""Dictates how the game works."""
# variables for the player_marker() function
player1 = None
player2 = None
# variables for the p1_marker_loc() and p2_marker_loc() functions
p1_input = None
p2_input = None
# variable for the board() function
board_list = [" "] * 10
# cleaning the terminal and displaying the welcoming message
print("\n" * 100)
print("WELCOME TO TIC TAC TOE IN PYTHON")
print()
board(board_list)
print()
# get the marker for each player
player1, player2 = player_marker(player1, player2)
print()
# keep looping the core gameplay until either the win or tie conditions are met
while True:
# check if someone won
if x_win_condition(board_list):
# declare who is the winner and break from the loop
if player1 == "X":
p1_won()
print()
break
elif player2 == "X":
p2_won()
print()
break
# check if someone won
elif o_win_condition(board_list):
# declare who is the winner and break from the loop
if player1 == "O":
p1_won()
print()
break
elif player2 == "O":
p2_won()
print()
break
# check if it is a tie
elif tie_condition(board_list):
tie_message()
print()
break
else:
# get the location of the marker for player 1
p1_input = p1_marker_loc(p1_input, board_list, player1)
# assign player1 to specific indexes at the board list
board_list[p1_input] = player1
print()
# display the marker on the board
print("\n" * 100)
print("TIC TAC TOE IN PYTHON")
print()
board(board_list)
print()
# check if someone won
if x_win_condition(board_list):
# declare who is the winner and break from the loop
if player1 == "X":
p1_won()
print()
break
elif player2 == "X":
p2_won()
print()
break
# check if someone won
elif o_win_condition(board_list):
# declare who is the winner and break from the loop
if player1 == "O":
p1_won()
print()
break
elif player2 == "O":
p2_won()
print()
break
# check if it is a tie
elif tie_condition(board_list):
tie_message()
print()
break
else:
# get the location of the marker for player 2
p2_input = p2_marker_loc(p2_input, board_list, player2)
# assign player2 to specific indexes at the board list
board_list[p2_input] = player2
print()
# display the marker on the board
print("\n" * 100)
print("TIC TAC TOE IN PYTHON")
print()
board(board_list)
print()
game_logic()
def game_restart():
"""Ask the players if they want to continue playing."""
# continue looping the question if the player's answer is not 'Y/YES' or 'N/NO'
while True:
reboot = input("Would you like to play again? (Y/N) ").upper()
print()
if reboot == "Y" or reboot == "YES":
game_logic()
elif reboot == "N" or reboot == "NO":
print("Thanks for playing!")
print()
break
game_restart()