-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.py
37 lines (37 loc) · 898 Bytes
/
board.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
def getChar(mask):
if (mask == 0):
return '.'
elif (mask == 1):
return 'x'
elif (mask == 2):
return 'o'
else:
return '!'
class Board:
winningPos = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]
state = 0
def toString(self):
s = ""
for i in range(3):
for j in range(3):
s += getChar(((self.state) >> (i * 3 * 2 + j * 2)) & 3)
if (i < 2):
s += '\n'
return s
def addMove(self, row, col, player):
self.state = self.state | (player << (row * 2 * 3 + col * 2))
def getState(self):
return self.state
def isOver(self):
for player in range(1, 3):
for comb in self.winningPos:
mask = 0
for i in comb:
mask = (mask | (player << (i * 2)))
if ((self.state & mask) == mask):
return True
isFull = True
for i in range(9):
if ((self.state >> (i * 2)) & 3 == 0):
isFull = False
return isFull