-
Notifications
You must be signed in to change notification settings - Fork 17
/
ultimateboard.py
124 lines (109 loc) · 5.18 KB
/
ultimateboard.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
from board import TTTBoard, TTTBoardDecision, GridStates
import itertools
class UTTTBoardDecision():
ACTIVE = 10
DRAW = 11
WON_X = 12
WON_O = 13
class UTTTBoard(object):
def __init__(self):
self.board = self.emptyState()
self.decision = UTTTBoardDecision.ACTIVE
self.nextBoardLocation = [None, None]
def emptyState(self):
return [[TTTBoard(), TTTBoard(), TTTBoard()],
[TTTBoard(), TTTBoard(), TTTBoard()],
[TTTBoard(), TTTBoard(), TTTBoard()]]
def determineBoardState(self):
def winCheck(listOfThree):
threeResults = map(lambda x: x.getBoardDecision(), listOfThree)
return len(set(threeResults)) == 1 and threeResults[0] in [TTTBoardDecision.WON_O, TTTBoardDecision.WON_X]
def getWinState(listOfThree):
threeResults = map(lambda x: x.getBoardDecision(), listOfThree)
return UTTTBoardDecision.WON_O if TTTBoardDecision.WON_O in threeResults else UTTTBoardDecision.WON_X
for row in self.board: # Check rows first
if winCheck(row): # The row was won
self.decision = getWinState(row)
return
for j in range(3): # Check columns next
column = [self.board[0][j], self.board[1][j], self.board[2][j]]
if winCheck(column):
self.decision = getWinState(column)
return
diagonal1 = [self.board[i][j] for (i,j) in zip(range(3), range(3))]
diagonal2 = [self.board[i][j] for (i, j) in zip(range(3), range(2,-1,-1))]
if winCheck(diagonal1):
self.decision = getWinState(diagonal1)
return
if winCheck(diagonal2):
self.decision = getWinState(diagonal2)
return
self.decision = UTTTBoardDecision.DRAW
for (i,j) in itertools.product(range(3), range(3)):
if self.board[i][j].getBoardDecision() == TTTBoardDecision.ACTIVE:
self.decision = UTTTBoardDecision.ACTIVE
return
def getEmptyBoardPlaces(self, whichBoard):
nextBoard = self.board[whichBoard[0]][whichBoard[1]]
return nextBoard.getEmptyBoardPlaces()
def getActiveBoardLocations(self):
activeBoards = []
for (i, j) in itertools.product(range(3), range(3)):
if self.board[i][j].getBoardDecision() == TTTBoardDecision.ACTIVE or self.board[i][j].getDoesBoardHaveEmptyCell():
activeBoards.append((i, j))
return activeBoards
def getNextBoardLocation(self):
return self.nextBoardLocation
def makeMove(self, who, whichBoard, whichLocation):
tttboard = self.board[whichBoard[0]][whichBoard[1]]
i, j = whichLocation[0], whichLocation[1]
if tttboard.getGrid(i, j) != GridStates.EMPTY:
print 'That location is not empty'
return
tttboard.makeMove(who, i, j, verbose=False)
#self.printBoard()
self.determineBoardState()
if self.decision == UTTTBoardDecision.DRAW:
print 'This Ultimate-TTT game was drawn!'
self.nextBoardLocation = [None, None]
elif self.decision != UTTTBoardDecision.ACTIVE:
print 'This Ultimate-TTT game was won by %s'%(GridStates.PLAYER_X if self.decision == UTTTBoardDecision.WON_X else GridStates.PLAYER_O)
self.nextBoardLocation = [None, None]
else:
nextTttboard = self.board[i][j]
self.nextBoardLocation = [i, j] if nextTttboard.getBoardDecision() == TTTBoardDecision.ACTIVE else [None, None]
def printBoard(self):
delimiter = '-------------'*3+'\n'
for boardRow in range(3):
rowString = delimiter
rowString += self.getBoardRowString(boardRow, 0) + '\n' + delimiter
rowString += self.getBoardRowString(boardRow, 1) + '\n' + delimiter
rowString += self.getBoardRowString(boardRow, 2) + '\n' + delimiter[:-1]
print rowString
def getBoardRowString(self, boardRow, row):
rowString = ''
for boardColumn in range(3):
board = self.board[boardRow][boardColumn]
rowString += board.getBoardRowString(row)
return rowString
def getBoardState(self):
boardStrings = []
for (i,j) in itertools.product(range(3), range(3)):
board = self.board[i][j]
[boardStrings.append(''.join(board.board[i])) for i in range(3)]
return ''.join(boardStrings)
def getBoardDecision(self):
return self.decision
if __name__ == '__main__':
b = UTTTBoard()
b.makeMove(GridStates.PLAYER_X, (1,1), (1,1))
b.makeMove(GridStates.PLAYER_O, b.getNextBoardLocation(), (1, 2))
b.makeMove(GridStates.PLAYER_X, b.getNextBoardLocation(), (1, 1))
b.makeMove(GridStates.PLAYER_O, b.getNextBoardLocation(), (0, 0))
b.makeMove(GridStates.PLAYER_X, b.getNextBoardLocation(), (1, 1))
b.makeMove(GridStates.PLAYER_O, b.getNextBoardLocation(), (2, 2))
b.makeMove(GridStates.PLAYER_X, b.getNextBoardLocation(), (1, 1))
b.makeMove(GridStates.PLAYER_O, b.getNextBoardLocation(), (2, 1))
b.makeMove(GridStates.PLAYER_X, b.getNextBoardLocation(), (1, 1))
b.printBoard()
print b.getBoardState()