-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
300 lines (237 loc) · 9.1 KB
/
game.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
import player
import cv2
import numpy as np
from PIL import ImageGrab
import pywinctl as pwc
import time
import subprocess
import torch
class Game:
def __init__(self):
print("Game started")
self.Player = player.Player("Les Hackathon")
self.board = np.zeros((22, 12))
self.piece_id = 0
self.piece = [[0,0,0],[0,0,0],[0,0,0]]
self.score = 0
self.holes = 0
self.gameover = False
self.img = 0
self.tetrominoes = 0
self.cleared_lines = 0
def reset(self):
self.board = np.zeros((22, 12))
self.piece_id = 0
self.score = 0
self.holes = 0
self.gameover = False
self.tetrominoes = 0
self.cleared_lines = 0
self.piece = [[0,0,0],[0,0,0],[0,0,0]]
self.img = 0
def getPiece(self):
piece = np.zeros((3, 3))
# get type of piece
for i in range(3):
for j in range(3):
piece[i][j] = int(self.board[i][j + 4])
# print(piece)
all_piece = {
"s": [[0,1,1],
[1,1,0]],
"z": [[1,1,0],
[0,1,1]],
"j": [[1,0,0],
[1,1,1]],
"l": [[0,0,1],
[1,1,1]],
"o": [[1,1],
[1,1]],
"i": [[1,1,1,1]],
"t": [[0,1,0],
[1,1,1]]
}
detect_piece = {
"s": [[0,1,1],[1,1,0],[0,0,0]],
"z": [[1,1,0],[0,1,1],[0,0,0]],
"j": [[1,0,0],[1,1,1],[0,0,0]],
"l": [[0,0,1],[1,1,1],[0,0,0]],
"o": [[0,1,1],[0,1,1],[0,0,0]],
"i": [[0,0,0],[1,1,1],[0,0,0]],
"t": [[0,1,0],[1,1,1],[0,0,0]]
}
for key, value in detect_piece.items():
if (piece == value).all():
self.piece_id = key
self.piece = all_piece[key]
# replace 2 upper line of matrix with 0
for i in range(12):
self.board[0][i] = 0
for i in range(12):
self.board[1][i] = 0
def detectBoard(self, img):
self.img = img
self.board = np.zeros((22, 12))
rows, cols, _ = img.shape
virtual_board = np.zeros((rows, cols, 3), dtype=np.uint8)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edges = cv2.Canny(blurred, 50, 150)
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
board_contour = max(contours, key=cv2.contourArea)
cv2.drawContours(img, [board_contour], -1, (0, 255, 0), 2)
# cv2.drawContours(virtual_board, [board_contour], -1, (0, 255, 0), 2)
(board_x, board_y, board_w, board_h) = cv2.boundingRect(board_contour)
# create cells
board_w -= 535
board_x += 535
board_y += 7
block_width = int(board_w / 12)
block_height = int(board_h / 22)
# detect pieces
tetro = {
"j": [249, 154, 45],
"l": [70, 188, 49],
"o": [142, 142, 142],
"i": [0, 0, 255],
"s": [26,163,255],
"t": [189, 74, 197],
"z": [0, 91, 255]
}
for key in tetro:
color = tetro[key]
color = np.array(color)
mask = cv2.inRange(img, np.add(color, -1), np.add(color, 1))
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
(x, y, w, h) = cv2.boundingRect(cnt)
# cv2.rectangle(virtual_board, (x, y), (x + w, y + h), (100, 100, 0), 2)
cv2.rectangle(img, (x, y), (x + w, y + h), (100, 100, 0), 2)
for n in range(22):
for n2 in range(12):
block_x = n2 * block_width
block_y = n * block_height
# cv2.rectangle(virtual_board, (block_x + board_x, block_y + board_y), (block_x + block_width + board_x, block_y + block_height + board_y), (0, 0, 0), 1)
cv2.rectangle(img, (block_x + board_x, block_y + board_y), (block_x + block_width + board_x, block_y + block_height + board_y), (0, 0, 0), 1)
if board_x + block_x <= x < board_x + block_x + block_width and board_y + block_y <= y < board_y + block_y + block_height:
self.board[n][n2] = 1
def check_collision(self, piece, pos):
future_y = pos["y"] + 1
for y in range(len(piece)):
for x in range(len(piece[y])):
if future_y + y > 21 or self.board[future_y + y][pos["x"] + x] and piece[y][x]:
return True
return False
def getNextState(self):
def putOnMatrix(piece, pos):
board = np.array(self.board)
for y in range(len(piece)):
for x in range(len(piece[y])):
if piece[y][x]:
board[pos["y"] + y][pos["x"] + x] = 1
return board
def getMaxLenOfForm(form):
print(form.shape[1])
return form.shape[1]
states = {}
board = np.array(self.board)
curr_piece = np.array(self.piece)
n_rot = 4
for i in range(n_rot):
# size of piece
valid_xs = 12 - getMaxLenOfForm(curr_piece)
for x in range(valid_xs + 1):
pos = {"x": x, "y": 0}
while not self.check_collision(curr_piece, pos):
pos["y"] += 1
board = putOnMatrix(curr_piece, {"x": pos["x"], "y": pos["y"]})
states[(x, i)] = self.getStateProp(board)
curr_piece = np.rot90(curr_piece, 1)
return states
def getHoles(self, board):
num_holes = 0
for col in zip(*board):
row = 0
while row < 22 and col[row] == 0:
row += 1
count = 0
while row < 22:
if(col[row] == 0):
count += 1
row += 1
num_holes += count
return num_holes
def getBumpAndHeight(self):
board = np.array(self.board)
# exit(0)
mask = board != 0
invert_heights = np.where(mask.any(axis=0), np.argmax(mask, axis=0), 22)
h = 22 - invert_heights
total_height = np.sum(h)
# print(board, total_height)
currs = h[:-1]
next = h[1:]
diff = np.abs(currs - next)
bumpiness = np.sum(diff)
# print("bumpiness: ",bumpiness)
# print("height: ",total_height)
return bumpiness, total_height
def getStateProp(self, board):
def nbrOfFullLines(board):
lines = 0
for i in range(22):
if np.all(board[i] == 1):
lines += 1
return lines
bump, height = self.getBumpAndHeight()
holes = self.getHoles(board)
full_line = nbrOfFullLines(board)
print(full_line, holes, bump, height)
return torch.FloatTensor([full_line * 3, holes, bump * 2, height * 2])
def checkGameOver(self):
template = cv2.imread("lose.png")
res = cv2.matchTemplate(self.img, template, cv2.TM_CCOEFF_NORMED)
if np.max(res) > 0.9:
print("Game Over")
return True
else:
return False
def step(self, action):
def getMaxLenOfForm(form):
return form.shape[1]
def putOnMatrix(piece, pos):
board = np.array(self.board)
for y in range(len(piece)):
for x in range(len(piece[y])):
if piece[y][x]:
board[pos["y"] + y][pos["x"] + x] = 1
return board
def nbrOfFullLines(board):
lines = 0
for i in range(22):
if np.all(board[i] == 1):
lines += 1
return lines
f_pos, rot = action
pos = {"x": 6, "y": 0}
for _ in range(rot):
self.Player.up()
self.piece = np.rot90(self.piece)
for _ in range(6):
self.Player.left()
for _ in range(f_pos):
self.Player.right()
for _ in range(22):
self.Player.down()
# self.Player.space()
while not self.check_collision(self.piece, pos):
pos["y"] += 1
board = putOnMatrix(self.piece, {"x": pos["x"], "y": pos["y"]})
full_lines = nbrOfFullLines(board)
self.score += (1 + (full_lines ** 2) * 12)
self.cleared_lines += full_lines
self.tetrominoes += 1
if(self.checkGameOver()):
self.score -= 20
self.gameover = True
return self.score, self.gameover