-
Notifications
You must be signed in to change notification settings - Fork 2
/
py2048_classes.py
374 lines (333 loc) · 11.9 KB
/
py2048_classes.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
"""
Python 2048 Game : Core Classes
Originally written by Phil Rodgers, University of Strathclyde
"""
import random
class Tile:
_value = 0
_has_merged = False
def __init__(self, tile_value):
self._value = tile_value
def __repr__(self):
return str("Tile({})".format(self._value))
def set_value(self, value):
self._value = value
def inc_value(self):
self._value = self._value + 1
self._has_merged = True
def has_merged(self):
return self._has_merged
def reset_merged(self):
self._has_merged = False
def get_value(self):
return self._value
def get_tile_value(self):
return 2 ** self._value
def __str__(self):
v = self._value
return f'{v:x}'
class Board:
def __init__(self, initial_state=None, initial_score=0, initial_merge_count=0):
"""Initialise the Board."""
if initial_state == None:
self.grid = [
[None, None, None, None],
[None, None, None, None],
[None, None, None, None],
[None, None, None, None]
]
else:
grid = []
for row in initial_state:
new_row = []
for element in row:
if element is None:
new_row.append(None)
else:
new_row.append(Tile(element))
grid.append(new_row)
self.grid = grid
self.score = initial_score
self.merge_count = initial_merge_count
def __repr__(self):
state = self.export_state()
score = self.score
merge_count = self.merge_count
return str("state={}, score={}, merge_count={}".format(state, score, merge_count))
def __str__(self):
"""Print out full state of the Board."""
return_string = self.print_metrics() + "\n"
return_string = return_string + self.print_board()
return return_string
def add_random_tiles(self, n):
if self.is_board_full():
return False
while n > 0:
x = random.randint(0, 3)
y = random.randint(0, 3)
if self.is_empty(x, y):
p = random.randint(1, 5)
if p == 1:
tile = Tile(2)
else:
tile = Tile(1)
self.grid[y][x] = tile
n = n - 1
return True
def make_move(self, move):
self.reset_tile_merges()
if move == 'UP':
return self.__go_up()
if move == 'DOWN':
return self.__go_down()
if move == 'LEFT':
return self.__go_left()
if move == 'RIGHT':
return self.__go_right()
return False
def __go_up(self):
moved = self.__scooch_up()
for x in range(4):
for y in range(4):
moved = self.__go_up_1(x, y) or moved
return moved
def __go_left(self):
moved = self.__scooch_left()
for y in range(4):
for x in range(4):
moved = self.__go_left_1(x, y) or moved
return moved
def __go_down(self):
moved = self.__scooch_down()
for x in range(4):
for y in [3, 2, 1, 0]:
moved = self.__go_down_1(x, y) or moved
return moved
def __go_right(self):
moved = self.__scooch_right()
for y in range(4):
for x in [3, 2, 1, 0]:
moved = self.__go_right_1(x, y) or moved
return moved
def __go_up_1(self, x, y):
moved = False
if y == 0:
return False
tile1 = self.grid[y][x]
if tile1 is not None:
tile2 = self.grid[y-1][x]
if tile2 is None:
self.grid[y-1][x] = tile1
self.grid[y][x] = None
moved = True
else:
if (not tile2.has_merged()) and tile2.get_value() == tile1.get_value():
self.grid[y-1][x] = tile1
self.grid[y][x] = None
tile1.inc_value()
self.score += tile1.get_tile_value()
self.merge_count += 1
moved = True
if moved:
for i in range(y+1,4):
self.grid[i-1][x] = self.grid[i][x]
self.grid[3][x] = None
return moved
def __go_left_1(self, x, y):
moved = False
if x == 0:
return False
tile1 = self.grid[y][x]
if tile1 is not None:
tile2 = self.grid[y][x-1]
if tile2 is None:
self.grid[y][x-1] = tile1
self.grid[y][x] = None
moved = True
else:
if (not tile2.has_merged()) and tile2.get_value() == tile1.get_value():
self.grid[y][x-1] = tile1
self.grid[y][x] = None
tile1.inc_value()
self.score += tile1.get_tile_value()
self.merge_count += 1
moved = True
if moved:
for i in range(x+1, 4):
self.grid[y][i-1] = self.grid[y][i]
self.grid[y][3] = None
return moved
def __go_right_1(self, x, y):
moved = False
if x == 3:
return False
tile1 = self.grid[y][x]
if tile1 is not None:
tile2 = self.grid[y][x+1]
if tile2 is None:
self.grid[y][x+1] = tile1
self.grid[y][x] = None
moved = True
else:
if (not tile2.has_merged()) and tile2.get_value() == tile1.get_value():
self.grid[y][x+1] = tile1
self.grid[y][x] = None
tile1.inc_value()
self.score += tile1.get_tile_value()
self.merge_count += 1
moved = True
if moved:
for i in range(x-1, -1, -1):
self.grid[y][i+1] = self.grid[y][i]
self.grid[y][0] = None
return moved
def __go_down_1(self, x, y):
moved = False
if y == 3:
return False
tile1 = self.grid[y][x]
if tile1 is not None:
tile2 = self.grid[y+1][x]
if tile2 is None:
self.grid[y+1][x] = tile1
self.grid[y][x] = None
moved = True
else:
if (not tile2.has_merged()) and tile2.get_value() == tile1.get_value():
self.grid[y+1][x] = tile1
self.grid[y][x] = None
tile1.inc_value()
self.score += tile1.get_tile_value()
self.merge_count += 1
moved = True
if moved:
for i in range(y-1,-1,-1):
self.grid[i+1][x] = self.grid[i][x]
self.grid[0][x] = None
return moved
def __scooch_up(self):
moved = False
for x in [0, 1, 2, 3]:
target = -1
pointer = 0
while pointer < 4:
target += 1
if self.grid[target][x] is None:
while pointer < 4 and self.grid[pointer][x] is None:
pointer += 1
if pointer < 4:
self.grid[target][x] = self.grid[pointer][x]
self.grid[pointer][x] = None
moved = True
pointer += 1
pointer = target + 1
return moved
def __scooch_left(self):
moved = False
for row in self.grid:
target = -1
pointer = 0
while pointer < 4:
target += 1
if row[target] is None:
while pointer < 4 and row[pointer] is None:
pointer += 1
if pointer < 4:
row[target] = row[pointer]
row[pointer] = None
moved = True
pointer += 1
pointer = target + 1
return moved
def __scooch_right(self):
moved = False
for row in self.grid:
target = 4
pointer = 2
while pointer > 0:
target -= 1
if row[target] is None:
while pointer >= 0 and row[pointer] is None:
pointer -= 1
if pointer >= 0:
row[target] = row[pointer]
row[pointer] = None
moved = True
pointer -= 1
pointer = target-1
return moved
def __scooch_down(self):
moved = False
for x in [0, 1, 2, 3]:
target = 4
pointer = 2
while pointer > 0:
target -= 1
if self.grid[target][x] is None:
while pointer >= 0 and self.grid[pointer][x] is None:
pointer -= 1
if pointer >= 0:
self.grid[target][x] = self.grid[pointer][x]
self.grid[pointer][x] = None
moved = True
pointer -= 1
pointer = target-1
return moved
def is_empty(self, x, y):
return self.grid[y][x] is None
def is_board_full(self):
for row in self.grid:
for tile in row:
if tile is None:
return False
return True
def print_board(self):
"""Create a user friendly view of the Board."""
cell_padding = 8
board_string = ""
for row in self.grid:
board_string = board_string + ("-" * (((cell_padding + 1) * 4) + 1)) + "\n"
board_string = board_string + "|"
for tile in row:
if tile is None:
board_string = board_string + (" " * cell_padding) + "|"
else:
tile_value = tile.get_tile_value()
board_string = board_string + str("{: ^{padding}}".format(tile_value, padding=cell_padding)) + "|"
board_string = board_string + "\n"
board_string = board_string + ("-" * (((cell_padding + 1) * 4) + 1))
return board_string
def print_metrics(self):
"""Create user friendly summary of the metrics for the board."""
max_tile_value, max_row_idx, max_tile_idx = self.get_max_tile()
board_metrics = str("Score:{}, Merge count:{}, Max tile:{}, Max tile coords:({},{})".format(self.score, self.merge_count, max_tile_value, max_row_idx + 1, max_tile_idx + 1))
return board_metrics
def reset_tile_merges(self):
for row in self.grid:
for tile in row:
tile and tile.reset_merged()
def get_max_tile(self):
"""Returns the value of the maximum tile on the board, along with its coordinates."""
max_tile_value = 0
max_row_idx = None
max_tile_idx = None
for row_idx, row in enumerate(self.grid):
for tile_idx, tile in enumerate(row):
if tile is not None:
tile_value = tile.get_tile_value()
if tile_value > max_tile_value:
max_tile_value = tile_value
max_row_idx = row_idx
max_tile_idx = tile_idx
return max_tile_value, max_row_idx, max_tile_idx
def export_state(self):
grid = []
for row in self.grid:
new_row = []
for element in row:
if element is None:
new_row.append(None)
else:
new_row.append(element.get_value())
grid.append(new_row)
return grid