-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathothelloGame.py
461 lines (393 loc) · 16.3 KB
/
othelloGame.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
import random
import numpy as np
# TODO: Create evaluation structure
class othello:
"""
Encapsulation of the board game othello
"""
class ai:
"""
Class to encapsulate all ai behaviour, more to come.
To create a new ai, make a child class :)
"""
def __init__(self, marker):
self.name = "base_ai"
self.marker = marker
self.depth = 0
self.search_mode = 0
def peekScore(self, board, x, y):
dupeBoard = self._duplicateBoard_(board)
self._makeMove_(dupeBoard, self.marker, x, y)
score = self.getCurrentScore(dupeBoard)[str(self.marker)]
return score
def createNewBoardState(self, board, x, y):
dupeBoard = self._duplicateBoard_(board)
self._makeMove_(dupeBoard, self.marker, x, y)
return dupeBoard
def createChildBoardState(self, board, x, y, marker):
dupeBoard = self._duplicateBoard_(board)
self._makeMove_(dupeBoard, marker, x, y)
return dupeBoard
def _makeMove_(self, board, tile, xstart, ystart):
tilesToFlip = self.isValidMove(board, tile, xstart, ystart)
if tilesToFlip == False:
return False
board[xstart, ystart] = tile
for x, y in tilesToFlip:
board[x, y] = tile
return True
def _duplicateBoard_(self, board):
b = np.copy(board)
return b
def isOnCorner(self, x, y):
# Returns True if the position is in one of the four corners.
return (
(x == 0 and y == 0)
or (x == 7 and y == 0)
or (x == 0 and y == 7)
or (x == 7 and y == 7)
)
def getCurrentScore(self, board):
# Determine the score by counting the tiles. Returns a dictionary with keys 'X' and 'O'.
xscore = 0
oscore = 0
for x in range(8):
for y in range(8):
if board[x, y] == "1":
xscore += 1
if board[x, y] == "-1":
oscore += 1
return {"1": xscore, "-1": oscore}
def _isOnBoard_(self, x, y):
return x >= 0 and x <= 7 and y >= 0 and y <= 7
def isValidMove(self, board, tile, xstart, ystart):
# Returns False if the player's move on space xstart, ystart is invalid.
# If it is a valid move, returns a list of spaces that would become the player's if they made a move here.
if board[xstart, ystart] != 0 or not self._isOnBoard_(xstart, ystart):
return False
board[xstart, ystart] = tile # temporarily set the tile on the board.
if tile == 1:
otherTile = -1
else:
otherTile = 1
tilesToFlip = []
for xdirection, ydirection in [
[0, 1],
[1, 1],
[1, 0],
[1, -1],
[0, -1],
[-1, -1],
[-1, 0],
[-1, 1],
]:
x, y = xstart, ystart
x += xdirection # first step in the direction
y += ydirection # first step in the direction
if self._isOnBoard_(x, y) and board[x, y] == otherTile:
# There is a piece belonging to the other player next to our piece.
x += xdirection
y += ydirection
if not self._isOnBoard_(x, y):
continue
while board[x, y] == otherTile:
x += xdirection
y += ydirection
if not self._isOnBoard_(
x, y
): # break out of while loop, then continue in for loop
break
if not self._isOnBoard_(x, y):
continue
if board[x, y] == tile:
# There are pieces to flip over. Go in the reverse direction until we reach the original space, noting all the tiles along the way.
while True:
x -= xdirection
y -= ydirection
if x == xstart and y == ystart:
break
tilesToFlip.append([x, y])
board[xstart, ystart] = 0 # restore the empty space
if (
len(tilesToFlip) == 0
): # If no tiles were flipped, this is not a valid move.
return False
return tilesToFlip
def getLegalMoves(self, board, tile):
# Returns a list of [x,y] lists of valid moves for the given player on the given board.
validMoves = []
for x in range(8):
for y in range(8):
if self.isValidMove(board, tile, x, y) != False:
validMoves.append([x, y])
return validMoves
def getMove(self, board):
print("WARNING: this is the base ai class, use a custom class please.")
return
def __init__(self, bot1=ai("X"), bot2=ai("O"), verbose=True):
self.mainBoard = self.getNewBoard()
self.resetBoard(self.mainBoard)
self.verbose = verbose
self.bot1 = bot1
self.bot2 = bot2
if not self.checkBotLegality(self.bot1, self.bot2):
print("Bots not compatiable: Fatal error")
return
def startgame(self, start_move=0):
# Welcome message for bots, comment out lines if you want to surpress terminal outputs
if self.verbose:
self.welcomeMessage(self.bot1)
self.welcomeMessage(self.bot2)
# Randomly initialise board (not implemented)
self.mainBoard = self.createRandomBoard(start_move)
# TODO: reimplement random player start
random_num = random.randint(0, 1)
if random_num == 0:
while True:
turn_state = self.takeTurn(self.bot1, self.bot2, verbose=self.verbose)
if turn_state == 1:
return
elif turn_state == -1:
break
turn_state = self.takeTurn(self.bot2, self.bot1, verbose=self.verbose)
if turn_state == 1:
return
elif turn_state == -1:
break
else:
while True:
turn_state = self.takeTurn(self.bot2, self.bot1, verbose=self.verbose)
if turn_state == 1:
return
elif turn_state == -1:
break
turn_state = self.takeTurn(self.bot1, self.bot2, verbose=self.verbose)
if turn_state == 1:
return
elif turn_state == -1:
break
# Game finished, show results
if self.verbose:
self.displayResults(self.bot1)
return self.getScoreOfBoard(self.mainBoard)
def createRandomBoard(self, turns_in):
while True:
B = self.getNewBoard()
self.resetBoard(B)
for i in range(turns_in):
legal_moves = self.getValidMoves(B, 1)
if legal_moves == []:
continue
move = random.choice(legal_moves)
self.makeMove(B, 1, move[0], move[1])
legal_moves = self.getValidMoves(B, -1)
if legal_moves == []:
continue
move = random.choice(legal_moves)
self.makeMove(B, -1, move[0], move[1])
# Check to see if any moves can stil be made on board
if self.getValidMoves(B, 1) == []:
continue
if self.getValidMoves(B, -1) == []:
continue
return B
def takeTurn(self, bot, bot_other, verbose=True):
if verbose:
self.drawBoard(self.mainBoard)
self.showPoints(self.mainBoard)
move = bot.getMove(self.mainBoard)
if move == "quit":
return 1
else:
self.makeMove(self.mainBoard, bot.marker, move[0], move[1])
if self.getValidMoves(self.mainBoard, bot_other.marker) == []:
return -1
else:
return 0
def checkBotLegality(self, bot1, bot2):
if bot1.marker == self.bot2.marker:
return False
return True
def welcomeMessage(self, bot):
if bot.name == "base_ai":
print("Welcome base ai I am treating you like a human.")
elif bot.name == "human":
print("Welcome human.")
else:
print("I only know how to deal with humans at the moment, sorry.")
return
def displayResults(self, bot):
self.drawBoard(self.mainBoard)
scores = self.getScoreOfBoard(self.mainBoard)
#print("X scored %s points. O scored %s points." % (scores["1"], scores["-1"]))
print(self.bot1.name,' score: ', str(scores[str(self.bot1.marker)]))
print(self.bot2.name,' score: ', str(scores[str(self.bot2.marker)]))
# if scores[str(bot.marker)] > scores[str(self.bot2.marker)]:
# print(
# "You beat the computer by %s points! Congratulations!"
# % (scores[str(bot.marker)] - scores[str(self.bot2.marker)])
# )
# elif scores[str(bot.marker)] < scores[str(self.bot2.marker)]:
# print(
# "You lost. The computer beat you by %s points."
# % (scores[str(self.bot2.marker)] - scores[str(bot.marker)])
# )
# else:
# print("The game was a tie!")
def drawBoard(self, board):
# This function prints out the board that it was passed. Returns None.
HLINE = " +---+---+---+---+---+---+---+---+"
VLINE = " | | | | | | | | |"
print(" 1 2 3 4 5 6 7 8")
print(HLINE)
for y in range(8):
print(VLINE)
print(y + 1, end=" ")
for x in range(8):
if board[x, y] == 0:
print("| ", end=" ")
else:
print("| %d" % (board[x, y] + 2), end=" ")
print("|")
print(VLINE)
print(HLINE)
def resetBoard(self, board):
# Blanks out the board it is passed, except for the original starting position.
for x in range(8):
for y in range(8):
board[x, y] = 0
# Starting pieces:
board[3, 3] = 1
board[3, 4] = -1
board[4, 3] = -1
board[4, 4] = 1
def getNewBoard(self):
# Creates a brand new, blank board data structure.
board = np.zeros((8, 8),dtype=int)
# board = []
# for i in range(8):
# board.append([" "] * 8)
return board
def isValidMove(self, board, tile, xstart, ystart):
# Returns False if the player's move on space xstart, ystart is invalid.
# If it is a valid move, returns a list of spaces that would become the player's if they made a move here.
if board[xstart, ystart] != 0 or not self.isOnBoard(xstart, ystart):
return False
board[xstart, ystart] = tile # temporarily set the tile on the board.
if tile == 1:
otherTile = -1
else:
otherTile = 1
tilesToFlip = []
for xdirection, ydirection in [
[0, 1],
[1, 1],
[1, 0],
[1, -1],
[0, -1],
[-1, -1],
[-1, 0],
[-1, 1],
]:
x, y = xstart, ystart
x += xdirection # first step in the direction
y += ydirection # first step in the direction
if self.isOnBoard(x, y) and board[x, y] == otherTile:
# There is a piece belonging to the other player next to our piece.
x += xdirection
y += ydirection
if not self.isOnBoard(x, y):
continue
while board[x, y] == otherTile:
x += xdirection
y += ydirection
if not self.isOnBoard(
x, y
): # break out of while loop, then continue in for loop
break
if not self.isOnBoard(x, y):
continue
if board[x, y] == tile:
# There are pieces to flip over. Go in the reverse direction until we reach the original space, noting all the tiles along the way.
while True:
x -= xdirection
y -= ydirection
if x == xstart and y == ystart:
break
tilesToFlip.append([x, y])
board[xstart, ystart] = 0 # restore the empty space
if len(tilesToFlip) == 0: # If no tiles were flipped, this is not a valid move.
return False
return tilesToFlip
def isOnBoard(self, x, y):
# Returns True if the coordinates are located on the board.
return x >= 0 and x <= 7 and y >= 0 and y <= 7
def getValidMoves(self, board, tile):
# Returns a list of [x,y] lists of valid moves for the given player on the given board.
validMoves = []
for x in range(8):
for y in range(8):
if self.isValidMove(board, tile, x, y) != False:
validMoves.append([x, y])
return validMoves
def getScoreOfBoard(self, board):
# Determine the score by counting the tiles. Returns a dictionary with keys 'X' and 'O'.
xscore = 0
oscore = 0
for x in range(8):
for y in range(8):
if board[x, y] == 1:
xscore += 1
if board[x, y] == -1:
oscore += 1
return {"1": xscore, "-1": oscore}
def makeMove(self, board, tile, xstart, ystart):
# Place the tile on the board at xstart, ystart, and flip any of the opponent's pieces.
# Returns False if this is an invalid move, True if it is valid.
tilesToFlip = self.isValidMove(board, tile, xstart, ystart)
if tilesToFlip == False:
return False
board[xstart, ystart] = tile
for x, y in tilesToFlip:
board[x, y] = tile
return True
def duplicateBoard(self, board):
b = np.copy(board)
return b
def isOnCorner(self, x, y):
# Returns True if the position is in one of the four corners.
return (
(x == 0 and y == 0)
or (x == 7 and y == 0)
or (x == 0 and y == 7)
or (x == 7 and y == 7)
)
def getComputerMove(self, board, computerTile):
# Given a board and the computer's tile, determine where to
# move and return that move as a [x, y] list.
possibleMoves = self.getValidMoves(board, computerTile)
# randomize the order of the possible moves
random.shuffle(possibleMoves)
# always go for a corner if available.
for x, y in possibleMoves:
if self.isOnCorner(x, y):
return [x, y]
# Go through all the possible moves and remember the best scoring move
bestScore = -1
for x, y in possibleMoves:
dupeBoard = self.duplicateBoard(board)
self.makeMove(dupeBoard, computerTile, x, y)
score = self.getScoreOfBoard(dupeBoard)[str(computerTile)]
if score > bestScore:
bestMove = [x, y]
bestScore = score
return bestMove
def showPoints(self, mainBoard):
# Prints out the current score.
scores = self.getScoreOfBoard(mainBoard)
# print(
# "You have %d points. The computer has %d points."
# % (scores[str(self.bot1.marker)], scores[str(self.bot2.marker)])
# )
print(self.bot1.name,' score: ', str(scores[str(self.bot1.marker)]))
print(self.bot2.name,' score: ', str(scores[str(self.bot2.marker)]))