-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.py
More file actions
298 lines (272 loc) · 9.97 KB
/
environment.py
File metadata and controls
298 lines (272 loc) · 9.97 KB
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
from state import State
from constants import *
import random
class Environment():
def __init__(self):
self.board = [[0, 0, 0, 0] for _ in range(4)]
self.init_state = State(self, self.board)
self.seed = 4321
random.seed(self.seed)
self.spawn_tile(self.init_state, True)
self.spawn_tile(self.init_state, True)
def get_init_state(self):
return self.init_state
def perform_action(self, state, action):
"""Perform an action on the board and return the new state
Args:
state (State): State to perform action on
action (int): action to perform
Raises:
Exception: Invalid action
Returns:
_type_: _description_
"""
score = 0
deepcopy = state.deepcopy()
if action == RIGHT:
for row, _ in enumerate(state.board):
# Move all values to the right
_move_right(state.board[row])
# Merge values right
inc_score = _merge_right(state.board[row])
score += inc_score
# Move all values to the right
_move_right(state.board[row])
elif action == LEFT:
for row, _ in enumerate(state.board):
# Move all values to the left
_move_left(state.board[row])
# Merge values left
inc_score = _merge_left(state.board[row])
score += inc_score
# Move all values to the left
_move_left(state.board[row])
elif action == UP:
# Transpose the board
state.board = _transpose_2d_list(state.board)
for row, _ in enumerate(state.board):
# Move all values to the left
_move_left(state.board[row])
# Merge values left
inc_score = _merge_left(state.board[row])
score += inc_score
# Move all values to the left
_move_left(state.board[row])
# Transpose the board back
state.board = _transpose_2d_list(state.board)
elif action == DOWN:
# Transpose the board
state.board = _transpose_2d_list(state.board)
for row, _ in enumerate(state.board):
# Move all values to the right
_move_right(state.board[row])
# Merge values right
inc_score = _merge_right(state.board[row])
score += inc_score
# Move all values to the right
_move_right(state.board[row])
# Transpose the board back
state.board = _transpose_2d_list(state.board)
else:
raise Exception("Invalid action")
new_state = State(self, state.board)
valid = deepcopy != new_state
if valid:
self.spawn_tile(state)
return (valid, new_state, score)
def apply_action(self, state, action):
"""Perform an action on the board and return the new state
ONLY DIFFERENCE IS THAT THIS DOES NOT SPAWN A NEW TILE
-- Used for getting the next state for the agent
Args:
state (State): State to perform action on
action (int): action to perform
Raises:
Exception: Invalid action
Returns:
_type_: _description_
"""
score = 0
deepcopy = state.deepcopy()
if action == RIGHT:
for row, _ in enumerate(state.board):
# Move all values to the right
_move_right(state.board[row])
# Merge values right
inc_score = _merge_right(state.board[row])
score += inc_score
# Move all values to the right
_move_right(state.board[row])
elif action == LEFT:
for row, _ in enumerate(state.board):
# Move all values to the left
_move_left(state.board[row])
# Merge values left
inc_score = _merge_left(state.board[row])
score += inc_score
# Move all values to the left
_move_left(state.board[row])
elif action == UP:
# Transpose the board
state.board = _transpose_2d_list(state.board)
for row, _ in enumerate(state.board):
# Move all values to the left
_move_left(state.board[row])
# Merge values left
inc_score = _merge_left(state.board[row])
score += inc_score
# Move all values to the left
_move_left(state.board[row])
# Transpose the board back
state.board = _transpose_2d_list(state.board)
elif action == DOWN:
# Transpose the board
state.board = _transpose_2d_list(state.board)
for row, _ in enumerate(state.board):
# Move all values to the right
_move_right(state.board[row])
# Merge values right
inc_score = _merge_right(state.board[row])
score += inc_score
# Move all values to the right
_move_right(state.board[row])
# Transpose the board back
state.board = _transpose_2d_list(state.board)
else:
raise Exception("Invalid action")
new_state = State(self, state.board)
valid = deepcopy != new_state
return (valid, new_state, score)
def is_solved(self, state):
for x in range(4):
for y in range(4):
if state.board[x][y] == 512:
return True
return False
def is_terminal(self, state):
if self.is_solved(state):
return True
else:
for tile, neighbours in matrix_neighbours(state.board):
if tile == 0:
return False
for neighbour in neighbours:
if tile == neighbour:
return False
return True
def get_empty_spaces(self, state):
# Get a list of all empty spaces
empty_spaces = []
for i in range(4):
for j in range(4):
if state.board[i][j] == 0:
empty_spaces.append((i, j))
return empty_spaces
def get_possible_spawns(self, state):
possible_spawns = []
empty_spaces = self.get_empty_spaces(state)
for space in empty_spaces:
for val in [2, 4]:
temp = state.deepcopy()
temp.board[space[0]][space[1]] = val
possible_spawns.append(temp)
return possible_spawns
def spawn_tile(self, state, new_game=False):
"""Spawn a new tile in a random empty space"""
# Get a list of all empty spaces
empty_spaces = self.get_empty_spaces(state)
# Select a random empty space
new_tile = random.choice(empty_spaces)
if new_game:
# Spawn a 2 in the empty space
state.board[new_tile[0]][new_tile[1]] = 2
else:
# Spawn a 2 or 4 in the empty space
state.board[new_tile[0]][new_tile[1]] = random.choice([2, 4])
def render(self, state):
"""Render 2D matrix to terminal"""
for row in state.board:
print(row)
print("-----------------")
def _transpose_2d_list(matrix):
return [list(row) for row in zip(*matrix)]
def _move_left(row):
"""Move all values to the left"""
for i in range(1, 4):
if row[i]:
for j in range(i, 0, -1):
if not row[j - 1]:
row[j - 1] = row[j]
row[j] = 0
else:
break
return row
def _move_right(row):
"""Move all values to the right"""
for i in range(2, -1, -1):
if row[i]:
for j in range(i, 3):
if not row[j + 1]:
row[j + 1] = row[j]
row[j] = 0
else:
break
return row
def _merge_left(row):
"""Merge values from right to left"""
score = 0
if row[0] == row[1]:
row[0] *= 2
row[1] = 0
score += row[0]
if row[1] == row[2]:
row[1] *= 2
row[2] = 0
score += row[1]
if row[2] == row[3]:
row[2] *= 2
row[3] = 0
score += row[2]
return score
def _merge_right(row):
"""Merge values from left to right"""
score = 0
if row[2] == row[3]:
row[3] *= 2
row[2] = 0
score += row[3]
if row[1] == row[2]:
row[2] *= 2
row[1] = 0
score += row[2]
if row[0] == row[1]:
row[1] *= 2
row[0] = 0
score += row[1]
return score
def matrix_neighbours(matrix):
"""iterate over elements of 4x4 matrix and get its neighbours"""
for i in range(4):
for j in range(4):
if i == 0:
if j == 0:
neighbours = [matrix[i][j+1], matrix[i+1][j]]
elif j == 3:
neighbours = [matrix[i][j-1], matrix[i+1][j]]
else:
neighbours = [matrix[i][j-1], matrix[i][j+1], matrix[i+1][j]]
elif i == 3:
if j == 0:
neighbours = [matrix[i][j+1], matrix[i-1][j]]
elif j == 3:
neighbours = [matrix[i][j-1], matrix[i-1][j]]
else:
neighbours = [matrix[i][j-1], matrix[i][j+1], matrix[i-1][j]]
else:
if j == 0:
neighbours = [matrix[i][j+1], matrix[i-1][j], matrix[i+1][j]]
elif j == 3:
neighbours = [matrix[i][j-1], matrix[i-1][j], matrix[i+1][j]]
else:
neighbours = [matrix[i][j-1], matrix[i][j+1], matrix[i-1][j], matrix[i+1][j]]
yield matrix[i][j], neighbours