-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQwirkle.py
375 lines (298 loc) · 11.7 KB
/
Qwirkle.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
from termcolor import colored
import copy
class COLORS:
RED = 'red'
YELLOW = 'yellow'
GREEN = 'green'
CYAN = 'cyan'
MAGENTA = 'magenta'
BLUE = 'blue'
class SHAPES:
TRIANGLE = '▲'
DIAMOND = '◆'
SQUARE = '■'
CIRCLE = '●'
STAR = '★'
SPARKLE = '❈'
class Piece:
def __init__(self, color=None, shape=None):
self.color = color
self.shape = shape
def __str__(self):
return '%s %s' % (self.color, self.shape)
def __repr__(self):
return self.__str__()
class GameBoard:
def __init__(self, board = [], previous_board = [], plays = [], last_plays = []):
self._board = board
self._previous_board = previous_board
self._plays = plays
self._last_plays = last_plays
def reset_board(self):
"""Clear the current board"""
self._board = []
self._plays = []
def start_turn(self):
"""Start a turn"""
self._plays = []
self._previous_board = copy.deepcopy(self._board)
def valid_plays(self):
"""Returns the valid plays"""
valid_plays = []
if not self._board:
return [(1, 1)]
for y in range(len(self._board)):
for x in range(len(self._board[y])):
if self._is_play_valid(None, x, y):
valid_plays.append((x, y))
return valid_plays
def get_board(self):
"""Return the current board with current moves"""
return self._board
def get_plays(self):
return self._plays
def get_prevoius_board(self):
return self._previous_board
def get_last_plays(self):
return self._last_plays
def play(self, piece, x=1, y=1):
"""Play a tile"""
if len(self._board) == 0:
self._board = [[None] * 3 for i in range(3)]
x = 1
y = 1
elif not self._is_play_valid(piece, x, y):
# print("not valid play")
return False
self._board[y][x] = piece
self._plays.append((x, y))
self._pad_board()
return True
def score(self):
"""Return the score for the current turn"""
if len(self._plays) == 0:
return 0
score = 0
scored_horizontally = []
scored_vertically = []
for play in self._plays:
x, y = play
min_x = x
while min_x - 1 >= 0 and self._board[y][min_x - 1] is not None:
min_x -= 1
max_x = x
while max_x + 1 < len(self._board[y]) and self._board[y][max_x + 1] is not None:
max_x += 1
if min_x != max_x:
qwirkle_count = 0
for t_x in range(min_x, max_x + 1):
if (t_x, y) not in scored_horizontally:
score += 1
qwirkle_count += 1
scored_horizontally.append((t_x, y))
if (x, y) not in scored_horizontally:
score += 1
qwirkle_count += 1
scored_horizontally.append((x, y))
t_x += 1
if qwirkle_count == 6:
score += 6
min_y = y
while min_y - 1 >= 0 and self._board[min_y - 1][x] is not None:
min_y -= 1
max_y = y
while max_y + 1 < len(self._board) and self._board[max_y + 1][x] is not None:
max_y += 1
if min_y != max_y:
qwirkle_count = 0
for t_y in range(min_y, max_y + 1):
if (x, t_y) not in scored_vertically:
score += 1
qwirkle_count += 1
scored_vertically.append((x, t_y))
if (x, y) not in scored_vertically:
score += 1
qwirkle_count += 1
scored_vertically.append((x, y))
t_y += 1
if qwirkle_count == 6:
score += 6
return score
def end_turn(self):
"""End the current turn"""
self._last_plays = self._plays[:]
self._plays = []
def reset_turn(self):
"""Reset the board to the way it was at the beginning of the turn"""
self._board = copy.deepcopy(self._previous_board)
self._plays = []
def print_board(self, show_valid_placements=True):
if len(self._board) == 0:
print(' A')
print('01', colored('■', 'white'))
return
valid_plays = self.valid_plays()
lines = []
for y in range(len(self._board)):
line = ''
for x in range(len(self._board[y])):
if self._board[y][x] is not None:
if (x, y) in self._last_plays:
line += colored(self._board[y][x].shape + ' ', self._board[y][x].color, 'on_white')
else:
line += colored(self._board[y][x].shape + ' ', self._board[y][x].color)
elif (x, y) in valid_plays and show_valid_placements:
line += colored('☐', 'white') + ' '
else:
line += ' '
lines.append(line)
# add in the top coord line
line = ''.join([chr(65 + i) + ' ' for i in range(len(self._board[0]))])
lines.insert(0, line)
lines.append(line)
for i in range(0, len(lines)):
i_display = str(i).zfill(2) if 0 < i < len(lines) - 1 else ' '
print(i_display, lines[i], i_display)
@staticmethod
def coord_to_position(coord):
x_coord = ord(coord[0]) - 65
y_coord = int(coord[1:]) - 1
return x_coord, y_coord
def _is_play_valid(self, piece, x, y):
"""Validates a move is within the board, not on the corners, not
replacing a existing piece, adjacent to an existing tile and valid in
its row/column"""
# Make sure the placement is not on a corner and is inside the board
if x < 0 or x >= len(self._board[0]):
return False
if y < 0 or y >= len(self._board):
return False
if x == 0 and y == 0:
return False
if x == 0 and y == len(self._board) - 1:
return False
if x == len(self._board[0]) - 1 and y == len(self._board) - 1:
return False
if x == len(self._board[0]) - 1 and y == 0:
return False
# Make sure the placement is not already taken
if self._board[y][x] is not None:
return False
# Make sure the placement has at least one adjacent placement
adjacent_checks = []
if y - 1 >= 0:
adjacent_checks.append((self._board[y - 1][x] is None))
if y + 1 < len(self._board):
adjacent_checks.append((self._board[y + 1][x] is None))
if x - 1 >= 0:
adjacent_checks.append((self._board[y][x - 1] is None))
if x + 1 < len(self._board[y]):
adjacent_checks.append((self._board[y][x + 1] is None))
if all(adjacent_checks):
return False
# Validate the play connects to an existing play
plays = [(play[0], play[1]) for play in self._plays]
if len(plays) > 0:
check_horizontal = True
check_vertical = True
if len(plays) > 1:
if plays[0][0] == plays[1][0]:
check_horizontal = False
if plays[0][1] == plays[1][1]:
check_vertical = False
in_plays = False
if check_horizontal:
t_x = x
while t_x - 1 >= 0 and self._board[y][t_x - 1] is not None:
t_x -= 1
if (t_x, y) in plays:
in_plays = True
t_x = x
while t_x + 1 < len(self._board[y]) and self._board[y][t_x + 1] is not None:
t_x += 1
if (t_x, y) in plays:
in_plays = True
if check_vertical:
t_y = y
while t_y - 1 >= 0 and self._board[t_y - 1][x] is not None:
t_y -= 1
if (x, t_y) in plays:
in_plays = True
t_y = y
while t_y + 1 < len(self._board) and self._board[t_y + 1][x] is not None:
t_y += 1
if (x, t_y) in plays:
in_plays = True
if not in_plays:
return False
# Don't test for piece shape/color if no piece provided
if piece is None:
return True
# Get & Verify all the tiles adjacent horizontally
row = [piece]
t_x = x + 1
while t_x < len(self._board[0]) and self._board[y][t_x] is not None:
row.append(self._board[y][t_x])
t_x += 1
t_x = x - 1
while t_x >= 0 and self._board[y][t_x] is not None:
row.append(self._board[y][t_x])
t_x -= 1
if not self._is_row_valid(row):
return False
# Get & Verify all the tiles adjacent vertically
row = [piece]
t_y = y + 1
while t_y < len(self._board) and self._board[t_y][x] is not None:
row.append(self._board[t_y][x])
t_y += 1
t_y = y - 1
while t_y >= 0 and self._board[t_y][x] is not None:
row.append(self._board[t_y][x])
t_y -= 1
if not self._is_row_valid(row):
return False
return True
def _is_row_valid(self, row):
"""If all row colors are equal, check each shape shows up at most once.
If all shapes are equal, check each color shows up at most once.
Otherwise the row is invalid."""
if len(row) == 1:
return True
if all(row[i].color == row[0].color for i in range(len(row))):
shapes = []
for i in range(len(row)):
if row[i].shape in shapes:
return False
shapes.append(row[i].shape)
elif all(row[i].shape == row[0].shape for i in range(len(row))):
colors = []
for i in range(len(row)):
if row[i].color in colors:
return False
colors.append(row[i].color)
else:
return False
return True
def _pad_board(self):
"""Ensures there is a padding of empty spots around the board, update the plays"""
# Check for top padding
if any(self._board[0][i] is not None for i in range(len(self._board[0]))):
self._board.insert(0, [None] * (len(self._board[0])))
self._plays = [(play[0], play[1]+1) for play in self._plays]
self._last_plays = [(play[0], play[1]+1) for play in self._last_plays]
# Check for bottom padding
bottom = len(self._board) - 1
if any(self._board[bottom][i] is not None for i in range(len(self._board[0]))):
self._board += [[None] * (len(self._board[0]))]
# Left padding
if any(self._board[i][0] is not None for i in range(len(self._board))):
for i in range(len(self._board)):
self._board[i].insert(0, None)
self._plays = [(play[0] + 1, play[1]) for play in self._plays]
self._last_plays = [(play[0] + 1, play[1]) for play in self._last_plays]
# Right padding
right = len(self._board[0]) - 1
if any(self._board[i][right] is not None for i in range(len(self._board))):
for i in range(len(self._board)):
self._board[i] += [None]