-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.py
290 lines (257 loc) · 9.15 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
import math
import time
import pygame
import random
pygame.init()
clock = pygame.time.Clock()
WIN = pygame.display.set_mode((540, 600))
pygame.display.set_caption("N Queen")
FPS = 20
# colours
BLACK = (0, 0, 0)
WHITE = (220, 220, 220)
RED = (230, 20, 40)
BLUE = (20, 80, 220)
OClr = BLUE
XClr = RED
txtClr = BLACK
boardClr = WHITE
playerScorePos = (30, 560)
compScorePos = (300, 560)
def PYtxt(txt: str, fontSize: int = 28, fontColour: tuple = BLACK, font: str = 'freesansbold.ttf'):
return (pygame.font.Font(font, fontSize)).render(txt, True, fontColour)
class tic_tac_toe():
def __init__(self, grid, width, height, currentPlayer) -> None:
self.grid = grid
self.width = width
self.height = height
self.player = 'X'
self.computer = 'O'
self.board = self.create_board(self.grid)
self.cubes = [
[Cube(self.board[i][j], i, j, width, height, self.grid)
for j in range(self.grid)]
for i in range(self.grid)
]
self.available_moves = self.moves()
self.currentPlayer = currentPlayer
self.winner = None
self.playerWins = 0
self.compWins = 0
def create_board(self, grid):
board = []
for i in range(grid):
helper = []
for j in range(grid):
helper.append(0)
board.append(helper)
self.grid = len(board)
return board
def drawGrid(self):
WIN.fill(boardClr)
rowGap = self.height / self.grid
colGap = self.width / self.grid
# Draw Cubes
for i in range(self.grid):
for j in range(self.grid):
self.cubes[i][j].draw(WIN)
thick = 1
# pygame.draw.line(win, (0, 0, 0), (i * rowGap, 0),i * rowGap, self.height), thick)
for i in range(self.grid+1):
pygame.draw.line(WIN, BLACK, (0, i*rowGap),
(self.height, rowGap*i), thick)
for i in range(self.grid):
pygame.draw.line(WIN, BLACK, (i*colGap, 0), (colGap*i, self.width))
WIN.blit(PYtxt(f"playerScore : {self.playerWins}"), playerScorePos)
WIN.blit(PYtxt(f"computer : {self.compWins}"), compScorePos)
pygame.display.update()
def print_board(self):
for i in range(len(self.board)):
print("-----------------")
for j in range(len(self.board[0])):
txt = self.board[i][j]
if txt == 0:
txt = " "
print("| "+txt, end=" ")
print("|")
def moves(self):
spaces = []
for i in range(len(self.board)):
for j in range(len(self.board)):
if self.board[i][j] == 0:
spaces.append((i, j))
return spaces
def checkWinner(self, pos):
i, j = pos
# checking row
if self.board[i][0] != 0:
res = all(ele == self.board[i][0] for ele in self.board[i])
if res == True:
return self.board[i][0]
# checking in a col
if self.board[0][j]:
res = all(ele[j] == self.board[0][j] for ele in self.board)
if res == True:
return self.board[0][j]
# checking principal diagonal
if i == j or i == len(self.board)-1-j:
if self.board[0][0] != 0:
res = all(self.board[i][i] == self.board[0][0]
for i in range(len(self.board)))
if res == True:
return self.board[0][0]
if self.board[0][len(self.board)-1] != 0:
res = all(self.board[i][j] == self.board[0][len(
self.board)-1]for i, j in zip(range(0, len(self.board), 1), range(len(self.board)-1, -1, -1)))
if res == True:
return self.board[0][len(self.board)-1]
def place_ai(self, pos, player):
x, y = pos
self.board[x][y] = player
self.winner = self.checkWinner(pos)
def place_board(self, pos, player):
x, y = pos
if self.board[x][y] == 0:
self.board[x][y] = player
self.cubes[x][y].value = player
self.winner = self.checkWinner(pos)
if self.winner == self.computer:
self.compWins += 1
elif self.winner != None:
self.playerWins += 1
return True
else:
print("Illegal move")
return False
class Cube():
def __init__(self, value, row, col, width, height, grid):
self.value = value
self.row = row
self.col = col
self.width = width
self.height = height
self.grid = grid
self.centerFactor = 10
def draw(self, win):
# fnt = pygame.font.SysFont("comicsans", 40)
textCorrectionFactor = 64
rowGap = self.height / self.grid
# colGap = self.width / self.grid
x = self.col * rowGap
y = self.row * rowGap
if self.value != 0:
# fnt = pygame.font.SysFont("comicsans", 40)
# text = fnt.render(self.value, 1, txtClr)
if self.value == 'X':
text = PYtxt(self.value, int(rowGap)-textCorrectionFactor, RED)
else:
text = PYtxt(self.value, int(rowGap) -
textCorrectionFactor, BLUE)
win.blit(text, (x + (rowGap/2 - text.get_width()/2),
y + (rowGap/2 - text.get_height()/2)))
# def moves(board):
# spaces = []
# for i in range(len(board)):
# for j in range(len(board)):
# if board[i][j] == 0:
# spaces.append((i, j))
# return spaces
def miniMax(board, player):
moves = len(board.moves())
if moves == len(board.board)**2:
return {
'position': random.choice([(0, 0), (1, 1), (2, 2), (2, 0)]),
}
maxPlayer = board.computer
otherPlayer = board.player if player == board.computer else board.computer
if board.winner != None:
spaces = len(board.available_moves) + 1
return {
'position': None,
'score': 1 * spaces if otherPlayer == maxPlayer else -1 * spaces,
}
if moves == 0:
return {
'position': None,
'score': 0
}
if player == maxPlayer:
best = {
'position': None,
'score': -math.inf
}
else:
best = {
'position': None,
'score': math.inf,
}
for possible_moves in board.moves():
x, y = possible_moves
board.place_ai((x, y), player)
sim_score = miniMax(board, otherPlayer)
board.place_ai((x, y), 0)
sim_score['position'] = (x, y)
if player == maxPlayer:
if sim_score['score'] > best['score']:
best = sim_score
else:
if sim_score['score'] < best['score']:
best = sim_score
return best
def main():
board = tic_tac_toe(3, 540, 540, 'X')
# board.print_board()
run = True
board.drawGrid()
itr = 0
# game Loop
while run:
clock.tick(FPS)
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
run = False
if board.currentPlayer == board.player:
# choice = int(
# input(f"enter the {board.currentPlayer} poisition : "))
# board.place_board((choice//3, choice % 3), board.player)
# board.currentPlayer = board.computer
if pygame.mouse.get_pressed()[0]:
x, y = pygame.mouse.get_pos()
if board.place_board((y//(540//3), x//(540//3)), board.player):
board.currentPlayer = board.computer
board.drawGrid()
else:
# choice = int(
# input(f"enter the {board.currentPlayer} poisition : "))
# board.place_board((choice//3, choice % 3), board.computer)
if pygame.mouse.get_pressed()[0]:
pos = miniMax(board, board.computer)
pos = pos['position']
# for slowing the ai
if itr:
time.sleep(0.5)
if not pos == None:
board.place_board(pos, board.computer)
itr += 1
# board.print_board()
# if pygame.mouse.get_pressed()[0]:
# x, y = pygame.mouse.get_pos()
# board.place_board((y//(540//3), x//(540//3)), board.computer)
# board.currentPlayer = board.player
board.drawGrid()
board.currentPlayer = board.player
# this is needed to dont skip the check of the above block and exec the whole programm again
time.sleep(1)
if board.winner != None or len(board.moves()) == 0:
print(board.winner)
board.board = board.create_board(board.grid)
for i in range(board.grid):
for j in range(board.grid):
board.cubes[i][j].value = 0
board.drawGrid()
board.winner = None
itr = 0
pygame.quit()
if __name__ == '__main__':
main()