-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquirkle.py
339 lines (267 loc) · 9.41 KB
/
quirkle.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
import codecs
from collections import defaultdict
import curses
import itertools
import random
import pdb
dbg = False
SHAPE = 0
COLOR = 1
class Vec2:
def __init__(self, x, y):
self.val = (x, y)
@property
def x(self):
return self.val[0]
@property
def y(self):
return self.val[1]
def __add__(self, other):
return (self.x + other.x, self.y + other.y)
def __sub__(self, other):
return (self.x - other.x, self.y - other.y)
def __hash__(self):
return self.val.__hash__()
def __eq__(self, other):
return self.x == other.x and self.y == other.y
class Board:
DIRS = [Vec2(0, -1), Vec2(0, 1), Vec2(1, 0), Vec2(-1, 0)]
def __init__(self, w, h, num_colors):
self.w = w
self.h = h
self.num_colors = num_colors
self._open = [Vec2(0, 0)]
self.open_tiles = set()
self.open_tiles.add((self.w // 2, self.h // 2))
self.tiles = defaultdict(lambda: None)
self.grid = [[None for i in range(h)] for j in range(w)]
def get_open_tiles(self):
return self.open_tiles
def test_move(self, loc, tile):
x, y = loc
#get horizontal and vertical contiguous tiles
horz = []
vert = []
if dbg:
debug()
for i in range(1, self.num_colors):
xx = (x + i) % self.w
if self.grid[xx][y]:
horz.append(self.grid[xx][y])
else:
break
for i in range(1, self.num_colors):
xx = (x - i) % self.w
if self.grid[xx][y]:
horz.append(self.grid[xx][y])
else:
break
for i in range(1, self.num_colors):
yy = (y + i) % self.h
if self.grid[x][yy]:
vert.append(self.grid[x][yy])
else:
break
for i in range(1, self.num_colors):
yy = (y - i) % self.h
if self.grid[x][yy]:
vert.append(self.grid[x][yy])
else:
break
vert.append(tile)
horz.append(tile)
score_h = self._test_group(horz)
score_v = self._test_group(vert)
if score_h == 0 or score_v == 0:
return 0
return score_h + score_v
def _test_group(self, tiles):
'''Tests if a group of tiles is valid assuming they
were to be placed contiguously in a line.
Returns the score that placing those tiles would generate (0 for invalid)
'''
lt = len(tiles)
if lt > self.num_colors:
return 0
elif lt == 1:
return 1
colors = [tile[COLOR] for tile in tiles]
shapes = [tile[SHAPE] for tile in tiles]
lc = len(set(colors)) # all equal if == 1, all unique if == lt
ls = len(set(shapes))
if lc == ls == 1:
return 0
debug()
if lc == lt:
# all colors are unique - shapes must be identical
if ls != 1:
return 0
else:
# duplicate colors - all colors must be identical and shapes must be unique
if lc != 1 and ls != lt:
return 0
if ls == lt:
# all shapes are unique - colors must be identical
if lc != 1:
return 0
else:
# duplicate shapes - all shapes must be identical and all colors must be unique
if ls != 1 and lc != lt:
return 0
if lt == self.num_colors:
return self.num_colors * 2
return lt
def move(self, loc, tile):
'''Places a tile at a location'''
score = self.test_move(loc, tile)
if score > 0:
x, y = loc
self.grid[x][y] = tile
self.open_tiles.remove((x, y))
for i, j in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
xx = (x + i) % self.w
yy = (y + j) % self.h
if not self.grid[xx][yy]:
self.open_tiles.add((xx, yy))
return score
class Bag:
def __init__(self, num_colors=6, num_sets=3):
self.num_colors = num_colors
self.infinite = num_sets < 1
if num_sets < 1:
num_sets = 1
self.tiles = [tile for tile in itertools.product(range(num_colors), repeat=2)] * num_sets
random.shuffle(self.tiles)
def draw(self, n):
tiles = []
for i in range(n):
if self.tiles:
if self.infinite:
tiles.append(self.tiles[random.randint(0, len(self.tiles) - 1)])
else:
tiles.append(self.tiles.pop())
return tiles
def debug():
curses.nocbreak()
curses.echo()
curses.endwin()
import pdb
pdb.set_trace()
class Player:
def __init__(self, board, bag, hand_size=6):
self.board = board
self.bag = bag
self.hand_size = hand_size
self.score = 0
self.hand = []
self.pickup_tiles()
def play(self):
self.play_one()
self.pickup_tiles()
if len(self.hand) == 0:
# game is done
return False
return True
def brute_force(self):
open_tiles = self.board.get_open_tiles()
def play_one(self):
open_tiles = list(self.board.get_open_tiles())
random.shuffle(open_tiles)
for x, y in open_tiles:
for tile in self.hand:
if self.board.test_move((x, y), tile):
score = self.board.move((x, y), tile)
if score:
self.hand.remove(tile)
self.score += score
return True
return False
def pickup_tiles(self):
num_to_pickup = self.hand_size - len(self.hand)
self.hand.extend(self.bag.draw(num_to_pickup))
def hand_to_str(self):
s = ""
for tile in self.hand:
r = 255 * (tile[1] in [5, 0, 1])
#g = 255 * (tile[1] in [1, 2, 3])
#b = 255 * (tile[1] in [3, 4, 5])
#s += codecs.decode(r"\033[48;2;{};{};{}m".format(r, g, b), 'unicode_escape') + self.SYMBOLS[tile[0]] + "\033[0m"
return s
class Game:
SYMBOLS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def __init__(self, screen, num_players, num_colors, hand_size):
self.h, self.w = screen.getmaxyx()
self.num_players = num_players
self.num_colors = num_colors
self.hand_size = hand_size
self.tile_bag = Bag(num_colors=num_colors)
self.board = Board(self.w, self.h - self.num_players - 1, num_colors=num_colors)
self.players = [Player(self.board, self.tile_bag, hand_size) for i in range(num_players)]
self.current_player = 0
self.header = curses.newwin(num_players + 1, self.w, 0, 0)
self.screen = curses.newwin(self.h - num_players - 1, self.w, num_players + 1, 0)
for i in range(num_colors):
print(curses.color_content(i))
curses.init_color(0, 0, 0, 0)
curses.init_color(1, 1000, 1000, 1000)
curses.init_color(2, 1000, 0, 0)
curses.init_color(3, 1000, 1000, 0)
curses.init_color(4, 0, 1000, 0)
curses.init_color(5, 0, 1000, 1000)
curses.init_color(6, 0, 0, 1000)
curses.init_color(7, 1000, 0, 1000)
for i in range(6):
curses.init_pair(i+1, i+2, 0)
def play(self):
if not self.players[self.current_player].play():
return True
self.current_player = (self.current_player + 1) % self.num_players
return False
def stop(self):
import time
time.sleep(10)
pass
def draw(self):
'''Draw the game board on screen'''
# draw header
self.header.addstr(0, self.w // 2 - 3, "Quirkle")
# draw players and their hands
for i in range(self.num_players):
self.header.addstr(i + 1, 0, "Player {:> 3d} | {:> 4d} | ".format(
i + 1,
self.players[i].score
))
for tile in self.players[i].hand:
self.header.addch(self.SYMBOLS[tile[SHAPE]], curses.color_pair(tile[COLOR] + 1))
if len(self.players[i].hand) < self.hand_size:
for i in range(self.hand_size - len(self.players[i].hand)):
self.header.addch(" ")
#self.header.addstr(i + 1, 0, "Player {}: {}".format(i + 1, self.players[i].hand_to_str()))
self.header.refresh()
#self.screen.addstr(str([curses.color_content(i) for i in range(self.num_colors)]))
for x in range(self.board.w):
for y in range(self.board.h):
tile = self.board.grid[x][y]
if tile:
sym = self.SYMBOLS[tile[SHAPE]]
self.screen.addch(y, x, sym, curses.color_pair(tile[COLOR] + 1))
self.screen.refresh()
def main(screen):
global dbg
game = Game(screen, num_players=3, num_colors=6, hand_size=6)
assert game.board._test_group([(0, 0), (0, 0)]) == 0
assert game.board._test_group([(0, 0), (0, 1)]) == 1
assert game.board._test_group([(0, 0), (1, 1)]) == 0
exit()
done = False
while not done:
game.draw()
a = screen.getch()
if a == ord('d'):
dbg = True
elif a == ord('q'):
break
done = game.play()
game.stop()
if __name__ == "__main__":
curses.wrapper(main)