-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.py
255 lines (180 loc) · 7.74 KB
/
env.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
import numpy as np
import random
from copy import deepcopy
import cv2
def get_score(lines_cleared):
if lines_cleared == 1:
return 4
elif lines_cleared == 2:
return 10
elif lines_cleared == 3:
return 30
elif lines_cleared == 4:
return 120
return 0
class TetrisEnv:
actions = {
0: 'shift_left',
1: 'shift_right',
2: 'rotate_counterclockwise',
3: 'rotate_clockwise',
4: 'soft_drop',
5: 'hard_drop',
6: 'none'
}
pieces = {
'I': [(0, 0), (0, -1), (0, -2), (0, -3)],
'O': [(0, 0), (0, -1), (-1, 0), (-1, -1)],
'S': [(0, 0), (1, 0), (-1, -1), (0, -1)],
'L': [(0, 0), (1, 0), (0, -1), (0, -2)],
'T': [(0, 0), (-1, 0), (1, 0), (0, -1)],
'J': [(0, 0), (-1, 0), (0, -1), (0, -2)],
'Z': [(0, 0), (-1, 0), (0, -1), (1, -1)],
}
WIDTH = 10
HEIGHT = 20
NUM_ACTIONS = 7
def __init__(self):
self.n_frames = 0
self.score = 0
self.curr_shape = None # anchor shape
self.shape_in_play = None
self.att_num = 1
self.board = np.zeros((self.HEIGHT, self.WIDTH))
return
def _rotate_ccw_helper(self, piece):
return [(-y, x) for x, y in piece]
def _rotate_cw_helper(self, piece):
return [(y, -x) for x, y in piece]
def gen_new_shape(self):
shape = random.choice(list(self.pieces.keys()))
shape = self.pieces[shape]
return shape
def is_current_piece_dropped(self):
return
def is_colliding(self, shape_structure, shape_coords):
for i, j in shape_structure:
pixel_x, pixel_y = shape_coords[0] + i, shape_coords[0] + j
if pixel_y < 0:
continue
elif pixel_x < 0 or pixel_x >= self.board.shape[0] or pixel_y >= self.board.shape[1] or self.board[pixel_x, pixel_y] != 0:
return True
return False
def shift_left(self):
if self.is_colliding(self.shape_in_play, self.curr_shape):
return (self.shape_in_play, self.curr_shape)
return (self.shape_in_play, (self.curr_shape[0] - 1, self.curr_shape[1]))
def shift_right(self):
if self.is_colliding(self.shape_in_play, self.curr_shape):
return (self.shape_in_play, self.curr_shape)
return (self.shape_in_play, (self.curr_shape[0] + 1, self.curr_shape[1]))
def soft_drop(self):
if self.is_colliding(self.shape_in_play, self.curr_shape):
return (self.shape_in_play, self.curr_shape)
return (self.shape_in_play, (self.curr_shape[0], self.curr_shape[1] + 1))
def hard_drop(self):
while True:
_, new_coords = self.soft_drop()
if new_coords == self.curr_shape:
return self.shape_in_play, new_coords
self.shape_in_play = new_coords
def rotate_counterclockwise(self):
new_shape = self._rotate_ccw_helper(self.shape_in_play)
if self.is_colliding(new_shape, self.curr_shape):
return (self.shape_in_play, self.curr_shape)
return (new_shape, self.curr_shape)
def rotate_clockwise(self):
new_shape = self._rotate_cw_helper(self.shape_in_play)
if self.is_colliding(new_shape, self.curr_shape):
return (self.shape_in_play, self.curr_shape)
return (new_shape, self.curr_shape)
def spawn_shape(self):
self.curr_shape = (self.WIDTH // 2, 0)
self.shape_in_play = self.gen_new_shape()
def is_dropped(self):
return self.is_colliding(self.shape_in_play, (self.curr_shape[0], self.curr_shape[1] + 1))
def get_clear_lines(self):
clearable_lines = [np.all(self.board[:, i]) for i in range(self.board.shape[1])] # array dictating which lines are clearable
new_board = np.zeros((self.HEIGHT, self.WIDTH))
curr_line = self.board.shape[1] - 1
for i in [*range(self.board.shape[1])][::-1]:
if not clearable_lines[i]:
new_board[:, curr_line] = self.board[:, i]
j -= 1
self.score += get_score(clearable_lines.count(True))
self.board = new_board
return clearable_lines.count(True)
def action_str_to_function(self):
return {
'shift_left': self.shift_left,
'shift_right': self.shift_right,
'rotate_counterclockwise': self.rotate_counterclockwise,
'rotate_clockwise': self.rotate_clockwise,
'soft_drop': self.soft_drop,
'hard_drop': self.hard_drop,
'none': lambda: (self.shape_in_play, self.curr_shape)
}
def get_no_valid_actions(self):
no_valid_actions = 0
for key in self.actions.keys():
if self.action_str_to_function()[self.actions[key]]() == (self.shape_in_play, self.curr_shape):
continue
else:
no_valid_actions += 1
return no_valid_actions
def clear_board(self):
self.n_frames = 0
self.score = 0
self.gen_new_shape()
self.board = np.zeros((self.HEIGHT, self.WIDTH))
return self.board
def _set_curr_piece(self, on_top=False):
for (i, j) in self.shape_in_play:
pixel_x, pixel_y = self.curr_shape[0] + i, self.curr_shape[1] + j
if (pixel_x < self.WIDTH and pixel_x >= 0) and (pixel_y < self.HEIGHT and pixel_y >= 0):
self.board[int(self.anchor[0] + i), int(self.anchor[1] + j)] = on_top
def get_bumpiness(self):
bumpiness = 0
for i in range(self.board.shape[1] - 1):
bumpiness += abs(self.board[:, i].sum() - self.board[:, i + 1].sum())
return bumpiness
def step(self, action):
self.curr_shape = (int(self.curr_shape[0]), int(self.curr_shape[1]))
self.shape_in_play, self.curr_shape = self.action_str_to_function()[self.actions[action]]()
self.shape_in_play, self.curr_shape = self.soft_drop()
self.n_frames += 1
self._set_curr_piece()
reward = self.get_no_valid_actions()
done = False
if self.is_dropped():
self._set_curr_piece(on_top=True)
reward += get_score(self.get_clear_lines())
if np.any(self.board[0, :]):
self.clear_board()
self.att_num += 1
done = True
reward -= 300
else:
self.spawn_shape()
self._set_curr_piece(on_top=True)
state = deepcopy(self.board)
self._set_curr_piece()
return state, reward, done
def print_board(self):
self._set_curr_piece(on_top=True)
result_str = 'o' + '-' * self.WIDTH + 'o' + '\n'
result_str += ('\n'.join(['|' + ''.join(['X' if j else ' ' for j in i]) + '|' for i in self.board.T]) + '\n')
result_str += 'o' + '-' * self.WIDTH + 'o'
print(result_str)
def render(self):
self._set_curr_piece(on_top=True)
tmpboard = deepcopy(self.board)
for i in tmpboard:
for j in i:
if j == 1:
j = 0
else:
j = 255
return cv2.resize(tmpboard, (self.WIDTH * 20, self.HEIGHT * 20), interpolation=cv2.INTER_NEAREST)
def close():
return