-
Notifications
You must be signed in to change notification settings - Fork 11
/
board.py
305 lines (264 loc) · 9.76 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
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
#!/usr/bin/python3
#
# Copyright (C) 2007, Joseph C. Lee
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import random
class Board(object):
"""Object that defines a board containing pieces."""
# Board is represented as a dict from x coordinates to lists representing
# columns of pieces. The beginning of the lists are the value of the piece
# in the column at y=0, and subsequent values are for increasing values of
# y. Missing values are represented either with None in the column list or
# a missing column in the dict.
def __init__(self):
self._data = {}
def clone(self):
"""Return a copy of the board."""
b = Board()
for (col_index, col) in list(self._data.items()):
b._data[col_index] = col[:]
return b
def get_value(self, x, y):
"""Return the value at coordinate (x,y), or None if no value is
present."""
col = self._data.get(x, None)
if col is None:
return None
if 0 <= y < len(col):
return col[y]
else:
return None
def set_value(self, x, y, value):
"""Set the value at coordinate (x,y) to the given value."""
assert y >= 0
col = self._data.get(x, None)
if col is None:
if value is not None:
self._data[x] = [None] * y + [value]
elif y < len(col):
col[y] = value
if value is None:
self._trim_column(x)
elif value is None:
pass
else:
self._data[x] = col + [None] * (y - len(col)) + [value]
def get_column_height(self, x):
"""Return the height of column x."""
col = self._data.get(x, None)
if col is None:
return 0
else:
return len(col)
@property
def width(self):
return (self.max_x - self.min_x)
@property
def height(self):
return (self.max_y - self.min_y)
@property
def min_x(self):
if len(self._data) == 0:
return 0
else:
return min(0, min(self._data.keys()))
@property
def max_x(self):
if len(self._data) == 0:
return 0
else:
return max(0, max(self._data.keys())) + 1
@property
def min_y(self):
return 0
@property
def max_y(self):
if len(self._data) == 0:
return 0
else:
return max(0, max(len(col) for col in list(self._data.values())))
def is_empty(self):
return (len(self._data) == 0)
def get_value_map(self):
"""Returns a map from coordinate tuples to values for all cells on the
board."""
value_map = {}
for (i, col) in list(self._data.items()):
for (j, value) in enumerate(col):
if value is not None:
value_map[(i, j)] = value
return value_map
def _trim_column(self, x):
# Removes any None values at the top of the given column, removing the
# column array entirely if it is empty.
col = self._data[x]
if col[-1] is not None:
return
for i in range(len(col) - 1, -1, -1):
if col[i] is not None:
self._data[x] = col[:i + 1]
return
del self._data[x]
def get_all_contiguous(self):
"""Returns a collection of all contiguous shapes with size >= 3,
where each contiguous shape is represented as a set of coordinate
tuples."""
examined = set()
all_contiguous = []
for (i, col) in list(self._data.items()):
for (j, value) in enumerate(col):
coord = (i, j)
if coord not in examined:
examined.add(coord)
contiguous = self.get_contiguous(*coord)
examined.update(contiguous)
if len(contiguous) >= 3:
all_contiguous.append(contiguous)
return all_contiguous
def get_contiguous(self, x, y):
"""Given a board coordinate, returns a set of all the coordinate
tuples that are contiguous and have the same value."""
value = self.get_value(x, y)
if value is None:
return set()
# Add the start location to the candidate and examined sets.
candidates = set()
candidates.add((x, y))
examined = set()
examined.add((x, y))
# Build the contiguous set.
contiguous = set()
while len(candidates) > 0:
coord = candidates.pop()
if self.get_value(coord[0], coord[1]) == value:
# If the candidate has the value we're looking for, add it
# to the contiguous set and add its unexamined neighbors to
# the candidate and examined sets.
contiguous.add(coord)
for (x_offset, y_offset) in ((1, 0), (-1, 0), (0, 1), (0, -1)):
coord2 = (coord[0] + x_offset, coord[1] + y_offset)
if coord2 not in examined:
examined.add(coord2)
candidates.add(coord2)
return contiguous
def remove_empty_columns(self):
"""Removes columns that are empty."""
new_data = {}
for i in sorted(self._data.keys()):
new_data[len(new_data)] = self._data[i]
self._data = new_data
def get_slide_map(self):
"""Returns a map showing where sliding pieces will go when empty
columns are removed, as a dictionary mapping old x coordinates
to new x coordinates. Does not include entries where the old
x coordinates are the same as the new ones."""
slide_map = {}
for (i, x) in enumerate(sorted(self._data.keys())):
if i != x:
slide_map[x] = i
return slide_map
def clear_pieces(self, pieces):
"""Given a set of coordinate tuples, removes their contents from the
board."""
for (x, y) in pieces:
self.set_value(x, y, None)
def insert_columns(self, col_index, num_columns):
"""Inserts empty columns at the given index, pushing higher-numbered
columns higher."""
assert num_columns >= 0
new_data = {}
for (i, col) in list(self._data.items()):
if i < col_index:
new_data[i] = col
else:
new_data[i + num_columns] = col
self._data = new_data
def delete_columns(self, col_index, num_columns):
"""Removes columns from the given location of the board, lowering the
higher-numbered columns to fill the space."""
assert 0 <= num_columns
new_data = {}
for (i, col) in list(self._data.items()):
if i < col_index:
new_data[i] = col
elif i >= col_index + num_columns:
new_data[i - num_columns] = col
self._data = new_data
def get_empty_columns(self):
"""Returns a list of empty (all zero) columns."""
empty_cols = []
for i in range(min(0, self.min_x), self.max_x):
if i not in list(self._data.items()):
empty_cols.append(i)
return empty_cols
def drop_pieces(self):
for (i, col) in list(self._data.items()):
self._data[i] = [x for x in col if x is not None]
def get_drop_map(self):
"""Returns a map showing where dropped pieces will go (compacting
out None values vertically), as a dictionary mapping old
coordinate tuples to new coordinate tuples."""
drop_map = {}
for (i, col) in list(self._data.items()):
offset = 0
for (j, value) in enumerate(col):
if value is not None:
drop_map[(i, j)] = (i, offset)
offset += 1
return drop_map
def __eq__(self, other):
return (self._data == other._data)
def __neq__(self, other):
return not self.__eq__(other)
def __repr__(self):
width = self.width
height = self.height
lines = []
for i in reversed(list(range(height))):
line = []
for j in range(width):
value = self.get_value(j, i)
if value is None:
line.append('.')
elif value == -1:
line.append('*')
else:
line.append(str(value))
lines.append(''.join(line))
return '\n'.join(lines)
def make_test_board(width, height):
b = Board()
r = random.Random()
r.seed(0)
unchosen = []
for x in range(width):
for y in range(height):
unchosen.append((x, y))
for i in range(width * height * 4 // 6):
coord = r.choice(unchosen)
unchosen.remove(coord)
b.set_value(coord[0], coord[1], r.randint(0, 2))
for i in range(10 + 1):
b.set_value(i, 0, i)
return b
def dump_board(b):
print(repr(b))
def main():
b = make_test_board(30, 20)
dump_board(b)
print(b.get_all_contiguous())
if __name__ == '__main__':
main()