-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathothelo.py
711 lines (593 loc) · 25.5 KB
/
othelo.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
from graphics import *
import copy
# grid settings
rows = 8
cols = 8
total_cells = rows * cols
moves_made = 4
wWidth = 800
wHeight = 800
cellWidth = wWidth // rows
cellHeight = wHeight // cols
EMPTY = -1
BLACK = 1
WHITE = 0
# n-ary Tree Node
class TreeNode:
def __init__(self, x):
self.val = x
self.parent = None
self.children = []
def add_child(self, child):
child_node = TreeNode(child)
child_node.parent = self
self.children.append(child_node)
def add_children(self, children):
children_nodes = [TreeNode(x) for x in children]
for j in children_nodes:
j.parent = self
self.children = children_nodes
class State:
def __init__(self, local_grid, blacks, whites, is_black, move):
self.grid = local_grid
self.black_cords = blacks
self.white_cords = whites
self.black_score = len(blacks)
self.white_score = len(whites)
self.black_turn = is_black
self.move = move
self.valid_moves = generate_valid_moves(self)
def generate_valid_moves(state: State):
# extract info from current state
grid = state.grid
row = len(grid)
col = len(grid[0])
black = state.black_turn
valid_moves = set()
if black:
for r, c in state.white_cords:
# check vertical boundaries
if r - 1 > -1 and r + 1 < row:
# check vertical valid moves
if grid[r - 1][c] == EMPTY:
start_ptr = r + 1
while start_ptr < row - 1 and grid[start_ptr][c] == WHITE:
start_ptr += 1
if grid[start_ptr][c] == BLACK:
valid_moves.add((r - 1, c))
if grid[r + 1][c] == EMPTY:
start_ptr = r - 1
while start_ptr > 0 and grid[start_ptr][c] == WHITE:
start_ptr -= 1
if grid[start_ptr][c] == BLACK:
valid_moves.add((r + 1, c))
# check horizontal boundaries
if c - 1 > -1 and c + 1 < col:
# check horizontal valid moves
if grid[r][c - 1] == EMPTY:
start_ptr = c + 1
while start_ptr < col - 1 and grid[r][start_ptr] == WHITE:
start_ptr += 1
if grid[r][start_ptr] == BLACK:
valid_moves.add((r, c - 1))
if grid[r][c + 1] == EMPTY:
start_ptr = c - 1
while start_ptr > 0 and grid[r][start_ptr] == WHITE:
start_ptr -= 1
if grid[r][start_ptr] == BLACK:
valid_moves.add((r, c + 1))
# check diagonal boundaries
if r - 1 > -1 and r + 1 < row and c - 1 > -1 and c + 1 < col:
# check primary diagonal valid moves
if grid[r - 1][c - 1] == EMPTY:
start_r = r + 1
start_c = c + 1
while start_r < row - 1 and start_c < col - 1 and grid[start_r][start_c] == WHITE:
start_r += 1
start_c += 1
if grid[start_r][start_c] == BLACK:
valid_moves.add((r - 1, c - 1))
if grid[r + 1][c + 1] == EMPTY:
start_r = r - 1
start_c = c - 1
while start_r > 0 and start_c > 0 and grid[start_r][start_c] == WHITE:
start_r -= 1
start_c -= 1
if grid[start_r][start_c] == BLACK:
valid_moves.add((r + 1, c + 1))
# check secondary diagonal valid moves
if grid[r - 1][c + 1] == EMPTY:
start_r = r + 1
start_c = c - 1
while start_r < row - 1 and start_c > 0 and grid[start_r][start_c] == WHITE:
start_r += 1
start_c -= 1
if grid[start_r][start_c] == BLACK:
valid_moves.add((r - 1, c + 1))
if grid[r + 1][c - 1] == EMPTY:
start_r = r - 1
start_c = c + 1
while start_r > 0 and start_c < col - 1 and grid[start_r][start_c] == WHITE:
start_r -= 1
start_c += 1
if grid[start_r][start_c] == BLACK:
valid_moves.add((r + 1, c - 1))
# in case white player's turn
else:
for r, c in state.black_cords:
# check vertical boundaries
if r - 1 > -1 and r + 1 < row:
# check vertical valid moves
if grid[r - 1][c] == EMPTY:
start_ptr = r + 1
while start_ptr < row - 1 and grid[start_ptr][c] == BLACK:
start_ptr += 1
if grid[start_ptr][c] == WHITE:
valid_moves.add((r - 1, c))
if grid[r + 1][c] == EMPTY:
start_ptr = r - 1
while start_ptr > 0 and grid[start_ptr][c] == BLACK:
start_ptr -= 1
if grid[start_ptr][c] == WHITE:
valid_moves.add((r + 1, c))
# check horizontal boundaries
if c - 1 > -1 and c + 1 < col:
# check horizontal valid moves
if grid[r][c - 1] == EMPTY:
start_ptr = c + 1
while start_ptr < col - 1 and grid[r][start_ptr] == BLACK:
start_ptr += 1
if grid[r][start_ptr] == WHITE:
valid_moves.add((r, c - 1))
if grid[r][c + 1] == EMPTY:
start_ptr = c - 1
while start_ptr > 0 and grid[r][start_ptr] == BLACK:
start_ptr -= 1
if grid[r][start_ptr] == WHITE:
valid_moves.add((r, c + 1))
# check diagonal boundaries
if r - 1 > -1 and r + 1 < row and c - 1 > -1 and c + 1 < col:
# check primary diagonal valid moves
if grid[r - 1][c - 1] == EMPTY:
start_r = r + 1
start_c = c + 1
while start_r < row - 1 and start_c < col - 1 and grid[start_r][start_c] == BLACK:
start_r += 1
start_c += 1
if grid[start_r][start_c] == WHITE:
valid_moves.add((r - 1, c - 1))
if grid[r + 1][c + 1] == EMPTY:
start_r = r - 1
start_c = c - 1
while start_r > 0 and start_c > 0 and grid[start_r][start_c] == BLACK:
start_r -= 1
start_c -= 1
if grid[start_r][start_c] == WHITE:
valid_moves.add((r + 1, c + 1))
# check secondary diagonal valid moves
if grid[r - 1][c + 1] == EMPTY:
start_r = r + 1
start_c = c - 1
while start_r < row - 1 and start_c > 0 and grid[start_r][start_c] == BLACK:
start_r += 1
start_c -= 1
if grid[start_r][start_c] == WHITE:
valid_moves.add((r - 1, c + 1))
if grid[r + 1][c - 1] == EMPTY:
start_r = r - 1
start_c = c + 1
while start_r > 0 and start_c < col - 1 and grid[start_r][start_c] == BLACK:
start_r -= 1
start_c += 1
if grid[start_r][start_c] == WHITE:
valid_moves.add((r + 1, c - 1))
return valid_moves
def generate_child(curr_state: State, move):
# extract info from current state
grid = curr_state.grid
local_grid = copy.deepcopy(grid)
row = len(grid)
col = len(grid[0])
black_balls = copy.deepcopy(curr_state.black_cords)
white_balls = copy.deepcopy(curr_state.white_cords)
black = curr_state.black_turn
[r, c] = move
if black:
local_grid[r][c] = BLACK
black_balls.append((r, c))
# check up boundary
if r - 1 > -1:
# check above for white
if grid[r - 1][c] == WHITE:
start_ptr = r - 1
while start_ptr > 0 and grid[start_ptr][c] == WHITE:
start_ptr -= 1
if grid[start_ptr][c] == BLACK:
end_ptr = start_ptr
start_ptr = r - 1
while start_ptr != end_ptr:
local_grid[start_ptr][c] = BLACK
white_balls.remove((start_ptr, c))
black_balls.append((start_ptr, c))
start_ptr -= 1
# check down boundary
if r + 1 < row:
# check below for white
if grid[r + 1][c] == WHITE:
start_ptr = r + 1
while start_ptr < row - 1 and grid[start_ptr][c] == WHITE:
start_ptr += 1
if grid[start_ptr][c] == BLACK:
end_ptr = start_ptr
start_ptr = r + 1
while start_ptr != end_ptr:
local_grid[start_ptr][c] = BLACK
white_balls.remove((start_ptr, c))
black_balls.append((start_ptr, c))
start_ptr += 1
# check left boundary
if c - 1 > -1:
# check left for white
if grid[r][c - 1] == WHITE:
start_ptr = c - 1
while start_ptr > 0 and grid[r][start_ptr] == WHITE:
start_ptr -= 1
if grid[r][start_ptr] == BLACK:
end_ptr = start_ptr
start_ptr = c - 1
while start_ptr != end_ptr:
local_grid[r][start_ptr] = BLACK
white_balls.remove((r, start_ptr))
black_balls.append((r, start_ptr))
start_ptr -= 1
# check right boundary
if c + 1 < col:
# check right for white
if grid[r][c + 1] == WHITE:
start_ptr = c + 1
while start_ptr < col - 1 and grid[r][start_ptr] == WHITE:
start_ptr += 1
if grid[r][start_ptr] == BLACK:
end_ptr = start_ptr
start_ptr = c + 1
while start_ptr != end_ptr:
local_grid[r][start_ptr] = BLACK
white_balls.remove((r, start_ptr))
black_balls.append((r, start_ptr))
start_ptr += 1
# check upper left boundary
if r - 1 > -1 and c - 1 > -1:
# check upper left for white
if grid[r - 1][c - 1] == WHITE:
start_r = r - 1
start_c = c - 1
while start_r > 0 and start_c > 0 and grid[start_r][start_c] == WHITE:
start_r -= 1
start_c -= 1
if grid[start_r][start_c] == BLACK:
end_r = start_r
end_c = start_c
start_r = r - 1
start_c = c - 1
while start_r != end_r and start_c != end_c:
local_grid[start_r][start_c] = BLACK
white_balls.remove((start_r, start_c))
black_balls.append((start_r, start_c))
start_r -= 1
start_c -= 1
# check lower right boundary
if r + 1 < row and c + 1 < col:
# check lower right for white
if grid[r + 1][c + 1] == WHITE:
start_r = r + 1
start_c = c + 1
while start_r < row - 1 and start_c < col - 1 and grid[start_r][start_c] == WHITE:
start_r += 1
start_c += 1
if grid[start_r][start_c] == BLACK:
end_r = start_r
end_c = start_c
start_r = r + 1
start_c = c + 1
while start_r != end_r and start_c != end_c:
local_grid[start_r][start_c] = BLACK
white_balls.remove((start_r, start_c))
black_balls.append((start_r, start_c))
start_r += 1
start_c += 1
# check upper right boundary
if r - 1 > -1 and c + 1 < col:
# check upper right for white
if grid[r - 1][c + 1] == WHITE:
start_r = r - 1
start_c = c + 1
while start_r > 0 and start_c < col - 1 and grid[start_r][start_c] == WHITE:
start_r -= 1
start_c += 1
if grid[start_r][start_c] == BLACK:
end_r = start_r
end_c = start_c
start_r = r - 1
start_c = c + 1
while start_r != end_r and start_c != end_c:
local_grid[start_r][start_c] = BLACK
white_balls.remove((start_r, start_c))
black_balls.append((start_r, start_c))
start_r -= 1
start_c += 1
# check lower left boundary
if r + 1 < row and c - 1 > - 1:
# check lower left for white
if grid[r + 1][c - 1] == WHITE:
start_r = r + 1
start_c = c - 1
while start_r < row - 1 and start_c > 0 and grid[start_r][start_c] == WHITE:
start_r += 1
start_c -= 1
if grid[start_r][start_c] == BLACK:
end_r = start_r
end_c = start_c
start_r = r + 1
start_c = c - 1
while start_r != end_r and start_c != end_c:
local_grid[start_r][start_c] = BLACK
white_balls.remove((start_r, start_c))
black_balls.append((start_r, start_c))
start_r += 1
start_c -= 1
# in case its white's turn
else:
local_grid[r][c] = WHITE
white_balls.append((r, c))
# check up boundary
if r - 1 > -1:
# check above for black
if grid[r - 1][c] == BLACK:
start_ptr = r - 1
while start_ptr > 0 and grid[start_ptr][c] == BLACK:
start_ptr -= 1
if grid[start_ptr][c] == WHITE:
end_ptr = start_ptr
start_ptr = r - 1
while start_ptr != end_ptr:
local_grid[start_ptr][c] = WHITE
black_balls.remove((start_ptr, c))
white_balls.append((start_ptr, c))
start_ptr -= 1
# check down boundary
if r + 1 < row:
# check below for black
if grid[r + 1][c] == BLACK:
start_ptr = r + 1
while start_ptr < row - 1 and grid[start_ptr][c] == BLACK:
start_ptr += 1
if grid[start_ptr][c] == WHITE:
end_ptr = start_ptr
start_ptr = r + 1
while start_ptr != end_ptr:
local_grid[start_ptr][c] = WHITE
black_balls.remove((start_ptr, c))
white_balls.append((start_ptr, c))
start_ptr += 1
# check left boundary
if c - 1 > -1:
# check left for black
if grid[r][c - 1] == BLACK:
start_ptr = c - 1
while start_ptr > 0 and grid[r][start_ptr] == BLACK:
start_ptr -= 1
if grid[r][start_ptr] == WHITE:
end_ptr = start_ptr
start_ptr = c - 1
while start_ptr != end_ptr:
local_grid[r][start_ptr] = WHITE
black_balls.remove((r, start_ptr))
white_balls.append((r, start_ptr))
start_ptr -= 1
# check right boundary
if c + 1 < col:
# check right for black
if grid[r][c + 1] == BLACK:
start_ptr = c + 1
while start_ptr < col - 1 and grid[r][start_ptr] == BLACK:
start_ptr += 1
if grid[r][start_ptr] == WHITE:
end_ptr = start_ptr
start_ptr = c + 1
while start_ptr != end_ptr:
local_grid[r][start_ptr] = WHITE
black_balls.remove((r, start_ptr))
white_balls.append((r, start_ptr))
start_ptr += 1
# check upper left boundary
if r - 1 > -1 and c - 1 > -1:
# check upper left for black
if grid[r - 1][c - 1] == BLACK:
start_r = r - 1
start_c = c - 1
while start_r > 0 and start_c > 0 and grid[start_r][start_c] == BLACK:
start_r -= 1
start_c -= 1
if grid[start_r][start_c] == WHITE:
end_r = start_r
end_c = start_c
start_r = r - 1
start_c = c - 1
while start_r != end_r and start_c != end_c:
local_grid[start_r][start_c] = WHITE
black_balls.remove((start_r, start_c))
white_balls.append((start_r, start_c))
start_r -= 1
start_c -= 1
# check lower right boundary
if r + 1 < row and c + 1 < col:
# check lower right for black
if grid[r + 1][c + 1] == BLACK:
start_r = r + 1
start_c = c + 1
while start_r < row - 1 and start_c < col - 1 and grid[start_r][start_c] == BLACK:
start_r += 1
start_c += 1
if grid[start_r][start_c] == WHITE:
end_r = start_r
end_c = start_c
start_r = r + 1
start_c = c + 1
while start_r != end_r and start_c != end_c:
local_grid[start_r][start_c] = WHITE
black_balls.remove((start_r, start_c))
white_balls.append((start_r, start_c))
start_r += 1
start_c += 1
# check upper right boundary
if r - 1 > -1 and c + 1 < col:
# check upper right for black
if grid[r - 1][c + 1] == BLACK:
start_r = r - 1
start_c = c + 1
while start_r > 0 and start_c < col - 1 and grid[start_r][start_c] == BLACK:
start_r -= 1
start_c += 1
if grid[start_r][start_c] == WHITE:
end_r = start_r
end_c = start_c
start_r = r - 1
start_c = c + 1
while start_r != end_r and start_c != end_c:
local_grid[start_r][start_c] = WHITE
black_balls.remove((start_r, start_c))
white_balls.append((start_r, start_c))
start_r -= 1
start_c += 1
# check lower left boundary
if r + 1 < row and c - 1 > - 1:
# check lower left for black
if grid[r + 1][c - 1] == BLACK:
start_r = r + 1
start_c = c - 1
while start_r < row - 1 and start_c > 0 and grid[start_r][start_c] == BLACK:
start_r += 1
start_c -= 1
if grid[start_r][start_c] == WHITE:
end_r = start_r
end_c = start_c
start_r = r + 1
start_c = c - 1
while start_r != end_r and start_c != end_c:
local_grid[start_r][start_c] = WHITE
black_balls.remove((start_r, start_c))
white_balls.append((start_r, start_c))
start_r += 1
start_c -= 1
child_state: State = State(local_grid, black_balls, white_balls, not black, (r, c))
return child_state
def generate_successors(parent: TreeNode):
curr_state: State = parent.val
valid_moves = generate_valid_moves(curr_state)
children = [generate_child(curr_state, (r, c)) for r, c in valid_moves]
parent.add_children(children)
def evaluation(curr_state: State):
return curr_state.black_score - curr_state.white_score
def mini_max(root: TreeNode, depth, alpha, beta, maximizing_player):
if depth == 0:
return evaluation(root.val), root.val
generate_successors(root)
if maximizing_player:
max_eval = float('-inf')
next_move_state = None
for child in root.children:
[curr_eval, _] = mini_max(child, depth - 1, alpha, beta, not maximizing_player)
if curr_eval > max_eval:
max_eval = curr_eval
next_move_state = child.val
alpha = max(alpha, curr_eval)
if beta <= alpha:
break
return max_eval, next_move_state
else:
min_eval = float('inf')
next_move_state = None
for child in root.children:
[curr_eval, _] = mini_max(child, depth - 1, alpha, beta, not maximizing_player)
if curr_eval < min_eval:
min_eval = curr_eval
next_move_state = child.val
beta = min(beta, curr_eval)
if beta <= alpha:
break
return min_eval, next_move_state
def show_hints(valid_moves, circles):
for point in valid_moves:
circles[point[1]][point[0]].setFill(color_rgb(100, 0, 0))
def remove_hints(valid_moves, circles):
for point in valid_moves:
circles[point[1]][point[0]].setFill(color_rgb(255, 255, 255))
def move(grid, r, c, player):
grid[r][c] = player
return grid
def updateGraphics(grid, circles):
for idx, col in enumerate(grid):
for jdx, cell in enumerate(col):
if cell == 1:
circles[jdx][idx].setFill(color_rgb(0, 0, 0))
elif cell == 0:
circles[jdx][idx].setFill(color_rgb(100, 100, 100))
update()
def mouseToGrid(mouse_click):
return int(mouse_click.getY() / cellWidth), int(mouse_click.getX() / cellWidth)
def main():
# setup initial grid
global moves_made
win = GraphWin("w", wWidth, wHeight)
win.setBackground('white')
for i in range(cols):
Line(Point(i * cellWidth, 0), Point(i * cellWidth, wHeight)).draw(win)
Line(Point(0, i * cellHeight), Point(wHeight, i * cellHeight)).draw(win)
grid = [[EMPTY for i in range(cols)] for j in range(rows)]
grid[(rows // 2) - 1][(cols // 2) - 1] = 1
grid[(rows // 2)][(cols // 2)] = 1
grid[(rows // 2)][(cols // 2) - 1] = 0
grid[(rows // 2) - 1][(cols // 2)] = 0
circles = []
for i in range(cols):
circles.append([])
for j in range(rows):
mid_point = Point((cellWidth / 2) + cellWidth * i, (cellHeight / 2) + cellHeight * j)
new_circle = Circle(mid_point, cellHeight * 0.8 / 2)
new_circle.draw(win)
circles[i].append(new_circle)
updateGraphics(grid, circles)
prev_grid = grid
blacks = [((rows // 2) - 1, (cols // 2) - 1), (rows // 2, (cols // 2))]
whites = [(rows // 2, (cols // 2) - 1), ((rows // 2) - 1, cols // 2)]
black_turn = True
initial_move = (None, None)
initial_state: State = State(grid, blacks, whites, black_turn, initial_move)
while win.checkKey() != 'Escape':
if len(initial_state.valid_moves) == 0:
break
if initial_state.black_turn: # Ai's turn
[state_cost, next_state] = mini_max(TreeNode(initial_state), 3, float('-inf'), float('inf'), black_turn)
initial_state = next_state
if len(initial_state.valid_moves) == 0:
initial_state.black_turn = not initial_state.black_turn
initial_state.valid_moves = generate_valid_moves(initial_state)
updateGraphics(initial_state.grid, circles)
else:
show_hints(initial_state.valid_moves, circles)
mouse_click = win.checkMouse()
if type(mouse_click) == Point:
r, c = mouseToGrid(mouse_click)
if (r, c) in initial_state.valid_moves:
remove_hints(initial_state.valid_moves, circles)
initial_state = generate_child(initial_state, [r, c])
if len(initial_state.valid_moves) == 0:
initial_state.black_turn = not initial_state.black_turn
initial_state.valid_moves = generate_valid_moves(initial_state)
updateGraphics(initial_state.grid, circles)
# black_turn = black_turn
print(initial_state.black_score)
print(initial_state.white_score)
win.getKey()
win.close()
main()