-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathplay.py
226 lines (189 loc) · 6.22 KB
/
play.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
"""
Play the 2048 game rendered by PyGame library.
Each game is logged - pairs of (game state, action) are saved.
"""
import pygame
import sys, os
from pygame.locals import *
from board_gym import GymBoard
from util import Step, Game, encode_action
import pickle
from pprint import pprint
def draw_text(surface, text, color, rect, font, aa=False, bkg=None):
"""
Draw some text into an area of a surface.
Automatically wraps words.
Returns any text that didn't get blitted.
"""
rect = Rect(rect)
y = rect.top
line_spacing = -2
# get the height of the font
font_height = font.size("Tg")[1]
while text:
i = 1
# determine if the row of text will be outside our area
if y + font_height > rect.bottom:
break
# determine maximum width of line
while font.size(text[:i])[0] < rect.width and i < len(text):
i += 1
# if we've wrapped the text, then adjust the wrap to the last word
if i < len(text):
i = text.rfind(" ", 0, i) + 1
# render the line and blit it to the surface
if bkg:
image = font.render(text[:i], 1, color, bkg)
image.set_colorkey(bkg)
else:
image = font.render(text[:i], aa, color)
surface.blit(image, (rect.left, y))
y += font_height + line_spacing
# remove the text we just blitted
text = text[i:]
return text
def render_board(board):
screen.fill(COLOR_MAP["background"])
for row in range(board.shape[0]):
for column in range(board.shape[1]):
tile_value = board.matrix[row, column]
if tile_value in COLOR_MAP:
color = COLOR_MAP[tile_value]
#elif (row, column) == board.last_random_tile_index:
# color = YELLOW
else:
color = COLOR_MAP["super"]
pygame.draw.rect(screen,
color,
[(T_MARGIN + T_WIDTH) * column + T_MARGIN,
(T_MARGIN + T_HEIGHT) * row + T_MARGIN,
T_WIDTH,
T_HEIGHT])
if (row, column) == board.last_random_tile_index:
text_color = RED
else:
text_color = COLOR_MAP["text"]
if tile_value:
text = str(tile_value)
else:
text = ""
draw_text(screen,
text,
text_color,
[(T_MARGIN + T_WIDTH) * column + T_WIDTH / 2,
(T_MARGIN + T_HEIGHT) * row + T_HEIGHT / 2,
T_WIDTH,
T_HEIGHT],
FONT_TILE,
aa=True)
draw_text(screen,
"SCORE: " + str(board.score),
BLACK,
[50,
HEIGHT - 50,
200,
50],
FONT_SCORE,
aa=True)
def play(board, save_normalized_matrix=True):
"""
Parameters
----------
board : numpy.array
save_normalized_matrix : bool
Whether to save normalized (log2 transformed) or original matrix.
Returns
-------
collections.namedtuple
Game with recorded steps.
"""
steps = []
render_board(board)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key in POSSIBLE_ACTIONS:
matrix = board.normalized_matrix if save_normalized_matrix else board.matrix
action = POSSIBLE_ACTIONS[event.key]
moved = board.move(action)
if moved:
print()
print(board.matrix)
print("SCORE:", board.score, "\tSTEP:", board.n_steps_valid, "\tHIGHEST VALUE:", board.highest_value)
steps.append(Step(matrix=matrix, action=action, action_encoded=encode_action(action)))
render_board(board)
if board.is_gameover():
print("GAME OVER!")
return Game(steps=steps, score=board.score, random_seed=board.random_seed, is_gameover=True)
else:
print("\nCannot move to this direction!")
elif event.key == pygame.K_q:
screen.fill(BLACK)
return Game(steps=steps, random_seed=board.random_seed, is_gameover=False)
elif event.key == pygame.K_p:
screen.fill(BLACK)
return "quit"
clock.tick(60)
pygame.display.flip()
### PyGame options
os.environ["SDL_VIDEO_CENTERED"] = "1"
pygame.init()
WIDTH = 700
HEIGHT = 700
WINDOW_SIZE = (WIDTH, HEIGHT)
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("2048")
FONT_TILE = pygame.font.SysFont("Arial", 40, bold=True)
FONT_SCORE = pygame.font.SysFont("Arial", 24, bold=True)
clock = pygame.time.Clock()
# Tile sizes
T_WIDTH = 150
T_HEIGHT = 150
T_MARGIN = 10
### Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
COLOR_MAP = {
"text": (119, 110, 101),
"background": (187, 173, 160),
0: (238, 228, 218),
2: (238, 228, 218),
4: (237, 224, 200),
8: (242, 177, 121),
16: (245, 149, 99),
32: (246, 124, 95),
64: (246, 94, 59),
128: (237, 207, 114),
256: (237, 204, 97),
512: (237, 200, 80),
1024: (237, 197, 63),
2048: (237, 194, 46),
"super": (60, 58, 50)
}
### Game logging
games_file = "data/games_01.pickle"
games = []
n_games = 3
# Map keys to 2048 board actions.
POSSIBLE_ACTIONS = {
pygame.K_UP: 0,
pygame.K_DOWN: 1,
pygame.K_LEFT: 2,
pygame.K_RIGHT: 3
}
for i in range(n_games):
board = GymBoard()
print(board.matrix)
game = play(board)
if game == "quit":
break
#pprint(game)
games.append(game)
with open(games_file, mode="bw") as f:
pickle.dump(games, f)