forked from SalamanderXing/adrian_python_lessons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic_tac_toe_auto.py
343 lines (329 loc) · 12.1 KB
/
tic_tac_toe_auto.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# implements a game tic tac toe
# imports
import random
# creates a game class for tic tac toe.
class TicTacToe:
# initializes the game
def __init__(self):
# initialize the board
self.board = [" "] * 10
# initialize the player
self.player = "X"
# initialize the computer
self.computer = "O"
# initialize the game status
self.game_status = True
# initialize the winner
self.winner = None
# initialize the move counter
self.move_counter = 0
# initialize the tie
self.tie = False
# initialize the move
self.move = 0
# initialize the computer move
self.computer_move = 0
# prints the board
def print_board(self):
# print the board
print("\n")
# print the top row
print(" | | ")
# print the middle row
print(" " + self.board[7] + " | " + self.board[8] + " | " + self.board[9])
# print the bottom row
print(" | | ")
# print the separator
print("-----------")
#checks if the game is over
def is_game_over(self):
# check if the game is over
if self.winner is not None:
# game is over
self.game_status = False
elif self.move_counter == 9:
self.game_status = 'draw'
else:
self.game_status = True
# return the game status
return self.game_status
# checks if the player has won
def is_player_won(self):
# check if the player has won
if self.board[7] == self.player and self.board[8] == self.player and self.board[9] == self.player:
# player has won
self.winner = self.player
elif self.board[4] == self.player and self.board[5] == self.player and self.board[6] == self.player:
# player has won
self.winner = self.player
elif self.board[1] == self.player and self.board[2] == self.player and self.board[3] == self.player:
# player has won
self.winner = self.player
elif self.board[7] == self.player and self.board[4] == self.player and self.board[1] == self.player:
# player has won
self.winner = self.player
elif self.board[8] == self.player and self.board[5] == self.player and self.board[2] == self.player:
# player has won
self.winner = self.player
elif self.board[9] == self.player and self.board[6] == self.player and self.board[3] == self.player:
# player has won
self.winner = self.player
elif self.board[7] == self.player and self.board[5] == self.player and self.board[3] == self.player:
# player has won
self.winner = self.player
elif self.board[9] == self.player and self.board[5] == self.player and self.board[1] == self.player:
# player has won
self.winner = self.player
else:
# player has not won
self.winner = None
# return the winner
return self.winner
# checks if the computer has won
def is_computer_won(self):
# check if the computer has won
if self.board[7] == self.computer and self.board[8] == self.computer and self.board[9] == self.computer:
# computer has won
self.winner = self.computer
elif self.board[4] == self.computer and self.board[5] == self.computer and self.board[6] == self.computer:
# computer has won
self.winner = self.computer
elif self.board[1] == self.computer and self.board[2] == self.computer and self.board[3] == self.computer:
# computer has won
self.winner = self.computer
elif self.board[7] == self.computer and self.board[4] == self.computer and self.board[1] == self.computer:
# computer has won
self.winner = self.computer
elif self.board[8] == self.computer and self.board[5] == self.computer and self.board[2] == self.computer:
# computer has won
self.winner = self.computer
elif self.board[9] == self.computer and self.board[6] == self.computer and self.board[3] == self.computer:
# computer has won
self.winner = self.computer
elif self.board[7] == self.computer and self.board[5] == self.computer and self.board[3] == self.computer:
# computer has won
self.winner = self.computer
elif self.board[9] == self.computer and self.board[5] == self.computer and self.board[1] == self.computer:
# computer has won
self.winner = self.computer
else:
# computer has not won
self.winner = None
# return the winner
return self.winner
# checks if the game is a tie
def is_tie(self):
# check if the game is a tie
if self.move_counter == 9:
# game is a tie
self.tie = True
else:
# game is not a tie
self.tie = False
# return the tie
return self.tie
# check if the move is valid
def is_move_valid(self, move):
# check if the move is valid
if move in range(1, 10) and self.board[move] == " ":
# move is valid
return True
else:
# move is not valid
return False
# makes the move
def make_move(self, move, player):
# make the move
self.board[move] = player
# update the move counter
self.move_counter += 1
# update the board
self.print_board()
# update the winner
self.winner = self.is_player_won()
# update the winner
self.winner = self.is_computer_won()
# update the game status
self.is_game_over()
# update the tie
self.tie = self.is_tie()
def run(self):
# run the game
while self.game_status:
# check if the game is over
if self.is_game_over() == 'draw':
# game is a tie
print("The game is a tie!")
break
elif self.is_game_over() == False:
# game is over
print("The game is over!")
break
# check if the player has won
if self.winner == self.player:
# player has won
print("The player has won!")
break
# check if the computer has won
elif self.winner == self.computer:
# computer has won
print("The computer has won!")
break
# check if the game is a tie
elif self.tie:
# game is a tie
print("The game is a tie!")
break
# ask the player to make a move
move = int(input("Enter your move: "))
# check if the move is valid
if self.is_move_valid(self.move):
# make the move
self.make_move(self.move, self.player)
# check if the game is over
if self.is_game_over() == 'draw':
# game is a tie
print("The game is a tie!")
break
elif self.is_game_over() == False:
# game is over
print("The game is over!")
break
# check if the player has won
if self.winner == self.player:
# player has won
print("The player has won!")
break
# check if the computer has won
elif self.winner == self.computer:
# computer has won
print("The computer has won!")
break
# check if the game is a tie
elif self.tie:
# game is a tie
print("The game is a tie!")
break
# make the computer move
self.make_move(self.computer_move(), self.computer)
# gets the move of the computer
def computer_move(self):
# check if the computer has won
if self.is_computer_won():
# computer has won
return self.winner
# check if the player has won
elif self.is_player_won():
# player has won
return self.winner
# check if the game is a tie
elif self.is_tie():
# game is a tie
return self.tie
# check if the computer has a winning move
elif self.board[5] == self.computer:
# computer has a winning move
return 5
elif self.board[1] == self.computer:
# computer has a winning move
return 1
elif self.board[3] == self.computer:
# computer has a winning move
return 3
elif self.board[7] == self.computer:
# computer has a winning move
return 7
elif self.board[9] == self.computer:
# computer has a winning move
return 9
elif self.board[2] == self.computer:
# computer has a winning move
return 2
elif self.board[8] == self.computer:
# computer has a winning move
return 8
elif self.board[6] == self.computer:
# computer has a winning move
return 6
elif self.board[4] == self.computer:
# computer has a winning move
return 4
# check if the computer has a blocking move
elif self.board[5] == self.player:
# computer has a blocking move
return 5
elif self.board[1] == self.player:
# computer has a blocking move
return 1
elif self.board[3] == self.player:
# computer has a blocking move
return 3
elif self.board[7] == self.player:
# computer has a blocking move
return 7
elif self.board[9] == self.player:
# computer has a blocking move
return 9
# gets the move of the player
def player_move(self):
# check if the player has won
if self.is_player_won():
# player has won
return self.winner
# check if the computer has won
elif self.is_computer_won():
# computer has won
return self.winner
# check if the game is a tie
elif self.is_tie():
# game is a tie
return self.tie
# check if the player has a winning move
elif self.board[5] == self.player:
# player has a winning move
return 5
elif self.board[1] == self.player:
# player has a winning move
return 1
elif self.board[3] == self.player:
# player has a winning move
return 3
elif self.board[7] == self.player:
# player has a winning move
return 7
elif self.board[9] == self.player:
# player has a winning move
return 9
elif self.board[2] == self.player:
# player has a winning move
return 2
elif self.board[8] == self.player:
# player has a winning move
return 8
elif self.board[6] == self.player:
# player has a winning move
return 6
elif self.board[4] == self.player:
# player has a winning move
return 4
# check if the player has a blocking move
elif self.board[5] == self.computer:
# player has a blocking move
return 5
elif self.board[1] == self.computer:
# player has a blocking move
return 1
elif self.board[3] == self.computer:
# player has a blocking move
return 3
elif self.board[7] == self.computer:
# player has a blocking move
return 7
elif self.board[9] == self.computer:
# player has a blocking move
return 9
# create the game
game = TicTacToe()
# run the game
if __name__ == '__main__':
game.run()