-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_engine.py
207 lines (178 loc) · 6.72 KB
/
game_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
197
198
199
200
201
202
203
204
205
206
207
import os.path
import pygame
import numpy as np
import random
import pickle as pkl
def generate_board():
# board = np.array([
# [0, 2048, 32, 32],
# [2048, 2048, 32, 32],
# [16, 16, 512, 512],
# [16, 16, 512, 512]
# ])
board = np.array([
[2, 4, 2, 8],
[16, 32, 64, 128],
[256, 512, 1024, 2048],
[4096, 8192, 16384, 32768]
])
return board
class Game2048:
def __init__(self):
self.size = 4
self.board_hist = []
self.board = np.zeros((self.size, self.size), dtype=int)
self.score_hist = []
self.score = 0
self.undo_counts = 0
self.reset()
if os.path.exists("save.pkl"):
with open("save.pkl", "rb") as f:
self.board, self.score, self.undo_counts = pkl.load(f)
def reset(self):
self.board = np.zeros((self.size, self.size), dtype=int)
self.score = 0
self.undo_counts = 0
self.add_tile()
self.add_tile()
return self.board
def add_tile(self):
empty_positions = list(zip(*np.where(self.board == 0)))
if empty_positions:
row, col = empty_positions[random.choice(range(len(empty_positions)))]
self.board[row, col] = 2 if random.random() < 0.9 else 4
def move(self, direction):
old_board = self.board.copy()
if direction == 'left':
self.board, score = self.slide_left(self.board)
elif direction == 'up':
transposed_board, score = self.slide_left(self.board.T)
self.board = transposed_board.T
elif direction == 'right':
flipped_board, score = self.slide_left(np.fliplr(self.board))
self.board = np.fliplr(flipped_board)
elif direction == 'down':
flipped_transposed_board, score = self.slide_left(np.fliplr(self.board.T))
self.board = np.fliplr(flipped_transposed_board).T
if (self.board != old_board).sum() > 0:
self.board_hist.append(old_board)
self.score_hist.append(self.score)
self.add_tile()
self.score += score
print(self.score)
def slide_left(self, board):
new_board = np.zeros_like(board)
score = 0
for i in range(self.size):
tiles = board[i][board[i] != 0]
new_row = []
skip = False
for j in range(len(tiles)):
if skip:
skip = False
continue
if j + 1 < len(tiles) and tiles[j] == tiles[j + 1]:
new_row.append(2 * tiles[j])
score += 2 * tiles[j]
skip = True
else:
new_row.append(tiles[j])
new_board[i, :len(new_row)] = new_row
return new_board, score
def is_game_over(self):
if np.any(self.board == 0):
return False
for i in range(self.size):
for j in range(self.size - 1):
if self.board[i, j] == self.board[i, j + 1] or self.board[j, i] == self.board[j + 1, i]:
return False
return True
def undo(self):
if len(self.score_hist):
self.score = self.score_hist[-1]
self.score_hist = self.score_hist[:-1]
self.board = self.board_hist[-1]
self.board_hist = self.board_hist[:-1]
self.undo_counts += 1
def save(self):
save_data = [self.board, self.score, self.undo_counts]
with open("save.pkl", "wb") as f:
pkl.dump(save_data, f)
class Game2048GUI:
def __init__(self, game):
self.game = game
self.size = game.size
self.width = 1200
self.height = 1200
self.tile_size = self.width // self.size
self.colors = {
0: (205, 193, 180),
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),
4096: (0, 128, 128), # Green-blue color
8192: (0, 100, 128), # Green-blue color
16384: (0, 76, 153), # Green-blue color
32768: (0, 51, 102), # Green-blue color
65536: (0, 25, 51) # Green-blue color
}
pygame.init()
self.screen = pygame.display.set_mode((self.width, self.height))
pygame.display.set_caption("2048")
def draw_board(self):
self.screen.fill((187, 173, 160))
for row in range(self.size):
for col in range(self.size):
value = self.game.board[row][col]
color = self.colors.get(value, (60, 58, 50))
rect = pygame.Rect(col * self.tile_size, row * self.tile_size, self.tile_size, self.tile_size)
pygame.draw.rect(self.screen, color, rect)
if value != 0:
font = pygame.font.Font(None, 72)
text = font.render(str(value), True, (119, 110, 101))
text_rect = text.get_rect(center=rect.center)
self.screen.blit(text, text_rect)
def run(self):
clock = pygame.time.Clock()
running = True
while running:
pygame.display.set_caption(f"2048, Score: {game.score}, Undo: {game.undo_counts}")
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.game.move('left')
elif event.key == pygame.K_RIGHT:
self.game.move('right')
elif event.key == pygame.K_UP:
self.game.move('up')
elif event.key == pygame.K_DOWN:
self.game.move('down')
elif event.key == pygame.K_SPACE:
self.game.undo()
elif event.key == pygame.K_ESCAPE:
running = False
elif event.key == pygame.K_r:
game.reset()
elif event.key == pygame.K_s:
game.save()
if self.game.is_game_over():
# running = False
print("Game Over")
self.draw_board()
pygame.display.flip()
clock.tick(30)
pygame.quit()
if __name__ == "__main__":
game = Game2048()
gui = Game2048GUI(game)
gui.run()