-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
87 lines (78 loc) · 2.7 KB
/
main.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
import pygame
from classes import Board, Mark, Button
import minimaxAI
pygame.init()
pygame.display.set_caption("Tic Tac Toe")
winWidth = 500
winHeight = 550
win = pygame.display.set_mode((winWidth, winHeight))
winColour = (255, 255, 255)
win.fill(winColour)
largeSize = 350
gridPos = (75, 175)
grid = Board(gridPos, largeSize)
grid.drawGrid(win)
playX = Button('Play X', 75, 50, Mark.xcolour)
playO = Button('Play O', winWidth-75-Button.width, 50, Mark.ocolour)
pygame.display.update()
depth = 100
gameWin = (False, '')
gameTie = False
run = True
while run:
event = pygame.event.wait()
if event.type == pygame.QUIT:
run = False
if not Button.clicked:
# mouse hovered over button
mouse = pygame.mouse.get_pos()
if playX.hover(win, mouse):
pygame.display.update()
elif playO.hover(win, mouse):
pygame.display.update()
else:
playX.draw(win, False)
playO.draw(win, False)
pygame.display.update()
# mouse clicks a button setting human player
if event.type == pygame.MOUSEBUTTONUP:
if playX.click(win, mouse):
pygame.display.update()
grid.setPlayers('X')
elif playO.click(win, mouse):
pygame.display.update()
grid.setPlayers('O')
elif grid.humanTurn():
if event.type == pygame.MOUSEBUTTONUP:
# mouse clicked on grid
column = grid.findColumn(event.pos[0])
row = grid.findRow(event.pos[1])
grid.update(win, row, column)
gameWin = (grid.checkWin(grid.human), grid.human)
gameTie = grid.checkTie()
pygame.display.update()
grid.nextTurn()
else:
aimove = minimaxAI.bestMove(grid.squares, depth, grid.ai)
grid.update(win, aimove[0], aimove[1])
pygame.display.update()
gameWin = (grid.checkWin(grid.ai), grid.ai)
gameTie = grid.checkTie()
grid.nextTurn()
if gameWin[0] or gameTie:
pygame.time.delay(700)
win.fill(winColour)
font = pygame.font.Font('freesansbold.ttf', 64)
if gameTie:
text = font.render('Tie Game', True, Mark.ocolour)
elif gameWin[1] == 'X':
text = font.render('X Wins', True, Mark.xcolour)
elif gameWin[1] == 'O':
text = font.render('O Wins', True, Mark.ocolour)
textRect = text.get_rect()
textRect.center = (winWidth // 2, winHeight // 2)
win.blit(text, textRect)
pygame.display.update()
pygame.time.delay(1500)
run = False
pygame.quit()