-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
280 lines (227 loc) · 8.05 KB
/
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
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
from utils import *
class Board:
def __init__(self, levels, rows, cols):
self.levels = levels
self.rows = rows
self.cols = cols
matrix = []
for i in range(0,levels):
level = []
for j in range(0,rows):
row = []
for k in range(0,cols):
row.append(Cell.CELL_EMPTY)
level.append(row)
matrix.append(level)
self.matrix = matrix
def get_available_moves(self):
available_moves = []
for level in range(0, len(self.matrix)):
for row in range(0, len(self.matrix[level])):
for col in range(0, len(self.matrix[level][row])):
if self.is_move_ok(level, row, col):
available_moves.append(Move(level, row, col))
return available_moves
def get_all_cells_of_player(self, pl_id):
if pl_id == PlayerID.PLAYER_1:
check_cell = Cell.CELL_PLAYER_1
else:
check_cell = Cell.CELL_PLAYER_2
res = []
for level in range(0, len(self.matrix)):
for row in range(0, len(self.matrix[level])):
for col in range(0, len(self.matrix[level][row])):
if self.matrix[level][row][col] == check_cell:
res.append(Move(level, row, col))
return res
def get_all_lines_with_cell(self, level, row, col):
# Returns an array of lines
res = []
# Lines in planes
level_plane = self.get_level_plane(level)
res.append(level_plane[row])
res.append(transpose(level_plane)[col])
row_plane = self.get_row_plane(row)
res.append(transpose(row_plane)[col])
# Diagonals in planes
if (level == row):
col_plane = self.get_col_plane(col)
res.append([col_plane[c][c] for c in range(0, len(col_plane))])
if (level == self.rows - row - 1):
col_plane = self.get_col_plane(col)
res.append([col_plane[c][len(col_plane) - c - 1] for c in range(0, len(col_plane))])
if (level == col):
row_plane = self.get_row_plane(row)
res.append([row_plane[c][c] for c in range(0, len(row_plane))])
if (level == self.cols - col - 1):
row_plane = self.get_row_plane(row)
res.append([row_plane[c][len(row_plane) - c - 1] for c in range(0, len(row_plane))])
if (row == col):
level_plane = self.get_level_plane(level)
res.append([level_plane[c][c] for c in range(0, len(level_plane))])
if (row == self.cols - col - 1):
level_plane = self.get_level_plane(level)
res.append([level_plane[c][len(level_plane) - c - 1] for c in range(0, len(level_plane))])
# Cube diagonal
if (level == row == col):
diag_plane = self.get_diagonal_plane(Diagonal.DIAGONAL_PRIMARY)
res.append([diag_plane[c][c] for c in range(0, len(diag_plane))])
if (level == (self.rows - row - 1) == (self.cols - col - 1)):
diag_plane = self.get_diagonal_plane(Diagonal.DIAGONAL_PRIMARY)
res.append([diag_plane[c][len(diag_plane) - c - 1] for c in range(0, len(diag_plane))])
if (level == col == (self.rows - row - 1)):
diag_plane = self.get_diagonal_plane(Diagonal.DIAGONAL_SECONDARY)
res.append([diag_plane[c][c] for c in range(0, len(diag_plane))])
if (level == row == (self.cols - col - 1)):
diag_plane = self.get_diagonal_plane(Diagonal.DIAGONAL_SECONDARY)
res.append([diag_plane[c][len(diag_plane) - c - 1] for c in range(0, len(diag_plane))])
return res
def check_win(self, last_move=None):
# Returns a RESULT
# Check level planes
for level in range(0, self.levels):
if last_move == None or last_move.level == level:
level_plane = self.get_level_plane(level)
result = self.check_win_in_plane(level_plane)
if result != Result.RESULT_CONTINUE:
return result
# Check row planes
for row in range(0, self.rows):
if last_move == None or last_move.row == row:
row_plane = self.get_row_plane(row)
result = self.check_win_in_plane(row_plane)
if result != Result.RESULT_CONTINUE:
return result
# Check col planes
for col in range(0, self.cols):
if last_move == None or last_move.col == col:
col_plane = self.get_col_plane(col)
result = self.check_win_in_plane(col_plane)
if result != Result.RESULT_CONTINUE:
return result
# Check cube diagonals
diag_plane_1 = self.get_diagonal_plane(Diagonal.DIAGONAL_PRIMARY)
result = self.check_win_in_plane(diag_plane_1)
if result != Result.RESULT_CONTINUE:
return result
diag_plane_2 = self.get_diagonal_plane(Diagonal.DIAGONAL_SECONDARY)
result = self.check_win_in_plane(diag_plane_2)
if result != Result.RESULT_CONTINUE:
return result
# Check if draw
is_draw = True
for row in self.matrix[self.levels-1]:
for col in row:
if col == Cell.CELL_EMPTY:
is_draw = False
if is_draw:
return Result.RESULT_DRAW
return Result.RESULT_CONTINUE
def check_win_in_plane(self, plane):
# Returns a RESULT
# "plane" is a 2D matrix
# Check rows
for row in plane:
result = self.check_win_in_line(row)
if result != Result.RESULT_CONTINUE:
return result
# Check cols
transposed_plane = transpose(plane)
for row in transposed_plane:
result = self.check_win_in_line(row)
if result != Result.RESULT_CONTINUE:
return result
# Check diagonals
diag_1 = [plane[col_n][col_n] for col_n in range(0, len(plane))]
result = self.check_win_in_line(diag_1)
if result != Result.RESULT_CONTINUE:
return result
diag_2 = [plane[col_n][self.cols-col_n-1] for col_n in range(0, len(plane))]
result = self.check_win_in_line(diag_2)
if result != Result.RESULT_CONTINUE:
return result
return Result.RESULT_CONTINUE
def check_win_in_line(self, line):
# Returns a RESULT
# "line" is a 1D matrix
cell_check = line[0]
if cell_check == Cell.CELL_EMPTY:
return Result.RESULT_CONTINUE
for cell_n in range(1, len(line)):
cell = line[cell_n]
if cell != cell_check:
return Result.RESULT_CONTINUE
if cell_check == Cell.CELL_PLAYER_1:
return Result.RESULT_WIN_1
else:
return Result.RESULT_WIN_2
def get_level_plane(self, level):
# Return a rows_x_cols matrix
return self.matrix[level]
def get_row_plane(self, row):
# Return a levels_x_cols matrix
structured_row = []
for level in self.matrix:
structured_row.append(level[row])
return structured_row
def get_col_plane(self, col):
# Return a levels_x_rows matrix
structured_col = []
for level in self.matrix:
temp = []
for row in level:
temp.append(row[col])
structured_col.append(temp)
return structured_col
def get_diagonal_plane(self, diag):
# Returns a levels_x_[rows,cols] matrix
structured_diag = []
for level in self.matrix:
temp = []
for row in range(0, len(level)):
for col in range(0, len(level[row])):
if (diag == Diagonal.DIAGONAL_PRIMARY and row == col) or (diag == Diagonal.DIAGONAL_SECONDARY and row == (self.cols-col-1)):
temp.append(level[row][col])
structured_diag.append(temp)
return structured_diag
def do_move(self, level, row, col, p_id, verbose=False):
ok = self.is_move_ok(level, row, col, verbose)
if ok:
if p_id == PlayerID.PLAYER_1:
self.matrix[level][row][col] = Cell.CELL_PLAYER_1
else:
self.matrix[level][row][col] = Cell.CELL_PLAYER_2
return ok
def undo_move(self, level, row, col):
self.matrix[level][row][col] = Cell.CELL_EMPTY
def is_move_ok(self, level, row, col, verbose=False):
# Out of bounds
if (level < 0 or level >= self.levels) or (row < 0 or row >= self.rows) or (col < 0 or col >= self.cols):
if verbose:
print("Cell out of bounds")
return False
# If the cell is already occupied
if self.matrix[level][row][col] != Cell.CELL_EMPTY:
if verbose:
print("Cell already occupied")
return False
# If the underlying level is not already occupied
elif level > 0 and self.matrix[level-1][row][col] == Cell.CELL_EMPTY:
if verbose:
print("Underlying cell still empty")
return False
else:
return True
def print_board(self):
for level in range(0, len(self.matrix)):
print("Level {0}".format(level))
for row in range(0, len(self.matrix[level])):
for col in range(0, len(self.matrix[level][row])):
print(self.matrix[level][row][col].value, end=' ')
print()
print()
def reset(self):
for level in range(0, len(self.matrix)):
for row in range(0, len(self.matrix[level])):
for col in range(0, len(self.matrix[level][row])):
self.matrix[level][row][col] = Cell.CELL_EMPTY