-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathothello.py
724 lines (639 loc) · 20 KB
/
othello.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
712
713
714
715
716
717
718
719
720
721
722
723
724
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# Othello Program
# John Fish
# Updated from May 29, 2015 - June 26, 2015
#
# Has both basic AI (random decision) as well as
# educated AI (minimax).
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#Library import
from tkinter import *
from math import *
from time import *
from random import *
from copy import deepcopy
#Variable setup
nodes = 0
depth = 4
moves = 0
#Tkinter setup
root = Tk()
screen = Canvas(root, width=500, height=600, background="#222",highlightthickness=0)
screen.pack()
class Board:
def __init__(self):
#White goes first (0 is white and player,1 is black and computer)
self.player = 0
self.passed = False
self.won = False
#Initializing an empty board
self.array = []
for x in range(8):
self.array.append([])
for y in range(8):
self.array[x].append(None)
#Initializing center values
self.array[3][3]="w"
self.array[3][4]="b"
self.array[4][3]="b"
self.array[4][4]="w"
#Initializing old values
self.oldarray = self.array
#Updating the board to the screen
def update(self):
screen.delete("highlight")
screen.delete("tile")
for x in range(8):
for y in range(8):
#Could replace the circles with images later, if I want
if self.oldarray[x][y]=="w":
screen.create_oval(54+50*x,54+50*y,96+50*x,96+50*y,tags="tile {0}-{1}".format(x,y),fill="#aaa",outline="#aaa")
screen.create_oval(54+50*x,52+50*y,96+50*x,94+50*y,tags="tile {0}-{1}".format(x,y),fill="#fff",outline="#fff")
elif self.oldarray[x][y]=="b":
screen.create_oval(54+50*x,54+50*y,96+50*x,96+50*y,tags="tile {0}-{1}".format(x,y),fill="#000",outline="#000")
screen.create_oval(54+50*x,52+50*y,96+50*x,94+50*y,tags="tile {0}-{1}".format(x,y),fill="#111",outline="#111")
#Animation of new tiles
screen.update()
for x in range(8):
for y in range(8):
#Could replace the circles with images later, if I want
if self.array[x][y]!=self.oldarray[x][y] and self.array[x][y]=="w":
screen.delete("{0}-{1}".format(x,y))
#42 is width of tile so 21 is half of that
#Shrinking
for i in range(21):
screen.create_oval(54+i+50*x,54+i+50*y,96-i+50*x,96-i+50*y,tags="tile animated",fill="#000",outline="#000")
screen.create_oval(54+i+50*x,52+i+50*y,96-i+50*x,94-i+50*y,tags="tile animated",fill="#111",outline="#111")
if i%3==0:
sleep(0.01)
screen.update()
screen.delete("animated")
#Growing
for i in reversed(range(21)):
screen.create_oval(54+i+50*x,54+i+50*y,96-i+50*x,96-i+50*y,tags="tile animated",fill="#aaa",outline="#aaa")
screen.create_oval(54+i+50*x,52+i+50*y,96-i+50*x,94-i+50*y,tags="tile animated",fill="#fff",outline="#fff")
if i%3==0:
sleep(0.01)
screen.update()
screen.delete("animated")
screen.create_oval(54+50*x,54+50*y,96+50*x,96+50*y,tags="tile",fill="#aaa",outline="#aaa")
screen.create_oval(54+50*x,52+50*y,96+50*x,94+50*y,tags="tile",fill="#fff",outline="#fff")
screen.update()
elif self.array[x][y]!=self.oldarray[x][y] and self.array[x][y]=="b":
screen.delete("{0}-{1}".format(x,y))
#42 is width of tile so 21 is half of that
#Shrinking
for i in range(21):
screen.create_oval(54+i+50*x,54+i+50*y,96-i+50*x,96-i+50*y,tags="tile animated",fill="#aaa",outline="#aaa")
screen.create_oval(54+i+50*x,52+i+50*y,96-i+50*x,94-i+50*y,tags="tile animated",fill="#fff",outline="#fff")
if i%3==0:
sleep(0.01)
screen.update()
screen.delete("animated")
#Growing
for i in reversed(range(21)):
screen.create_oval(54+i+50*x,54+i+50*y,96-i+50*x,96-i+50*y,tags="tile animated",fill="#000",outline="#000")
screen.create_oval(54+i+50*x,52+i+50*y,96-i+50*x,94-i+50*y,tags="tile animated",fill="#111",outline="#111")
if i%3==0:
sleep(0.01)
screen.update()
screen.delete("animated")
screen.create_oval(54+50*x,54+50*y,96+50*x,96+50*y,tags="tile",fill="#000",outline="#000")
screen.create_oval(54+50*x,52+50*y,96+50*x,94+50*y,tags="tile",fill="#111",outline="#111")
screen.update()
#Drawing of highlight circles
for x in range(8):
for y in range(8):
if self.player == 0:
if valid(self.array,self.player,x,y):
screen.create_oval(68+50*x,68+50*y,32+50*(x+1),32+50*(y+1),tags="highlight",fill="#008000",outline="#008000")
if not self.won:
#Draw the scoreboard and update the screen
self.drawScoreBoard()
screen.update()
#If the computer is AI, make a move
if self.player==1:
startTime = time()
self.oldarray = self.array
alphaBetaResult = self.alphaBeta(self.array,depth,-float("inf"),float("inf"),1)
self.array = alphaBetaResult[1]
if len(alphaBetaResult)==3:
position = alphaBetaResult[2]
self.oldarray[position[0]][position[1]]="b"
self.player = 1-self.player
deltaTime = round((time()-startTime)*100)/100
if deltaTime<2:
sleep(2-deltaTime)
nodes = 0
#Player must pass?
self.passTest()
else:
screen.create_text(250,550,anchor="c",font=("Consolas",15), text="The game is done!")
#Moves to position
def boardMove(self,x,y):
global nodes
#Move and update screen
self.oldarray = self.array
self.oldarray[x][y]="w"
self.array = move(self.array,x,y)
#Switch Player
self.player = 1-self.player
self.update()
#Check if ai must pass
self.passTest()
self.update()
#METHOD: Draws scoreboard to screen
def drawScoreBoard(self):
global moves
#Deleting prior score elements
screen.delete("score")
#Scoring based on number of tiles
player_score = 0
computer_score = 0
for x in range(8):
for y in range(8):
if self.array[x][y]=="w":
player_score+=1
elif self.array[x][y]=="b":
computer_score+=1
if self.player==0:
player_colour = "green"
computer_colour = "gray"
else:
player_colour = "gray"
computer_colour = "green"
screen.create_oval(5,540,25,560,fill=player_colour,outline=player_colour)
screen.create_oval(380,540,400,560,fill=computer_colour,outline=computer_colour)
#Pushing text to screen
screen.create_text(30,550,anchor="w", tags="score",font=("Consolas", 50),fill="white",text=player_score)
screen.create_text(400,550,anchor="w", tags="score",font=("Consolas", 50),fill="black",text=computer_score)
moves = player_score+computer_score
#METHOD: Test if player must pass: if they do, switch the player
def passTest(self):
mustPass = True
for x in range(8):
for y in range(8):
if valid(self.array,self.player,x,y):
mustPass=False
if mustPass:
self.player = 1-self.player
if self.passed==True:
self.won = True
else:
self.passed = True
self.update()
else:
self.passed = False
#METHOD: Stupid AI - Chooses a random move
def dumbMove(self):
#Generates all possible moves
choices = []
for x in range(8):
for y in range(8):
if valid(self.array,self.player,x,y):
choices.append([x,y])
#Chooses a random move, moves there
dumbChoice = choice(choices)
self.arrayMove(dumbChoice[0],dumbChoice[1])
#METHOD: Not so stupid AI - Chooses a move based on what will get it the most pieces next turn
def slightlyLessDumbMove(self):
#Generates all possible choices and boards corresponding to those
boards = []
choices = []
for x in range(8):
for y in range(8):
if valid(self.array,self.player,x,y):
test = move(self.array,x,y)
boards.append(test)
choices.append([x,y])
#Determines the best score based on the prior generated boards and a "Dumb" Heuristic: dumbScore()
bestScore = -float("inf")
bestIndex = 0
for i in range(len(boards)):
score= dumbScore(boards[i],self.player)
if score>bestScore:
bestIndex=i
bestScore = score
#Move to the best location based on dumbScore()
self.arrayMove(choices[bestIndex][0],choices[bestIndex][1])
#METHOD: Actually Decent AI - Choose a move based on a simple heuristic
#Same as slightlyLessDumbMove() just uses slightlyLessDumbScore()
def decentMove(self):
#Generates all possible choices and boards corresponding to those
boards = []
choices = []
for x in range(8):
for y in range(8):
if valid(self.array,self.player,x,y):
test = move(self.array,x,y)
boards.append(test)
choices.append([x,y])
bestScore = -float("inf")
bestIndex = 0
#Determines the best score based on the prior generated boards and a "Meh" Heuristic: slightlyLessDumbScore()
for i in range(len(boards)):
score= slightlyLessDumbScore(boards[i],self.player)
if score>bestScore:
bestIndex=i
bestScore = score
#Move to the best location based on slightlyLessDumbScore()
self.arrayMove(choices[bestIndex][0],choices[bestIndex][1])
#This contains the minimax algorithm
#http://en.wikipedia.org/wiki/Minimax
def minimax(self, node, depth, maximizing):
global nodes
nodes += 1
boards = []
choices = []
for x in range(8):
for y in range(8):
if valid(self.array,self.player,x,y):
test = move(node,x,y)
boards.append(test)
choices.append([x,y])
if depth==0 or len(choices)==0:
return ([decentHeuristic(node,1-maximizing),node])
if maximizing:
bestValue = -float("inf")
bestBoard = []
for board in boards:
val = self.minimax(board,depth-1,0)[0]
if val>bestValue:
bestValue = val
bestBoard = board
return ([bestValue,bestBoard])
else:
bestValue = float("inf")
bestBoard = []
for board in boards:
val = self.minimax(board,depth-1,1)[0]
if val<bestValue:
bestValue = val
bestBoard = board
return ([bestValue,bestBoard])
#alphaBeta pruning on the minimax tree
#http://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning
def alphaBeta(self,node,depth,alpha,beta,maximizing):
global nodes
nodes += 1
boards = []
choices = []
for x in range(8):
for y in range(8):
if valid(self.array,self.player,x,y):
test = move(node,x,y)
boards.append(test)
choices.append([x,y])
if depth==0 or len(choices)==0:
return ([finalHeuristic(node,maximizing),node])
if maximizing:
v = -float("inf")
bestBoard = []
bestChoice = []
for board in boards:
boardValue = self.alphaBeta(board,depth-1,alpha,beta,0)[0]
if boardValue>v:
v = boardValue
bestBoard = board
bestChoice = choices[boards.index(board)]
alpha = max(alpha,v)
if beta <= alpha:
break
return([v,bestBoard,bestChoice])
else:
v = float("inf")
bestBoard = []
bestChoice = []
for board in boards:
boardValue = self.alphaBeta(board,depth-1,alpha,beta,1)[0]
if boardValue<v:
v = boardValue
bestBoard = board
bestChoice = choices[boards.index(board)]
beta = min(beta,v)
if beta<=alpha:
break
return([v,bestBoard,bestChoice])
#FUNCTION: Returns a board after making a move according to Othello rules
#Assumes the move is valid
def move(passedArray,x,y):
#Must copy the passedArray so we don't alter the original
array = deepcopy(passedArray)
#Set colour and set the moved location to be that colour
if board.player==0:
colour = "w"
else:
colour="b"
array[x][y]=colour
#Determining the neighbours to the square
neighbours = []
for i in range(max(0,x-1),min(x+2,8)):
for j in range(max(0,y-1),min(y+2,8)):
if array[i][j]!=None:
neighbours.append([i,j])
#Which tiles to convert
convert = []
#For all the generated neighbours, determine if they form a line
#If a line is formed, we will add it to the convert array
for neighbour in neighbours:
neighX = neighbour[0]
neighY = neighbour[1]
#Check if the neighbour is of a different colour - it must be to form a line
if array[neighX][neighY]!=colour:
#The path of each individual line
path = []
#Determining direction to move
deltaX = neighX-x
deltaY = neighY-y
tempX = neighX
tempY = neighY
#While we are in the bounds of the board
while 0<=tempX<=7 and 0<=tempY<=7:
path.append([tempX,tempY])
value = array[tempX][tempY]
#If we reach a blank tile, we're done and there's no line
if value==None:
break
#If we reach a tile of the player's colour, a line is formed
if value==colour:
#Append all of our path nodes to the convert array
for node in path:
convert.append(node)
break
#Move the tile
tempX+=deltaX
tempY+=deltaY
#Convert all the appropriate tiles
for node in convert:
array[node[0]][node[1]]=colour
return array
#Method for drawing the gridlines
def drawGridBackground(outline=False):
#If we want an outline on the board then draw one
if outline:
screen.create_rectangle(50,50,450,450,outline="#111")
#Drawing the intermediate lines
for i in range(7):
lineShift = 50+50*(i+1)
#Horizontal line
screen.create_line(50,lineShift,450,lineShift,fill="#111")
#Vertical line
screen.create_line(lineShift,50,lineShift,450,fill="#111")
screen.update()
#Simple heuristic. Compares number of each tile.
def dumbScore(array,player):
score = 0
#Set player and opponent colours
if player==1:
colour="b"
opponent="w"
else:
colour = "w"
opponent = "b"
#+1 if it's player colour, -1 if it's opponent colour
for x in range(8):
for y in range(8):
if array[x][y]==colour:
score+=1
elif array[x][y]==opponent:
score-=1
return score
#Less simple but still simple heuristic. Weights corners and edges as more
def slightlyLessDumbScore(array,player):
score = 0
#Set player and opponent colours
if player==1:
colour="b"
opponent="w"
else:
colour = "w"
opponent = "b"
#Go through all the tiles
for x in range(8):
for y in range(8):
#Normal tiles worth 1
add = 1
#Edge tiles worth 3
if (x==0 and 1<y<6) or (x==7 and 1<y<6) or (y==0 and 1<x<6) or (y==7 and 1<x<6):
add=3
#Corner tiles worth 5
elif (x==0 and y==0) or (x==0 and y==7) or (x==7 and y==0) or (x==7 and y==7):
add = 5
#Add or subtract the value of the tile corresponding to the colour
if array[x][y]==colour:
score+=add
elif array[x][y]==opponent:
score-=add
return score
#Heuristic that weights corner tiles and edge tiles as positive, adjacent to corners (if the corner is not yours) as negative
#Weights other tiles as one point
def decentHeuristic(array,player):
score = 0
cornerVal = 25
adjacentVal = 5
sideVal = 5
#Set player and opponent colours
if player==1:
colour="b"
opponent="w"
else:
colour = "w"
opponent = "b"
#Go through all the tiles
for x in range(8):
for y in range(8):
#Normal tiles worth 1
add = 1
#Adjacent to corners are worth -3
if (x==0 and y==1) or (x==1 and 0<=y<=1):
if array[0][0]==colour:
add = sideVal
else:
add = -adjacentVal
elif (x==0 and y==6) or (x==1 and 6<=y<=7):
if array[7][0]==colour:
add = sideVal
else:
add = -adjacentVal
elif (x==7 and y==1) or (x==6 and 0<=y<=1):
if array[0][7]==colour:
add = sideVal
else:
add = -adjacentVal
elif (x==7 and y==6) or (x==6 and 6<=y<=7):
if array[7][7]==colour:
add = sideVal
else:
add = -adjacentVal
#Edge tiles worth 3
elif (x==0 and 1<y<6) or (x==7 and 1<y<6) or (y==0 and 1<x<6) or (y==7 and 1<x<6):
add=sideVal
#Corner tiles worth 15
elif (x==0 and y==0) or (x==0 and y==7) or (x==7 and y==0) or (x==7 and y==7):
add = cornerVal
#Add or subtract the value of the tile corresponding to the colour
if array[x][y]==colour:
score+=add
elif array[x][y]==opponent:
score-=add
return score
#Seperating the use of heuristics for early/mid/late game.
def finalHeuristic(array,player):
if moves<=8:
numMoves = 0
for x in range(8):
for y in range(8):
if valid(array,player,x,y):
numMoves += 1
return numMoves+decentHeuristic(array,player)
elif moves<=52:
return decentHeuristic(array,player)
elif moves<=58:
return slightlyLessDumbScore(array,player)
else:
return dumbScore(array,player)
#Checks if a move is valid for a given array.
def valid(array,player,x,y):
#Sets player colour
if player==0:
colour="w"
else:
colour="b"
#If there's already a piece there, it's an invalid move
if array[x][y]!=None:
return False
else:
#Generating the list of neighbours
neighbour = False
neighbours = []
for i in range(max(0,x-1),min(x+2,8)):
for j in range(max(0,y-1),min(y+2,8)):
if array[i][j]!=None:
neighbour=True
neighbours.append([i,j])
#If there's no neighbours, it's an invalid move
if not neighbour:
return False
else:
#Iterating through neighbours to determine if at least one line is formed
valid = False
for neighbour in neighbours:
neighX = neighbour[0]
neighY = neighbour[1]
#If the neighbour colour is equal to your colour, it doesn't form a line
#Go onto the next neighbour
if array[neighX][neighY]==colour:
continue
else:
#Determine the direction of the line
deltaX = neighX-x
deltaY = neighY-y
tempX = neighX
tempY = neighY
while 0<=tempX<=7 and 0<=tempY<=7:
#If an empty space, no line is formed
if array[tempX][tempY]==None:
break
#If it reaches a piece of the player's colour, it forms a line
if array[tempX][tempY]==colour:
valid=True
break
#Move the index according to the direction of the line
tempX+=deltaX
tempY+=deltaY
return valid
#When the user clicks, if it's a valid move, make the move
def clickHandle(event):
global depth
xMouse = event.x
yMouse = event.y
if running:
if xMouse>=450 and yMouse<=50:
root.destroy()
elif xMouse<=50 and yMouse<=50:
playGame()
else:
#Is it the player's turn?
if board.player==0:
#Delete the highlights
x = int((event.x-50)/50)
y = int((event.y-50)/50)
#Determine the grid index for where the mouse was clicked
#If the click is inside the bounds and the move is valid, move to that location
if 0<=x<=7 and 0<=y<=7:
if valid(board.array,board.player,x,y):
board.boardMove(x,y)
else:
#Difficulty clicking
if 300<=yMouse<=350:
#One star
if 25<=xMouse<=155:
depth = 1
playGame()
#Two star
elif 180<=xMouse<=310:
depth = 4
playGame()
#Three star
elif 335<=xMouse<=465:
depth = 6
playGame()
def keyHandle(event):
symbol = event.keysym
if symbol.lower()=="r":
playGame()
elif symbol.lower()=="q":
root.destroy()
def create_buttons():
#Restart button
#Background/shadow
screen.create_rectangle(0,5,50,55,fill="#000033", outline="#000033")
screen.create_rectangle(0,0,50,50,fill="#000088", outline="#000088")
#Arrow
screen.create_arc(5,5,45,45,fill="#000088", width="2",style="arc",outline="white",extent=300)
screen.create_polygon(33,38,36,45,40,39,fill="white",outline="white")
#Quit button
#Background/shadow
screen.create_rectangle(450,5,500,55,fill="#330000", outline="#330000")
screen.create_rectangle(450,0,500,50,fill="#880000", outline="#880000")
#"X"
screen.create_line(455,5,495,45,fill="white",width="3")
screen.create_line(495,5,455,45,fill="white",width="3")
def runGame():
global running
running = False
#Title and shadow
screen.create_text(250,203,anchor="c",text="Othello",font=("Consolas", 50),fill="#aaa")
screen.create_text(250,200,anchor="c",text="Othello",font=("Consolas", 50),fill="#fff")
#Creating the difficulty buttons
for i in range(3):
#Background
screen.create_rectangle(25+155*i, 310, 155+155*i, 355, fill="#000", outline="#000")
screen.create_rectangle(25+155*i, 300, 155+155*i, 350, fill="#111", outline="#111")
spacing = 130/(i+2)
for x in range(i+1):
#Star with double shadow
screen.create_text(25+(x+1)*spacing+155*i,326,anchor="c",text="\u2605", font=("Consolas", 25),fill="#b29600")
screen.create_text(25+(x+1)*spacing+155*i,327,anchor="c",text="\u2605", font=("Consolas",25),fill="#b29600")
screen.create_text(25+(x+1)*spacing+155*i,325,anchor="c",text="\u2605", font=("Consolas", 25),fill="#ffd700")
screen.update()
def playGame():
global board, running
running = True
screen.delete(ALL)
create_buttons()
board = 0
#Draw the background
drawGridBackground()
#Create the board and update it
board = Board()
board.update()
runGame()
#Binding, setting
screen.bind("<Button-1>", clickHandle)
screen.bind("<Key>",keyHandle)
screen.focus_set()
#Run forever
root.wm_title("Othello")
root.mainloop()