-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtic_tac_toe.py
More file actions
executable file
·408 lines (310 loc) · 14.1 KB
/
tic_tac_toe.py
File metadata and controls
executable file
·408 lines (310 loc) · 14.1 KB
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
import pygame, sys, random, copy
from pygame.locals import *
from TicTacToeBot import TicTacToeBot
##CONSTANTS, yo##
WINDOWWIDTH = 640
WINDOWHEIGHT = 480
FPS = 30
BOXSIZE = 100
GAPSIZE = 10
BOARDWIDTH = 3
BOARDHEIGHT = 3
XMARK = 'X'
OMARK = 'O'
XMARGIN = int((WINDOWWIDTH -(BOARDWIDTH * (BOXSIZE + GAPSIZE)))/2)
YMARGIN = int((WINDOWHEIGHT - (BOARDHEIGHT * (BOXSIZE + GAPSIZE)))/2)
# R G B
GRAY = (100, 100, 100)
NAVYBLUE = ( 60, 60, 100)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)
YELLOW = (255, 255, 0)
ORANGE = (255, 128, 0)
PURPLE = (255, 0, 255)
CYAN = ( 0, 255, 255)
BLACK = ( 0, 0, 0)
COMBLUE = (233, 232, 255)
BGCOLOR = BLACK
BOXCOLOR = BGCOLOR
LINECOLOR = COMBLUE
def main():
global FPSCLOCK, DISPLAYSURF
pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
mainFont = pygame.font.SysFont('Arial', 90)
smallFont = pygame.font.SysFont('Arial', 25)
pygame.display.set_caption('Tic-Tac-Toe')
bot1 = TicTacToeBot()
#bot1.train(100)
bot1.load_data("b3.dat")
#bot1.evaluate_action(...)
playerScore = 0
computerScore = 0
tieScore = 0
playerWins = False
computerWins = False
mousex = None
mainBoard = makeEachBoxFalse(False)
usedBoxes = makeEachBoxFalse(False)
player = XMARK
DISPLAYSURF.fill(BGCOLOR)
drawLines()
player, usedBoxes, mainBoard, playerWins, computerWins = boardReset(mainFont, bot1, player, usedBoxes, mainBoard, playerWins, computerWins, bot1)
pygame.display.update()
while True:
#bprint(player)
playerWins, computerWins = gameWon(mainBoard)
if playerWins:
pygame.time.wait(500)
highlightWin(mainBoard)
pygame.time.wait(1000)
pygame.display.update()
playerScore += 1
DISPLAYSURF.fill(BGCOLOR)
drawLines()
player, usedBoxes, mainBoard, playerWins, computerWins = boardReset(mainFont, bot1, player, usedBoxes, mainBoard, playerWins, computerWins, bot1)
pygame.display.update()
elif computerWins:
pygame.time.wait(500)
highlightWin(mainBoard)
pygame.time.wait(1000)
pygame.display.update()
computerScore += 1
DISPLAYSURF.fill(BGCOLOR)
drawLines()
player, usedBoxes, mainBoard, playerWins, computerWins = boardReset(mainFont, bot1, player, usedBoxes, mainBoard, playerWins, computerWins, bot1)
pygame.display.update()
else:
if gameOver(usedBoxes, mainBoard):
tieScore += 1
DISPLAYSURF.fill(BGCOLOR)
drawLines()
player,usedBoxes, mainBoard, playerWins, computerWins = boardReset(mainFont, bot1, player,usedBoxes, mainBoard, playerWins, computerWins, bot1)
pygame.display.update()
if player == OMARK:
spielfeld = [0]*9
for y in range(3):
for x in range(3):
if mainBoard[x][y] == OMARK:
spielfeld[x+3*y] = 1
if mainBoard[x][y] == XMARK:
spielfeld[x+3*y] = 2
(boxx, boxy) = bot1.get_action(spielfeld)
pygame.time.wait(500)
player = makeMove(bot1,player,usedBoxes,mainBoard,mainFont,boxx,boxy)
else:
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
# elif evmakeMoveent.type == MOUSEMOTION:
# mousex, mousey = event.pos
elif event.type == MOUSEBUTTONUP:
mousex, mousey = event.pos
boxx, boxy = getBoxAtPixel(mousex, mousey)
player = makeMove(bot1,player,usedBoxes,mainBoard,mainFont,boxx,boxy)
drawScoreBoard(smallFont, playerScore, computerScore, tieScore)
pygame.display.update()
FPSCLOCK.tick(FPS)
###### Functions to set up the board #########
def drawLines():
#######VERTICAL LINES########
left = XMARGIN + BOXSIZE
top = YMARGIN
width = GAPSIZE
height = (BOXSIZE + GAPSIZE) * BOARDHEIGHT
vertRect1 = pygame.Rect(left, top, width, height)
pygame.draw.rect(DISPLAYSURF, WHITE, vertRect1)
vertRect2 = pygame.Rect(left + BOXSIZE + GAPSIZE, top, width, height)
pygame.draw.rect(DISPLAYSURF, WHITE, vertRect2)
########HORIZONTAL LINES ##########
left = XMARGIN
top = YMARGIN + BOXSIZE
width = (BOXSIZE + GAPSIZE) * BOARDWIDTH
height = GAPSIZE
horizRect1 = pygame.Rect(left, top, width, height)
pygame.draw.rect(DISPLAYSURF, WHITE, horizRect1)
horizRect2 = pygame.Rect(left, top + BOXSIZE + GAPSIZE, width, height)
pygame.draw.rect(DISPLAYSURF, WHITE, horizRect2)
def makeEachBoxFalse(val):
usedBoxes = []
for i in range(BOARDWIDTH):
usedBoxes.append([val] * BOARDHEIGHT)
return usedBoxes
def drawScoreBoard(smallFont, playerScore, computerScore, tieScore):
scoreBoard = smallFont.render('Player: ' + str(playerScore) + ' ' + 'Computer: ' + str(computerScore) + ' ' + 'Tie: ' + str(tieScore), True, COMBLUE, BGCOLOR)
scoreBoardRect = scoreBoard.get_rect()
scoreBoardRect.x = 0
scoreBoardRect.y = 0
DISPLAYSURF.blit(scoreBoard, scoreBoardRect)
##### Coordinate Functions #####
def getBoxAtPixel(x, y):
for boxx in range(BOARDWIDTH):
for boxy in range(BOARDHEIGHT):
left, top = leftTopCoordsOfBox(boxx, boxy)
boxRect = pygame.Rect(left, top, BOXSIZE, BOXSIZE)
if boxRect.collidepoint(x, y):
return (boxx, boxy)
return (None, None)
def leftTopCoordsOfBox(boxx, boxy):
left = boxx *(BOXSIZE + GAPSIZE) + XMARGIN
top = boxy * (BOXSIZE + GAPSIZE) + YMARGIN
return left, top
def centerxAndCenteryOfBox(boxx, boxy):
centerx = boxx * (BOXSIZE + GAPSIZE) + XMARGIN + (BOXSIZE / 2)
centery = boxy * (BOXSIZE + GAPSIZE) + YMARGIN + (BOXSIZE / 2) + 5
return centerx, centery
##### Functions dealing with computer and player moves ######
def makeMove(bot1,player,usedBoxes,mainBoard, mainFont,boxx = None,boxy = None):
if boxx != None and boxy != None:
if not usedBoxes[boxx][boxy]:
markBox(player, boxx, boxy, mainFont)
usedBoxes[boxx][boxy] = True
mainBoard[boxx][boxy] = player
if player == XMARK:
player = OMARK
else: player = XMARK
pygame.display.update()
return player
def markBox(player, boxx, boxy, mainFont):
centerx, centery = centerxAndCenteryOfBox(boxx, boxy)
mark = mainFont.render(player, True, GREEN)
markRect = mark.get_rect()
markRect.centerx = centerx
markRect.centery = centery
DISPLAYSURF.blit(mark, markRect)
##### Functions dealing with an end of game ########
def gameWon(mainBoard):
if ((mainBoard[0][0] == mainBoard[1][0] == mainBoard[2][0] == XMARK) or
(mainBoard[0][1] == mainBoard[1][1] == mainBoard[2][1] == XMARK) or
(mainBoard[0][2] == mainBoard[1][2] == mainBoard[2][2] == XMARK) or
(mainBoard[0][0] == mainBoard[0][1] == mainBoard[0][2] == XMARK) or
(mainBoard[1][0] == mainBoard[1][1] == mainBoard[1][2] == XMARK) or
(mainBoard[2][0] == mainBoard[2][1] == mainBoard[2][2] == XMARK) or
(mainBoard[0][0] == mainBoard[1][1] == mainBoard[2][2] == XMARK) or
(mainBoard[0][2] == mainBoard[1][1] == mainBoard[2][0] == XMARK)):
playerWins = True
computerWins = False
return playerWins, computerWins
elif ((mainBoard[0][0] == mainBoard[1][0] == mainBoard[2][0] == OMARK) or
(mainBoard[0][1] == mainBoard[1][1] == mainBoard[2][1] == OMARK) or
(mainBoard[0][2] == mainBoard[1][2] == mainBoard[2][2] == OMARK) or
(mainBoard[0][0] == mainBoard[0][1] == mainBoard[0][2] == OMARK) or
(mainBoard[1][0] == mainBoard[1][1] == mainBoard[1][2] == OMARK) or
(mainBoard[2][0] == mainBoard[2][1] == mainBoard[2][2] == OMARK) or
(mainBoard[0][0] == mainBoard[1][1] == mainBoard[2][2] == OMARK) or
(mainBoard[0][2] == mainBoard[1][1] == mainBoard[2][0] == OMARK)):
playerWins = False
computerWins = True
return playerWins, computerWins
else:
playerWins = False
computerWins = False
return playerWins, computerWins
def gameOver(usedBoxes, mainBoard):
for boxx in range(BOARDWIDTH):
for boxy in range(BOARDHEIGHT):
if usedBoxes[boxx][boxy] == False:
return False
else:
return True
def boardReset(mainFont, bot1, player,usedBoxes, mainBoard, playerWins, computerWins, bot = None):
pygame.time.wait(1000)
usedBoxes = makeEachBoxFalse(False)
mainBoard = makeEachBoxFalse(False)
print "#######################################################################"
print usedBoxes
print mainBoard
print "#######################################################################"
player = XMARK
if (bot != None):
for i in range(len(bot.initial_field)):
if (bot.initial_field[i] > 0):
x, y = __1D_to_2D(i)
usedBoxes[x][y] = True
if(int(bot.initial_field[i]) == 1):
mainBoard[x][y] = XMARK
elif(int(bot.initial_field[i]) == 2):
mainBoard[x][y] = OMARK
markBox(mainBoard[x][y], x, y, mainFont)
if player == XMARK:
player = OMARK
else: player = XMARK
print usedBoxes
print mainBoard
print "-----------------------------------------------------------------------"
playerWins = computerWins = False
print usedBoxes
print mainBoard
print "#######################################################################"
return player,usedBoxes, mainBoard, playerWins, computerWins
"""
Converts the x,y-position in a 2D-array to the position in a corresponding 1D-array
"""
def __1D_to_2D (pos):
x = pos % 3
y = int(pos) / 3
return x, y
def highlightWin(mainBoard):
scenario1 = 1
scenario2 = 2
scenario3 = 3
scenario4 = 4
scenario5 = 5
scenario6 = 6
scenario7 = 7
scenario8 = 8
if mainBoard[0][0] == mainBoard[1][0] == mainBoard[2][0] != False:
highLightBoxes(mainBoard, scenario1)
elif mainBoard[0][1] == mainBoard[1][1] == mainBoard[2][1] != False:
highLightBoxes(mainBoard, scenario2)
elif mainBoard[0][2] == mainBoard[1][2] == mainBoard[2][2] != False:
highLightBoxes(mainBoard, scenario3)
elif mainBoard[0][0] == mainBoard[0][1] == mainBoard[0][2] != False:
highLightBoxes(mainBoard, scenario4)
elif mainBoard[1][0] == mainBoard[1][1] == mainBoard[1][2] != False:
highLightBoxes(mainBoard, scenario5)
elif mainBoard[2][0] == mainBoard[2][1] == mainBoard[2][2] != False:
highLightBoxes(mainBoard, scenario6)
elif mainBoard[0][0] == mainBoard[1][1] == mainBoard[2][2] != False:
highLightBoxes(mainBoard, scenario7)
elif mainBoard[2][0] == mainBoard[1][1] == mainBoard[0][2] != False:
highLightBoxes(mainBoard, scenario8)
def highLightBoxes(mainBoard, scenario):
if scenario == 1:
startPos = (XMARGIN + (BOXSIZE/2), YMARGIN + (BOXSIZE/2))
endPos = (XMARGIN + BOXSIZE + GAPSIZE + BOXSIZE + GAPSIZE + (BOXSIZE/2), YMARGIN + (BOXSIZE/2))
pygame.draw.line(DISPLAYSURF, LINECOLOR, startPos, endPos, 10)
elif scenario == 2:
startPos = (XMARGIN + (BOXSIZE/2), YMARGIN + BOXSIZE + GAPSIZE + (BOXSIZE/2))
endPos = (XMARGIN + (BOXSIZE/2) + BOXSIZE + GAPSIZE + BOXSIZE + GAPSIZE, YMARGIN + BOXSIZE + GAPSIZE + (BOXSIZE/2))
pygame.draw.line(DISPLAYSURF, LINECOLOR, startPos, endPos, 10)
elif scenario == 3:
startPos = (XMARGIN + (BOXSIZE/2), YMARGIN + BOXSIZE + GAPSIZE + BOXSIZE + GAPSIZE + (BOXSIZE/2))
endPos = (XMARGIN + (BOXSIZE/2) + BOXSIZE + GAPSIZE + BOXSIZE + GAPSIZE, YMARGIN + BOXSIZE + GAPSIZE + BOXSIZE + GAPSIZE + (BOXSIZE/2))
pygame.draw.line(DISPLAYSURF, LINECOLOR, startPos, endPos, 10)
elif scenario == 4:
startPos = (XMARGIN + (BOXSIZE/2), YMARGIN + (BOXSIZE/2))
endPos = (XMARGIN + (BOXSIZE/2), YMARGIN + BOXSIZE + GAPSIZE + BOXSIZE + GAPSIZE + (BOXSIZE/2))
pygame.draw.line(DISPLAYSURF, LINECOLOR, startPos, endPos, 10)
elif scenario == 5:
startPos = (XMARGIN + BOXSIZE + GAPSIZE + (BOXSIZE/2), YMARGIN + (BOXSIZE/2))
endPos = (XMARGIN + BOXSIZE + GAPSIZE + (BOXSIZE/2), YMARGIN + BOXSIZE + GAPSIZE + BOXSIZE + GAPSIZE + (BOXSIZE/2))
pygame.draw.line(DISPLAYSURF, LINECOLOR, startPos, endPos, 10)
elif scenario == 6:
startPos = (XMARGIN + BOXSIZE + GAPSIZE + BOXSIZE + GAPSIZE + (BOXSIZE/2), YMARGIN + (BOXSIZE/2))
endPos = (XMARGIN + BOXSIZE + GAPSIZE + BOXSIZE + GAPSIZE + (BOXSIZE/2), YMARGIN + BOXSIZE + GAPSIZE + BOXSIZE + GAPSIZE + (BOXSIZE/2))
pygame.draw.line(DISPLAYSURF, LINECOLOR, startPos, endPos, 10)
elif scenario == 7:
startPos = (XMARGIN + (BOXSIZE/2), YMARGIN + (BOXSIZE/2))
endPos = (XMARGIN + BOXSIZE + GAPSIZE + BOXSIZE + GAPSIZE + (BOXSIZE/2), YMARGIN + BOXSIZE + GAPSIZE + BOXSIZE + GAPSIZE + (BOXSIZE/2))
pygame.draw.line(DISPLAYSURF, LINECOLOR, startPos, endPos, 10)
elif scenario == 8:
startPos = (XMARGIN + BOXSIZE + GAPSIZE + BOXSIZE + GAPSIZE + (BOXSIZE/2), YMARGIN + (BOXSIZE/2))
endPos = (XMARGIN + (BOXSIZE/2), YMARGIN + BOXSIZE + GAPSIZE + BOXSIZE + GAPSIZE + (BOXSIZE/2))
pygame.draw.line(DISPLAYSURF, LINECOLOR, startPos, endPos, 10)
if __name__ == '__main__':
main()