-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.py
325 lines (247 loc) · 9.87 KB
/
tictactoe.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
import sys
import copy
import pygame
import random
import numpy as np
# CONSTANTS
# SHAPE & SIZE
WIDTH = 400
HEIGHT = 400
ROWS = 3
COLS = 3
SQSIZE = WIDTH // ROWS
LINE_WIDTH = 12
CIRCLE_WIDTH = 12
CROSS_WIDTH = 14
RADIUS = SQSIZE // 4
OFFSET = 40
# COLORS
BG_COLOR = (28, 170, 156)
LINE_COLOR = (23, 145, 135)
CIRCLE_COLOR = (239, 231, 200)
CROSS_COLOR = (66, 66, 66)
# PYGAME SETUP
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
screen.fill(BG_COLOR)
pygame.display.set_caption("TIC TAC TOE AI")
mode = None
class Board:
def __init__(self):
self.squares = np.zeros((ROWS, COLS))
self.empty_sqrs = self.squares
self.marked_sqrs = 0
def final_state(self, show=False):
# player 1 won - returns 1
# player 2 won - returns 2
# match draw - returns 0
# vertical wins
for col in range(COLS):
if self.squares[0][col] == self.squares[1][col] == self.squares[2][col] != 0:
if show:
color = CIRCLE_COLOR if self.squares[0][col] == 2 else CROSS_COLOR
initial_pos = (col * SQSIZE + SQSIZE // 2, 20)
final_pos = (col * SQSIZE + SQSIZE // 2, HEIGHT - 20)
ch = 'X' if self.squares[0][col] == 1 else 'O'
pygame.display.set_caption(f"TIC TAC TOE AI => Player {ch} won")
pygame.draw.line(screen, color, initial_pos, final_pos, LINE_WIDTH)
return self.squares[0][col]
# horizontal wins
for row in range(ROWS):
if self.squares[row][0] == self.squares[row][1] == self.squares[row][2] != 0:
if show:
color = CIRCLE_COLOR if self.squares[row][0] == 2 else CROSS_COLOR
initial_pos = (20, row * SQSIZE + SQSIZE // 2)
final_pos = (WIDTH - 20, row * SQSIZE + SQSIZE // 2)
ch = 'X' if self.squares[row][0] == 1 else 'O'
pygame.display.set_caption(f"TIC TAC TOE AI => Player {ch} won")
pygame.draw.line(screen, color, initial_pos, final_pos, LINE_WIDTH)
return self.squares[row][0]
# desc diagonal
if self.squares[0][0] == self.squares[1][1] == self.squares[2][2] != 0:
if show:
color = CIRCLE_COLOR if self.squares[0][0] == 2 else CROSS_COLOR
initial_pos = (20, 20)
final_pos = (WIDTH - 20, HEIGHT - 20)
ch = 'X' if self.squares[0][0] == 1 else 'O'
pygame.display.set_caption(f"TIC TAC TOE AI => Player {ch} won")
pygame.draw.line(screen, color, initial_pos, final_pos, CROSS_WIDTH)
return self.squares[0][0]
# asc diagonal
if self.squares[0][2] == self.squares[1][1] == self.squares[2][0] != 0:
if show:
color = CIRCLE_COLOR if self.squares[0][2] == 2 else CROSS_COLOR
initial_pos = (20, HEIGHT - 20)
final_pos = (WIDTH - 20, 20)
ch = 'X' if self.squares[0][2] == 1 else 'O'
pygame.display.set_caption(f"TIC TAC TOE AI => Player {ch} won")
pygame.draw.line(screen, color, initial_pos, final_pos, CROSS_WIDTH)
return self.squares[0][2]
return 0
def mark_sqr(self, row, col, player):
self.squares[row][col] = player
self.marked_sqrs += 1
def empty_sqr(self, row, col):
return self.squares[row][col] == 0
def get_empty_sqrs(self):
empty_sqrs = []
for row in range(ROWS):
for col in range(COLS):
if self.empty_sqr(row, col):
empty_sqrs.append((row, col))
return empty_sqrs
def isfull(self):
return self.marked_sqrs == 9
def isempty(self):
return self.marked_sqrs == 0
class AI:
def __init__(self, level=1, player=2):
self.level = level
self.player = player
def rnd(self, board):
empty_sqrs = board.get_empty_sqrs()
index = random.randrange(0, len(empty_sqrs))
return empty_sqrs[index] # (row, col)
def minimax(self, board, maximizing):
# terminal case
result = board.final_state()
# player 1 wins
if result == 1:
return 1, None # eval, move
# player 2 wins
if result == 2:
return -1, None # eval, move
# match draw
elif board.isfull():
return 0, None # eval, move
if maximizing: # maximizing player
max_eval = -100
best_move = None
empty_sqrs = board.get_empty_sqrs()
for (row, col) in empty_sqrs:
temp_board = copy.deepcopy(board)
temp_board.mark_sqr(row, col, 1)
eval = self.minimax(temp_board, False)[0]
if eval > max_eval:
max_eval = eval
best_move = (row, col)
return max_eval, best_move
elif not maximizing: # minimizing player(ai)
min_eval = 100
best_move = None
empty_sqrs = board.get_empty_sqrs()
for (row, col) in empty_sqrs:
temp_board = copy.deepcopy(board)
temp_board.mark_sqr(row, col, self.player)
eval = self.minimax(temp_board, True)[0]
if eval < min_eval:
min_eval = eval
best_move = (row, col)
return min_eval, best_move
def eval(self, main_board):
if self.level == 0:
# random ai
eval = 'random'
move = self.rnd(main_board)
else:
# mini max ai
eval , move = self.minimax(main_board, False)
print(f'AI has chosen: {move} with an eval of: {eval}')
return move # row, col
class Game:
def __init__(self):
self.board = Board()
self.ai = AI()
self.player = 1
self.gamemode = 'ai' # or 'ai'
self.running = True
self.show_lines()
def make_move(self, row, col):
self.board.mark_sqr(row, col, self.player)
self.draw_fig(row, col)
self.next_turn()
def show_lines(self):
# background
screen.fill(BG_COLOR)
# vertical lines
pygame.draw.line(screen, LINE_COLOR, (SQSIZE, 0), (SQSIZE, HEIGHT), LINE_WIDTH)
pygame.draw.line(screen, LINE_COLOR, (WIDTH - SQSIZE, 0), (WIDTH - SQSIZE, HEIGHT), LINE_WIDTH)
# horizontal lines
pygame.draw.line(screen, LINE_COLOR, (0, SQSIZE), (WIDTH, SQSIZE), LINE_WIDTH)
pygame.draw.line(screen, LINE_COLOR, (0, HEIGHT - SQSIZE), (WIDTH, HEIGHT - SQSIZE), LINE_WIDTH)
def draw_fig(self, row, col):
if self.player == 1:
# draw 'X'
# desc line
start_desc = (col * SQSIZE + OFFSET, row * SQSIZE + OFFSET)
end_desc = (col * SQSIZE + SQSIZE - OFFSET, row * SQSIZE + SQSIZE - OFFSET)
pygame.draw.line(screen, CROSS_COLOR, start_desc, end_desc, CROSS_WIDTH)
# asc line
start_asc = (col * SQSIZE + OFFSET, row * SQSIZE + SQSIZE - OFFSET)
end_asc = (col * SQSIZE + SQSIZE - OFFSET, row * SQSIZE + OFFSET)
pygame.draw.line(screen, CROSS_COLOR, start_asc, end_asc, CROSS_WIDTH)
elif self.player == 2:
# draw 'O'
center = (col * SQSIZE + SQSIZE // 2, row * SQSIZE + SQSIZE // 2)
pygame.draw.circle(screen, CIRCLE_COLOR, center, RADIUS, CIRCLE_WIDTH)
def next_turn(self):
self.player = self.player % 2 + 1
ch = 'X' if self.player == 1 else 'O'
pygame.display.set_caption(f"TIC TAC TOE AI => Player {ch}")
def change_gamemode(self):
self.gamemode = 'ai' if self.gamemode == 'pvp' else 'pvp'
mode = self.gamemode
def isOver(self):
return self.board.final_state(show=True) != 0 or self.board.isfull()
def reset(self):
self.__init__()
def main():
# game object
game = Game()
mode = game.gamemode
board = game.board
ai = game.ai
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
# g - game mode
if event.key == pygame.K_g:
game.change_gamemode()
# r - restart
if event.key == pygame.K_r:
game.reset()
board = game.board
ai = game.ai
# 0 - random ai
if event.key == pygame.K_0:
ai.level = 0
# 1 - minimax ai
if event.key == pygame.K_1:
ai.level = 1
if event.type == pygame.MOUSEBUTTONDOWN:
pos = event.pos
row = pos[1] // SQSIZE
col = pos[0] // SQSIZE
if board.empty_sqr(row, col) and game.running:
game.make_move(row, col)
if game.isOver():
print("Match Finished")
if board.isfull():
pygame.display.set_caption(f"TIC TAC TOE AI => Match Draw(Press r to restart)")
game.running = False
if game.gamemode == 'ai' and game.player == ai.player and game.running:
# update the screen
pygame.display.update()
# ai methods
row, col = ai.eval(board)
game.make_move(row, col)
if game.isOver():
print("Match Finished")
if board.isfull():
pygame.display.set_caption(f"TIC TAC TOE AI => Match Draw(Press r to restart)")
game.running = False
pygame.display.update()
main()