-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwozerofoureight.py
363 lines (303 loc) · 13 KB
/
twozerofoureight.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
import numpy as np
import numpy.random as nr
class TwoZeroFourEight():
def __init__(self, n):
# set the size of the grid
self.n = n
# score variable
self.score = 0
# create a matrix to store the values
self.game = np.zeros((n,n), dtype=np.int32)
# Store all empty tuples where next tile can be
# placed
self.empty_cells = dict()
# Starter tiles
self.starter_tiles = [2, 2, 2, 2, 2, 2, 2, 2, 2, 4]
# Add all the empty tiles to the dictionary
for i in range(n):
for j in range(n):
self.empty_cells[(i, j)] = 0
# Add two tiles to start with
self.insert_new_tile()
self.insert_new_tile()
def generate_random_tile(self):
# Choose an empty_cell randomly
# Generate a number from starter_tile list
keys = self.empty_cells.keys()
if len(keys) > 0:
index = nr.randint(len(keys))
location = list(keys)[index]
starter_index = nr.randint(len(self.starter_tiles))
value = self.starter_tiles[starter_index]
else:
if self.is_game_over():
# No moves are possible and the
# grid is locked
location = None
value = None
else:
# Some moves are possible,
# which will open up new tiles
location = None
value = 0
return location, value
def insert_new_tile(self):
location, value = self.generate_random_tile()
# Check for end of game
if location is None:
if value is None:
# Game over, No tile added
return True, False
elif value == 0:
# Game not over, No tile added
return False, False
else:
self.game[location] = value
self.empty_cells.pop(location, None)
# Game not over, tile added successfully
return False, True
def get_score(self):
return self.score
def get_tile_value(self, r, c):
return self.game[r, c]
def get_state(self):
return self.game
def slide_right(self, dryrun = False):
moves_possible = False
# First slide everything to the right as much as
# possible
for c in reversed(range(0, self.n - 1)):
for r in range(0, self.n):
self.move_tile_right(r, c, dryrun)
# Then start merging and sliding again
# Start at the penultimate column from right and
# check what can be combined.
for c in reversed(range(0, self.n - 1)):
for r in range(0, self.n):
# If the current cell is not empty
if self.game[r, c] > 0:
# Can we combine with the one to the right?
if self.game[r, c] == self.game[r, c + 1]:
# Yes we can
moves_possible = True
# Then combine and introduce an empty cell here.
if not dryrun:
self.merge_right(r, c)
# Now move the combined cell as far right
self.move_tile_right(r, c + 1)
else:
return moves_possible
else:
# Move the current cell as far right as possible
can_move_tile = self.move_tile_right(r, c, dryrun)
moves_possible = moves_possible or can_move_tile
return moves_possible
def merge_right(self, r, c):
self.game[r, c + 1] *= 2
self.score += self.game[r, c + 1]
self.game[r, c] = 0
# Push the key into empty_cells dictionary
self.empty_cells[(r,c)] = 0
def move_tile_right(self, r, c, dryrun = False):
moves_possible = False
# Find location of last empty cell in this direction
last_empty_cell = None
for cc in range(c + 1, self.n):
# Check if this is an empty cell
# and keep track of last empty cell
if self.game[r, cc] == 0:
last_empty_cell = (r, cc)
# Now move the cell to that location
if last_empty_cell is not None:
if not dryrun:
self.game[last_empty_cell] = self.game[r, c]
self.empty_cells.pop(last_empty_cell, None)
self.game[r, c] = 0
# Push the key into self.empty_cells dict
self.empty_cells[(r, c)] = 0
else:
moves_possible = True
return moves_possible
def slide_left(self, dryrun = False):
moves_possible = False
# First slide everything to the left as much as
# possible
for c in range(1, self.n):
for r in range(0, self.n):
self.move_tile_left(r, c, dryrun)
# Then start merging and sliding again
# Start at the penultimate column from left and
# check what can be combined.
for c in range(1, self.n):
for r in range(0, self.n):
# If the current cell is not empty
if self.game[r, c] > 0:
# Can we combine with the one to the left?
if self.game[r, c] == self.game[r, c - 1]:
# Yes we can
moves_possible = True
# Then combine and introduce an empty cell here.
if not dryrun:
self.merge_left(r, c)
# Now move the combined cell as far left
self.move_tile_left(r, c - 1)
else:
return moves_possible
else:
# Move the current cell as far left as possible
can_move_tile = self.move_tile_left(r, c, dryrun)
moves_possible = moves_possible or can_move_tile
return moves_possible
def merge_left(self, r, c):
self.game[r, c - 1] *= 2
self.score += self.game[r, c - 1]
self.game[r, c] = 0
# Push the key into empty_cells dictionary
self.empty_cells[(r,c)] = 0
def move_tile_left(self, r, c, dryrun = False):
moves_possible = False
# Find location of last empty cell in this direction
last_empty_cell = None
for cc in reversed(range(0, c)):
# Check if this is an empty cell
# and keep track of last empty cell
if self.game[r, cc] == 0:
last_empty_cell = (r, cc)
# Now move the cell to that location
if last_empty_cell is not None:
if not dryrun:
self.game[last_empty_cell] = self.game[r, c]
self.empty_cells.pop(last_empty_cell, None)
self.game[r, c] = 0
# Push the key into empty_cells dict
self.empty_cells[(r, c)] = 0
else:
moves_possible = True
return moves_possible
def slide_down(self, dryrun = False):
moves_possible = False
# First slide everything downwards as much as
# possible
for r in reversed(range(0, self.n - 1)):
for c in range(0, self.n):
self.move_tile_down(r, c, dryrun)
# Then start merging and sliding again
# Start at the penultimate column from bottom and
# check what can be combined.
for r in reversed(range(0, self.n - 1)):
for c in range(0, self.n):
# If the current cell is not empty
if self.game[r, c] > 0:
# Can we combine with the one below?
if self.game[r, c] == self.game[r + 1, c]:
# Yes we can
moves_possible = True
# Then combine and introduce an empty cell here.
if not dryrun:
self.merge_down(r, c)
# Now move the combined cell as far down
self.move_tile_down(r + 1, c)
else:
return moves_possible
else:
# Move the current cell as far down as possible
can_move_tile = self.move_tile_down(r, c, dryrun)
moves_possible = moves_possible or can_move_tile
return moves_possible
def merge_down(self, r, c):
self.game[r + 1, c] *= 2
self.score += self.game[r + 1, c]
self.game[r, c] = 0
# Push the key into empty_cells dictionary
self.empty_cells[(r,c)] = 0
def move_tile_down(self, r, c, dryrun = False):
moves_possible = False
# Find location of last empty cell in this direction
last_empty_cell = None
for rr in range(r + 1, self.n):
# Check if this is an empty cell
# and keep track of last empty cell
if self.game[rr, c] == 0:
last_empty_cell = (rr, c)
# Now move the cell to that location
if last_empty_cell is not None:
if not dryrun:
self.game[last_empty_cell] = self.game[r, c]
self.empty_cells.pop(last_empty_cell, None)
self.game[r, c] = 0
# Push the key into empty_cells dict
self.empty_cells[(r, c)] = 0
else:
moves_possible = True
return moves_possible
def slide_up(self, dryrun = False):
moves_possible = False
# First slide everything upwards as much as
# possible
for r in range(1, self.n):
for c in range(0, self.n):
self.move_tile_up(r, c, dryrun)
# Then start merging and sliding again
# Start at the second column from top and
# check what can be combined.
for r in range(1, self.n):
for c in range(0, self.n):
# If the current cell is not empty
if self.game[r, c] > 0:
# Can we combine with the one above?
if self.game[r, c] == self.game[r - 1, c]:
# Yes we can
moves_possible = True
# Then combine and introduce an empty cell here.
if not dryrun:
self.merge_up(r, c)
# Now move the combined cell as far down
self.move_tile_up(r - 1, c)
else:
return moves_possible
else:
# Move the current cell as far up as possible
can_move_tile = self.move_tile_up(r, c, dryrun)
moves_possible = moves_possible or can_move_tile
return moves_possible
def merge_up(self, r, c):
self.game[r - 1, c] *= 2
self.score += self.game[r - 1, c]
self.game[r, c] = 0
# Push the key into empty_cells dictionary
self.empty_cells[(r,c)] = 0
def move_tile_up(self, r, c, dryrun = False):
moves_possible = False
# Find location of last empty cell in this direction
last_empty_cell = None
for rr in reversed(range(0, r)):
# Check if this is an empty cell
# and keep track of last empty cell
if self.game[rr, c] == 0:
last_empty_cell = (rr, c)
# Now move the cell to that location
if last_empty_cell is not None:
if not dryrun:
self.game[last_empty_cell] = self.game[r, c]
self.empty_cells.pop(last_empty_cell, None)
self.game[r, c] = 0
# Push the key into empty_cells dict
self.empty_cells[(r, c)] = 0
else:
moves_possible = True
return moves_possible
def is_game_over(self):
if len(self.empty_cells.keys()) > 0:
return False
for i in range(self.n):
for j in range(self.n):
if self.game[i, j] == 2048:
return True
# This can also be done one at a time instead
# of checking all possibilities
moves_possible = []
moves_possible.append(self.slide_right(dryrun = True))
moves_possible.append(self.slide_down(dryrun = True))
moves_possible.append(self.slide_left(dryrun = True))
moves_possible.append(self.slide_up(dryrun = True))
return not any(moves_possible)