forked from shayakbanerjee/ultimate-ttt-rl
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ultimateboard.py
247 lines (109 loc) · 5.35 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
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
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 = [x.getBoardDecision() for x in listOfThree]
return len(set(threeResults)) == 1 and threeResults[0] in [TTTBoardDecision.WON_O, TTTBoardDecision.WON_X]
def getWinState(listOfThree):
threeResults = [x.getBoardDecision() for x in 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(list(range(3)), list(range(3)))]
diagonal2 = [self.board[i][j] for (i, j) in zip(list(range(3)), list(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(list(range(3)), list(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(list(range(3)), list(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(list(range(3)), list(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())