-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXOH.py
514 lines (447 loc) · 16.6 KB
/
XOH.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# In the name of god #
# The XO game : hard #
# Author : Pooya.Sh.K #
import pygame, sys, time, random
from pygame.locals import *
pygame.init ()
# VARIABLES
WINDOWWIDTH = 640 # the width of the games window
WINDOWHEIGHT = 690 # the height of the games window
LINELENGHT = 540 # lenght of the board lines
SIDEGAP = 50 # the gaps between the window and the board
LINEWIDTH = 3 # the width of the board line in pixels
LINEGAPS = 178 # the gaps between lines
XSCORES = 0 # the games X won
OSCORES = 0 # the games O won
DSCORES = 0 # the draw games
FONT = pygame.font.Font('freesansbold.ttf', 40) # the font object
# lines coordinates
LINECORS_START_POS = ((228, 50 ), (409, 50 ), (50 , 228), (50 , 409))
LINECORS_END_POS = ((228, 590), (409, 590), (590, 228), (590, 409))
# R G B
WHITE = (255, 255, 255)
BLACK = ( 0 , 0 , 0 )
RED = (255, 0 , 0 )
GREEN = ( 0 , 255, 0 )
BLUE = ( 0 , 0 , 255)
CYAN = ( 0 , 255, 255)
MAGENTA = (255, 0 , 255)
YELLOW = (255, 255, 0 )
ORANGE = (128, 128, 0 )
BGCOLOR = GREEN
LINECOLOR = MAGENTA
XCOLOR = RED
OCOLOR = BLUE
POINT_FG_COLOR = YELLOW
POINT_BG_COLOR = CYAN
X = "X"
O = "O"
E = "EMPTY"
# box features
# x , y ,(XOE)
BOX1 = [(139, 139), E]
BOX2 = [(320, 139), E]
BOX3 = [(501, 139), E]
BOX4 = [(139, 320), E]
BOX5 = [(320, 320), E]
BOX6 = [(501, 320), E]
BOX7 = [(139, 501), E]
BOX8 = [(320, 501), E]
BOX9 = [(501, 501), E]
ALLBOXES = (BOX1, BOX2, BOX3,
BOX4, BOX5, BOX6,
BOX7, BOX8, BOX9)
CORNER_BOXES = (BOX1, BOX3, BOX9, BOX7)
SIDE_BOXES = (BOX2, BOX4, BOX8, BOX6)
ALLROWS = ((BOX1, BOX2, BOX3),
(BOX4, BOX5, BOX6),
(BOX7, BOX8, BOX9),
(BOX1, BOX4, BOX7),
(BOX2, BOX5, BOX8),
(BOX3, BOX6, BOX9),
(BOX1, BOX5, BOX9),
(BOX3, BOX5, BOX7))
ROW_MAKERS = ((BOX1, BOX3, BOX9),
(BOX3, BOX9, BOX7),
(BOX9, BOX7, BOX1),
(BOX7, BOX1, BOX3))
class base:
""" class for making the bases """
def make_window ():
""" makes a window """
DISPLAYSURF = pygame.display.set_mode ((WINDOWWIDTH, WINDOWHEIGHT))
DISPLAYSURF.fill (BGCOLOR)
pygame.display.update ()
return DISPLAYSURF
def draw_board ():
""" draws a (#) shaped board """
for i in range (4):
pygame.draw.line (DISPLAYSURF, LINECOLOR, LINECORS_START_POS[i],
LINECORS_END_POS [i],LINEWIDTH)
pygame.display.update ()
def reset ():
""" resets the screen """
DISPLAYSURF.fill (GREEN)
base.draw_board ()
for box in ALLBOXES:
box [1] = E
pygame.display.update ()
class check_all_rows:
""" class for checking all rows """
def winner ():
""" checks if any row is full of X or O & ++ the players score"""
for row in ALLROWS:
a = row[0][1]
b = row[1][1]
c = row[2][1]
if a == b == c != E:
if a == b == c == X:
pygame.draw.line (DISPLAYSURF, ORANGE, row[0][0], row[2][0], LINEWIDTH+5)
pygame.display.update ()
draw.winner (X)
return X
if a == b == c == O:
pygame.draw.line (DISPLAYSURF, ORANGE, row[0][0], row[2][0], LINEWIDTH+5)
pygame.display.update ()
draw.winner (O)
return O
return None
def defence_or_end ():
""" checks if any row is going to be full of X|O """
rows = list ()
for row in ALLROWS:
a = row [0][1]
b = row [1][1]
c = row [2][1]
if a == b != c == E:
rows.append (row)
if a == c != b == E:
rows.append (row)
if b == c != a == E:
rows.append (row)
if len (rows) == 0:
return None
elif len (rows) == 1:
row = rows[0]
a = row [0][1]
b = row [1][1]
c = row [2][1]
if a == b != c == E:
return row [2]
elif a == c != b == E:
return row [1]
elif b == c != a == E:
return row [0]
elif len (rows) >= 2:
for row in rows:
a = row[0][1]
b = row[1][1]
c = row[2][1]
if a == b == X != c == E:
return row [2]
elif a == c == X != b == E:
return row [1]
elif b == c == X != a == E:
return row [0]
return None
def rowmaker ():
""" cheks if there is any row maker that we could full it """
for row in ROW_MAKERS:
a = row[0][1]
b = row[1][1]
c = row[2][1]
if a == b != c == E:
return row [2]
elif a == c != b == E:
return row [1]
elif b == c != a == E:
return row [0]
return None
class draw :
""" class for drawing X or O in a BOX """
def X (box):
""" draws X in the givven box """
x = box[0][0]
y = box[0][1]
rlline = ((x+70, y-70), (x-70, y+70))
lrline = ((x-70, y-70), (x+70, y+70))
pygame.draw.line (DISPLAYSURF, XCOLOR, rlline[0], rlline[1], LINEWIDTH)
pygame.draw.line (DISPLAYSURF, XCOLOR, lrline[0], lrline[1], LINEWIDTH)
pygame.display.update ()
return
def O (box):
""" draws O in the givven box """
x = box[0][0]
y = box[0][1]
pygame.draw.circle (DISPLAYSURF, OCOLOR, (x, y), 70, 10)
pygame.display.update ()
return
def scores ():
""" draws the players scores """
printxscores = FONT.render("X:"+str(XSCORES), True, RED , CYAN )
printoscores = FONT.render("O:"+str(OSCORES), True, BLUE , YELLOW )
printdscores = FONT.render("D:"+str(DSCORES), True, GREEN , MAGENTA )
xrect = printxscores.get_rect ()
orect = printoscores.get_rect ()
drect = printdscores.get_rect ()
xrect.center = (100, 640)
orect.center = (540, 640)
drect.center = (330, 640)
DISPLAYSURF.blit (printxscores, xrect)
DISPLAYSURF.blit (printoscores, orect)
DISPLAYSURF.blit (printdscores, drect)
def winner (w):
if w == X:
fon = FONT.render("Sorry but 'I' scored! :P", True, BLUE, YELLOW)
if w == O:
fon = FONT.render("congrats! 'you' could score!! :O", True, BLUE, YELLOW)
if w == E:
fon = FONT.render("this game was 'draw'. :|", True, BLUE, YELLOW)
rec = fon.get_rect ()
rec.center = (320, 345)
DISPLAYSURF.blit (fon, rec)
pygame.display.update ()
time.sleep (1)
return None
class X_start:
""" the first three moves when X starts """
def first_move ():
""" the best choice in the firs move is one of the corner boxes """
return CORNER_BOXES [random.randint (0, 3)]
def sec_move (first_move):
""" returns the box in the opposite corner of the board """
try:
f = first_move
p = None
for i in range (4):
if CORNER_BOXES [i] == f:
p = i
return CORNER_BOXES [(p+2)%4]
except TypeError:
raise RuntimeError ("somthing bad happened") from None
def second_move (first_move):
""" if the next box to the first move is full the best move is another cornerbox"""
if X_start.sec_move (first_move)[1] == E:
return X_start.sec_move (first_move)
elif X_start.sec_move (first_move)[1] != E:
for i in range (4):
if first_move == CORNER_BOXES[i]:
return CORNER_BOXES [(i+1)%4]
def random_move ():
""" if there is no possible boxes to win we
just make a random decision to continue the game """
for box in ALLBOXES :
if box [1] == E:
return box
class O_start :
""" the first moves when O starts """
def first_move ():
""" the best move is BOX5, if it is full one of the corner boxes is the best"""
if BOX5 [1] == E:
return BOX5
elif BOX5 [1] != E:
return CORNER_BOXES [random.randint (0, 3)]
def second_move (xfirst_move):
xf = xfirst_move
if xf == BOX5:
boxes = list ()
for box in ALLBOXES:
if box [1] == E:
boxes.append (box)
return boxes [random.randint(0, len(boxes)-1)]
elif xf != BOX5:
if X_start.sec_move (xf)[1] == E:
return check_all_rows.defence_or_end ()
elif X_start.sec_move (xf)[1] != E:
for box in CORNER_BOXES:
if box[1] == E:
return box
return None
def random_move ():
""" if there is no possible boxes to win, we
just make a random decision to continue the game """
for box in ALLBOXES:
if box [1] == E:
return box
def take_players_choice (mousex, mousey):
""" if the event is mouse click, returns the clicked box """
x = mousex
y = mousey
for box in ALLBOXES:
bx = range(box[0][0]-89, box[0][0]+90)
by = range(box[0][1]-89, box[0][1]+90)
if (x in bx) and (y in by):
return box
return None
def main (): # the main game function #
global XSCORES, OSCORES, DSCORES, DISPLAYSURF
DISPLAYSURF = base.make_window ()
base.draw_board ()
XSTART = True
OSTART = False
runing = True
TURN = 0
while runing: # the main game loop
if XSTART == True and OSTART == False:
for e in pygame.event.get ():
if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE):
runing = False
pygame.quit ()
sys.exit ()
if e.type == MOUSEBUTTONUP and TURN % 2 == 1:
box = take_players_choice (pygame.mouse.get_pos()[0],
pygame.mouse.get_pos()[1])
if box in ALLBOXES:
if box [1] == E:
box [1] = O
draw.O (box)
TURN += 1
if TURN == 9 or check_all_rows.winner ()!=None:
if check_all_rows.winner () == X:
XSCORES += 1
XSTART, OSTART = True, False
base.reset ()
TURN = 0
continue
elif check_all_rows.winner () == O:
OSCORES += 1
XSTART, OSTART = False, True
base.reset ()
TURN = 0
continue
elif TURN == 9:
draw.winner (E)
DSCORES += 1
XSTART, OSTART = OSTART, XSTART
base.reset ()
TURN = 0
continue
if TURN % 2 == 0:
box = None
if TURN == 0:
box = X_start.first_move ()
FIRST_MOVE = box
if TURN == 2:
box = X_start.second_move (FIRST_MOVE)
if check_all_rows.defence_or_end()!=None:
box = check_all_rows.defence_or_end ()
if box == None :
box = X_start.random_move ()
if box in ALLBOXES:
box [1] = X
draw.X (box)
TURN += 1
if TURN == 9 or check_all_rows.winner ()!=None:
if check_all_rows.winner () == X:
XSCORES += 1
XSTART, OSTART = True, False
base.reset ()
TURN = 0
continue
elif check_all_rows.winner () == O:
OSCORES += 1
XSTART, OSTART = False, True
base.reset ()
TURN = 0
continue
elif TURN == 9:
draw.winner (E)
DSCORES += 1
XSTART, OSTART = OSTART, XSTART
base.reset ()
TURN = 0
continue
if XSTART == False and OSTART == True:
for e in pygame.event.get ():
if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE):
runing = False
pygame.quit ()
sys.exit ()
if e.type == MOUSEBUTTONUP and TURN % 2 == 0:
box = take_players_choice (pygame.mouse.get_pos()[0],
pygame.mouse.get_pos()[1])
if box in ALLBOXES:
if box [1] == E:
box [1] = O
draw.O (box)
TURN += 1
if TURN == 9 or check_all_rows.winner ()!=None:
if check_all_rows.winner () == X:
XSCORES += 1
XSTART, OSTART = True, False
base.reset ()
TURN = 0
continue
elif check_all_rows.winner () == O:
OSCORES += 1
XSTART, OSTART = False, True
base.reset ()
TURN = 0
continue
elif TURN == 9:
draw.winner (E)
DSCORES += 1
XSTART, OSTART = OSTART, XSTART
base.reset ()
TURN = 0
continue
if TURN % 2 == 1:
box = None
if TURN == 1:
box = O_start.first_move ()
X_FIRST_MOVE = box
if TURN == 3:
box = O_start.second_move (X_FIRST_MOVE)
if check_all_rows.defence_or_end () != None:
box = check_all_rows.defence_or_end ()
if box == None:
box = O_start.random_move ()
if box in ALLBOXES:
if box[1] == E:
box [1] = X
draw.X (box)
TURN += 1
if TURN == 9 or check_all_rows.winner ()!=None:
if check_all_rows.winner () == X:
XSCORES += 1
XSTART, OSTART = True, False
base.reset ()
TURN = 0
continue
elif check_all_rows.winner () == O:
OSCORES += 1
XSTART, OSTART = False, True
base.reset ()
TURN = 0
continue
elif TURN == 9:
draw.winner (E)
DSCORES += 1
XSTART, OSTART = OSTART, XSTART
base.reset ()
TURN = 0
continue
if TURN == 9 or check_all_rows.winner ()!=None:
if check_all_rows.winner () == X:
XSCORES += 1
XSTART, OSTART = True, False
TURN = 0
base.reset ()
continue
elif check_all_rows.winner () == O:
OSCORES += 1
XSTART, OSTART = False, True
TURN = 0
base.reset ()
continue
elif TURN == 9:
draw.winner (E)
DSCORES += 1
XSTART, OSTART = OSTART, XSTART
TURN = 0
base.reset ()
continue
draw.scores ()
pygame.display.update ()
if __name__ == "__main__":
main ()