-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOthello.py
More file actions
112 lines (96 loc) · 3.28 KB
/
Othello.py
File metadata and controls
112 lines (96 loc) · 3.28 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
# https://github.com/suragnair/alpha-zero-general/blob/master/othello/OthelloGame.py
from __future__ import print_function
import sys
sys.path.append('..')
from Board import Board
import numpy as np
class OthelloGame:
def __init__(self, n):
self.n = n
def getInitBoard(self):
# return initial board (numpy board)
b = Board(self.n)
return np.array(b.pieces)
def getBoardSize(self):
# (a,b) tuple
return (self.n, self.n)
def getActionSize(self):
# return number of actions
return self.n*self.n + 1
def getNextState(self, board, player, action):
# if player takes action on board, return next (board,player)
# action must be a valid move
if action == self.n*self.n:
return (board, -player)
b = Board(self.n)
b.pieces = np.copy(board)
move = (int(action/self.n), action%self.n)
b.execute_move(move, player)
return (b.pieces, -player)
def getValidMoves(self, board, player):
# return a fixed size binary vector
valids = [0]*self.getActionSize()
b = Board(self.n)
b.pieces = np.copy(board)
legalMoves = b.get_legal_moves(player)
if len(legalMoves)==0:
valids[-1]=1
return np.array(valids)
for x, y in legalMoves:
valids[self.n*x+y]=1
return np.array(valids)
def getGameEnded(self, board, player):
# return 0 if not ended, 1 if player 1 won, -1 if player 1 lost
# player = 1
b = Board(self.n)
b.pieces = np.copy(board)
if b.has_legal_moves(player):
return 0
if b.has_legal_moves(-player):
return 0
if b.countDiff(player) > 0:
return 1
return -1
def getCanonicalForm(self, board, player):
# return state if player==1, else return -state if player==-1
return player*board
def getSymmetries(self, board, pi):
# mirror, rotational
assert(len(pi) == self.n**2+1) # 1 for pass
pi_board = np.reshape(pi[:-1], (self.n, self.n))
l = []
for i in range(1, 5):
for j in [True, False]:
newB = np.rot90(board, i)
newPi = np.rot90(pi_board, i)
if j:
newB = np.fliplr(newB)
newPi = np.fliplr(newPi)
l += [(newB, list(newPi.ravel()) + [pi[-1]])]
return l
def stringRepresentation(self, board):
# 8x8 numpy array (canonical board)
return board.tostring()
def getScore(self, board, player):
b = Board(self.n)
b.pieces = np.copy(board)
return b.countDiff(player)
def display(board):
n = board.shape[0]
for y in range(n):
print (y,"|",end="")
print("")
print(" -----------------------")
for y in range(n):
print(y, "|",end="") # print the row #
for x in range(n):
piece = board[y][x] # get the piece to print
if piece == -1: print("b ",end="")
elif piece == 1: print("W ",end="")
else:
if x==n:
print("-",end="")
else:
print("- ",end="")
print("|")
print(" -----------------------")