-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe_GUI.py
431 lines (378 loc) · 13.1 KB
/
tictactoe_GUI.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import pygame
import random
import copy
#base
matrix = ['', '', '', '', '', '', '', '', '']
#initialize pygame
pygame.init() #IMPORTANT
#box of the game
window = pygame.display.set_mode((600, 750)) #(x, y)
#title and icon
pygame.display.set_caption("Tic Tac Toe")
icon = pygame.image.load('tic-tac-toe.png') #32 pixel
pygame.display.set_icon(icon)
#board
boardIMG = pygame.image.load('board.png')
#players icon
player1IMG = pygame.image.load('Player 1.png')
player2IMG = pygame.image.load('Player 2.png')
#computer icon
computerIMG = pygame.image.load('computer.png')
#score
score1 = 0
score2 = 0
font2 = pygame.font.SysFont('timesnewroman', 60, True)
def display_score(x, y, score_value):
score = font2.render(': ' + str(score_value), True, (255, 255, 255))
window.blit(score, (x, y))
#buttons
class option():
def __init__(self, color, x, y, width, height, font_size):
self.color = color
self.x = x
self.y = y
self.width = width
self.height = height
self.font_size = font_size
def draw(self, window, str = '', border = None):
#Call this method to draw the button on the screen
if border:
pygame.draw.rect(window, border, (self.x-2,self.y-2,self.width+4,self.height+4),0)
#pygame.draw.rect(Surface, color, Rect, width=0)
pygame.draw.rect(window, self.color, (self.x,self.y,self.width,self.height),0)
if str != '':
font = pygame.font.SysFont('verdana', self.font_size)
text = font.render(str, True, (0,0,0))
window.blit(text, (self.x + (self.width/2 - text.get_width()/2), self.y + (self.height/2 - text.get_height()/2)))
def isOver(self, pos):
#Pos is the mouse position or a tuple of (x,y) coordinates
if pos[0] > self.x and pos[0] < self.x + self.width and pos[1] > self.y and pos[1] < self.y + self.height:
return True
return False
# X's and O's
font = pygame.font.SysFont('verdana', 170, True)
def X_O(x, y, str):
xo = font.render(str, True, (255, 255, 255))
window.blit(xo, (x, y))
print_coordinates = [(30, 0), (230, 0), (425, 0), (30, 195), (230, 195), (425, 195), (30, 390), (230, 390), (425, 390)]
x_range = [(0, 190), (225, 378), (412, 600)]
y_range = [(0, 192), (225, 380), (415, 600)] #almost same as x_range tho
def draw_line(x_start, y_start, x_end, y_end):
pygame.time.wait(400)
pygame.draw.line(window, (0, 0, 0), (x_start, y_start), (x_end, y_end), width = 20)
pygame.display.update()
pygame.time.wait(1000)
#win
def win(matrix):
global score1, score2
i = 0
for j in range(3):
if matrix[i] != '' and matrix[i] == matrix[i + 1] and matrix[i + 1] == matrix[i + 2]: #horizontal
if j == 0:
draw_line(5, 96, 595, 96)
elif j == 1:
draw_line(5, 305, 595, 305)
else:
draw_line(5, 510, 595, 510)
if matrix[i] == 'X':
score1 += 1
finish.draw(window, 'Player 1 Won', (255, 255, 255))
pygame.display.update()
pygame.time.wait(1000)
elif matrix[i] == 'O':
score2 += 1
finish.draw(window, 'Player 2 Won', (255, 255, 255))
pygame.display.update()
pygame.time.wait(1000)
return True
i = i + 3
for j in range(3):
if matrix[j] != '' and matrix[j] == matrix[j + 3] and matrix[j + 3] == matrix[j + 6]: #vertical
if j == 0:
draw_line(94, 5, 94, 595)
elif j == 1:
draw_line(301, 5, 301, 595)
else:
draw_line(507, 5, 507, 595)
if matrix[j] == 'X':
score1 += 1
finish.draw(window, 'Player 1 Won', (255, 255, 255))
pygame.display.update()
pygame.time.wait(1000)
elif matrix[j] == 'O':
score2 += 1
finish.draw(window, 'Player 2 Won', (255, 255, 255))
pygame.display.update()
pygame.time.wait(1000)
return True
if matrix[0] != '' and matrix[0] == matrix[4] and matrix[4] == matrix[8]: #diagonal 1
draw_line(0, 0, 600, 600)
if matrix[4] == 'X':
score1 += 1
finish.draw(window, 'Player 1 Won', (255, 255, 255))
pygame.display.update()
pygame.time.wait(1000)
elif matrix[4] == 'O':
score2 += 1
finish.draw(window, 'Player 2 Won', (255, 255, 255))
pygame.display.update()
pygame.time.wait(1000)
return True
elif matrix[2] != '' and matrix[2] == matrix[4] and matrix[4] == matrix[6]: #diagonal 2
draw_line(600, 0, 0, 600)
if matrix[4] == 'X':
score1 += 1
finish.draw(window, 'Player 1 Won', (255, 255, 255))
pygame.display.update()
pygame.time.wait(1000)
elif matrix[4] == 'O':
score2 += 1
finish.draw(window, 'Player 2 Won', (255, 255, 255))
pygame.display.update()
pygame.time.wait(1000)
return True
return False
#draw
def draw(matrix): #when the matrix is full
for i in matrix:
if i == '':
return False
finish.draw(window, 'DRAW!', (255, 255, 255))
pygame.display.update()
pygame.time.wait(1000)
return True
def find_win(matrix):
for i in [0, 3, 6]: #horizontal
index = -1
count = 0
k = i
for j in range(3):
if matrix[k] == 'O':
count = count + 1
elif matrix[k] == '':
index = k
k = k + 1
if count == 2 and index != -1:
matrix[index] = 'O'
return True
for i in range(3): #vertical
index = -1
count = 0
k = i
for j in range(3):
if matrix[k] == 'O':
count = count + 1
elif matrix[k] == '':
index = k
k = k + 3
if count == 2 and index != -1:
matrix[index] = 'O'
return True
index = -1
count = 0
for i in [0, 4, 8]: #diagonal 1
if matrix[i] == 'O':
count = count + 1
elif matrix[i] == '':
index = i
if count == 2 and index != -1:
matrix[index] = 'O'
return True
index = -1
count = 0
for i in [6, 4, 2]: #diagonal 2
if matrix[i] == 'O':
count = count + 1
elif matrix[i] == '':
index = i
if count == 2 and index != -1:
matrix[index] = 'O'
return True
return False
def find_save(matrix):
for i in [0, 3, 6]: #horizontal
index = -1
count = 0
k = i
for j in range(3):
if matrix[k] == 'X':
count = count + 1
elif matrix[k] == '':
index = k
k = k + 1
if count == 2 and index != -1:
matrix[index] = 'O'
return True
for i in range(3): #vertical
index = -1
count = 0
k = i
for j in range(3):
if matrix[k] == 'X':
count = count + 1
elif matrix[k] == '':
index = k
k = k + 3
if count == 2 and index != -1:
matrix[index] = 'O'
return True
index = -1
count = 0
for i in [0, 4, 8]: #diagonal 1
if matrix[i] == 'X':
count = count + 1
elif matrix[i] == '':
index = i
if count == 2 and index != -1:
matrix[index] = 'O'
return True
index = -1
count = 0
for i in [6, 4, 2]: #diagonal 2
if matrix[i] == 'X':
count = count + 1
elif matrix[i] == '':
index = i
if count == 2 and index != -1:
matrix[index] = 'O'
return True
return False
def find_GoodMove(matrix):
l = []
for i in range(9):
if matrix[i] == '':
l.append(i)
l_copy = copy.deepcopy(l)
greater_win = 0
good_indexes = []
while l:
ind = l.pop()
matrix[ind] = 'O'
win = 0
for i in [0, 3, 6]: #horizontal
index = -1
count = 0
k = i
for j in range(3):
if matrix[k] == 'O':
count = count + 1
elif matrix[k] == '':
index = k
k = k + 1
if count == 2 and index != -1:
win = win + 1
for i in range(3): #vertical
index = -1
count = 0
k = i
for j in range(3):
if matrix[k] == 'O':
count = count + 1
elif matrix[k] == '':
index = k
k = k + 3
if count == 2 and index != -1:
win = win + 1
index = -1
count = 0
for i in [0, 4, 8]: #diagonal 1
if matrix[i] == 'O':
count = count + 1
elif matrix[i] == '':
index = i
if count == 2 and index != -1:
win = win + 1
index = -1
count = 0
for i in [6, 4, 2]: #diagonal 2
if matrix[i] == 'O':
count = count + 1
elif matrix[i] == '':
index = i
if count == 2 and index != -1:
win = win + 1
matrix[ind] = ''
if win > greater_win:
greater_win = win
good_indexes.clear()
good_indexes.append(ind)
elif win == greater_win:
good_indexes.append(ind)
if good_indexes:
#print(greater_win)
matrix[random.choice(good_indexes)] = 'O'
else:
matrix[random.choice(l_copy)] = 'O'
#setting images
running = True
start_game = 0
#for center: x-coordinate = center of width of window - width of button/2, y-coordinate = center of height of window - height of button/2
vs_player = option((135,206,235), 150, 405, 300, 100, 60)
vs_computer = option((135,206,235), 150, 245, 300, 100, 60)
finish = option((135,206,235), 50, 250, 500, 100, 60)
boxes = [option((255, 128, 255), 0, 0, 190, 193, 120), option((255, 128, 255), 225, 0, 154, 193, 120), option((255, 128, 255), 412, 0, 190, 193, 120), option((255, 128, 255), 0, 225, 190, 157, 120), option((255, 128, 255), 225, 225, 154, 157, 120), option((255, 128, 255), 412, 225, 190, 157, 120), option((255, 128, 255), 0, 415, 190, 185, 120), option((255, 128, 255), 225, 415, 154, 185, 120), option((255, 128, 255), 412, 415, 190, 185, 120)]
#game loop
current = 0
while running:
pos = pygame.mouse.get_pos()
#print(pos)
window.fill((255, 128, 255)) #RGB value (mint green) (in case u don't want any background image, u can colour background using this)
#turns
if current%2 == 0:
turn = 'X'
else:
turn = 'O'
if start_game == 1:
if not find_win(matrix):
if not find_save(matrix):
find_GoodMove(matrix)
current = current + 1
pygame.time.wait(500)
#input
for input in pygame.event.get(): #Here, event detects any input
#quit
if input.type == pygame.QUIT:
running = False
#mouse click
if input.type == pygame.MOUSEBUTTONDOWN:
if start_game:
for i in range(9):
if boxes[i].isOver(pos) and matrix[i] == '':
current = current + 1
matrix[i] = turn
else:
if vs_player.isOver(pos):
start_game = 2
elif vs_computer.isOver(pos):
start_game = 1
#mouse hovering
if start_game == 0 and input.type == pygame.MOUSEMOTION:
if vs_player.isOver(pos):
vs_player.color = (0, 0, 139)
else:
vs_player.color = (135, 206, 235)
if vs_computer.isOver(pos):
vs_computer.color = (0, 0, 139)
else:
vs_computer.color = (135, 206, 235)
#Start Screen
if start_game > 0:
window.blit(boardIMG, (0, 0))
for i in range(9):
boxes[i].draw(window, matrix[i])
window.blit(player1IMG, (118, 660))
if start_game == 2:
window.blit(player2IMG, (418, 660))
else:
window.blit(computerIMG, (418, 660))
else:
#start_option.draw(window, 'Play', (255, 255, 255))
vs_player.draw(window, '2 Players', (255, 255, 255))
vs_computer.draw(window, '1 Player', (255, 255, 255))
#score
if start_game > 0:
display_score(185, 660, score1)
display_score(485, 660, score2)
pygame.display.update() #IMPORTANT
if win(matrix) or draw(matrix):
matrix = ['', '', '', '', '', '', '', '', ''] #resets the board