-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathengine.py
196 lines (155 loc) · 5.94 KB
/
engine.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
#from __future__ import print_function
import numpy as np
import random
shapes = {
'T': [(0, 0), (-1, 0), (1, 0), (0, -1)],
'J': [(0, 0), (-1, 0), (0, -1), (0, -2)],
'L': [(0, 0), (1, 0), (0, -1), (0, -2)],
'Z': [(0, 0), (-1, 0), (0, -1), (1, -1)],
'S': [(0, 0), (-1, -1), (0, -1), (1, 0)],
'I': [(0, 0), (0, -1), (0, -2), (0, -3)],
'O': [(0, 0), (0, -1), (-1, 0), (-1, -1)],
}
shape_names = ['T', 'J', 'L', 'Z', 'S', 'I', 'O']
def rotated(shape, cclk=False):
if cclk:
return [(-j, i) for i, j in shape]
else:
return [(j, -i) for i, j in shape]
def is_occupied(shape, anchor, board):
for i, j in shape:
x, y = anchor[0] + i, anchor[1] + j
if y < 0:
continue
if x < 0 or x >= board.shape[0] or y >= board.shape[1] or board[x, y]:
return True
return False
def left(shape, anchor, board):
new_anchor = (anchor[0] - 1, anchor[1])
return (shape, anchor) if is_occupied(shape, new_anchor, board) else (shape, new_anchor)
def right(shape, anchor, board):
new_anchor = (anchor[0] + 1, anchor[1])
return (shape, anchor) if is_occupied(shape, new_anchor, board) else (shape, new_anchor)
def soft_drop(shape, anchor, board):
new_anchor = (anchor[0], anchor[1] + 1)
return (shape, anchor) if is_occupied(shape, new_anchor, board) else (shape, new_anchor)
def hard_drop(shape, anchor, board):
while True:
_, anchor_new = soft_drop(shape, anchor, board)
if anchor_new == anchor:
return shape, anchor_new
anchor = anchor_new
def rotate_left(shape, anchor, board):
new_shape = rotated(shape, cclk=False)
return (shape, anchor) if is_occupied(new_shape, anchor, board) else (new_shape, anchor)
def rotate_right(shape, anchor, board):
new_shape = rotated(shape, cclk=True)
return (shape, anchor) if is_occupied(new_shape, anchor, board) else (new_shape, anchor)
def idle(shape, anchor, board):
return (shape, anchor)
class TetrisEngine:
def __init__(self, width, height):
self.width = width
self.height = height
self.board = np.zeros(shape=(width, height), dtype=np.bool)
# actions are triggered by letters
self.value_action_map = {
0: left,
1: right,
2: hard_drop,
3: soft_drop,
4: rotate_left,
5: rotate_right,
6: idle,
}
self.action_value_map = dict([(j, i) for i, j in self.value_action_map.items()])
self.nb_actions = len(self.value_action_map)
# for running the engine
self.time = -1
self.score = -1
self.anchor = None
self.shape = None
self.n_deaths = 0
# used for generating shapes
self._shape_counts = [0] * len(shapes)
# clear after initializing
self.clear()
def _choose_shape(self):
maxm = max(self._shape_counts)
m = [5 + maxm - x for x in self._shape_counts]
r = random.randint(1, sum(m))
for i, n in enumerate(m):
r -= n
if r <= 0:
self._shape_counts[i] += 1
return shapes[shape_names[i]]
def _new_piece(self):
# Place randomly on x-axis with 2 tiles padding
#x = int((self.width/2+1) * np.random.rand(1,1)[0,0]) + 2
self.anchor = (self.width / 2, 0)
#self.anchor = (x, 0)
self.shape = self._choose_shape()
def _has_dropped(self):
return is_occupied(self.shape, (self.anchor[0], self.anchor[1] + 1), self.board)
def _clear_lines(self):
can_clear = [np.all(self.board[:, i]) for i in range(self.height)]
new_board = np.zeros_like(self.board)
j = self.height - 1
for i in range(self.height - 1, -1, -1):
if not can_clear[i]:
new_board[:, j] = self.board[:, i]
j -= 1
self.score += sum(can_clear)
self.board = new_board
return sum(can_clear)
def valid_action_count(self):
valid_action_sum = 0
for value, fn in self.value_action_map.items():
# If they're equal, it is not a valid action
if fn(self.shape, self.anchor, self.board) != (self.shape, self.anchor):
valid_action_sum += 1
return valid_action_sum
def step(self, action):
self.anchor = (int(self.anchor[0]), int(self.anchor[1]))
self.shape, self.anchor = self.value_action_map[action](self.shape, self.anchor, self.board)
# Drop each step
self.shape, self.anchor = soft_drop(self.shape, self.anchor, self.board)
# Update time and reward
self.time += 1
reward = self.valid_action_count()
#reward = random.randint(0, 0)
reward = 1
done = False
if self._has_dropped():
self._set_piece(True)
reward += 100 * self._clear_lines()
if np.any(self.board[:, 0]):
self.clear()
self.n_deaths += 1
done = True
reward = -100
else:
self._new_piece()
self._set_piece(True)
#state = np.copy(self.board)
state = self.board
self._set_piece(False)
return state, reward, done
def clear(self):
self.time = 0
self.score = 0
self._new_piece()
self.board = np.zeros_like(self.board)
return self.board
def _set_piece(self, on=False):
for i, j in self.shape:
x, y = i + self.anchor[0], j + self.anchor[1]
if x < self.width and x >= 0 and y < self.height and y >= 0:
self.board[int(self.anchor[0] + i), int(self.anchor[1] + j)] = on
def __repr__(self):
self._set_piece(True)
s = 'o' + '-' * self.width + 'o\n'
s += '\n'.join(['|' + ''.join(['X' if j else ' ' for j in i]) + '|' for i in self.board.T])
s += '\no' + '-' * self.width + 'o'
self._set_piece(False)
return s