-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.py
365 lines (328 loc) · 10.5 KB
/
logic.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
"""
This module contains the logic of the game.
"""
import random
import json
import gamelib
from constants import *
class Game:
"""
This class represents the game. It contains the game board and the game score and best score.
"""
def __init__(self):
"""
Initializes the game board with a all the cells empty and a random cell
with a value from the possible initial values.
"""
self.board = [[EMPTY for j in range(COLUMNS)] for i in range(ROWS)]
self.generate_initial_cells()
self.score = INITIAL_SCORE
self.best = get_best_score()
def generate_initial_cells(self):
"""
Generates the initial cells of the game board.
"""
self.insert_random_new_cell()
self.insert_random_new_cell()
def reset(self):
"""
Resets the game board with a all the cells empty and a random cell
with a value from the possible initial values.
"""
self.store_best_score()
self.board = [[EMPTY for j in range(COLUMNS)] for i in range(ROWS)]
self.generate_initial_cells()
self.score = INITIAL_SCORE
self.best = get_best_score()
def store_best_score(self):
"""
Stores the best score in the file.
"""
with open("utils/best_score.json", "w", encoding="utf-8") as file:
json.dump({"best_score": self.best}, file)
def show(self):
"""
Shows the game board on the screen.
"""
draw_score(self.score, self.best)
draw_board(self.board)
def new_game_button_clicked(self, x_pos, y_pos):
"""
Returns True if the new game button was clicked, False otherwise.
"""
return (
NEW_GAME_BUTTON[0] <= x_pos <= NEW_GAME_BUTTON[2]
and NEW_GAME_BUTTON[1] <= y_pos <= NEW_GAME_BUTTON[3]
)
def get_random_empty_cell(self):
"""
Returns a random empty cell from the board. If there are no empty cells, returns False.
"""
empty_cells = [
(i, j)
for i in range(ROWS)
for j in range(COLUMNS)
if self.board[i][j] == EMPTY
]
return random.choice(empty_cells) if empty_cells else False
def insert_random_new_cell(self):
"""
Inserts a new cell with a value from the possible initial values
in a random empty cell of the board.
"""
row, column = self.get_random_empty_cell()
self.board[row][column] = random.choice(POSSIBLE_INITIAL_VALUES)
def combine_row(self, row_index):
"""
Combines the numbers of a line that can be combined and moves them to the left.
If there are no combinable numbers, returns the original line.
"""
row = self.board[row_index]
combinable_numbers = [number for number in row if number != EMPTY]
if combinable_numbers:
result = []
i = 0
while i < len(combinable_numbers):
if (
i < len(combinable_numbers) - 1
and combinable_numbers[i] == combinable_numbers[i + 1]
):
result.append(combinable_numbers[i] * 2)
self.score += combinable_numbers[i] * 2
if self.score > self.best:
self.best = self.score
i += 2
else:
result.append(combinable_numbers[i])
i += 1
empty_cells_to_add = len(row) - len(result)
result += [EMPTY] * empty_cells_to_add
self.board[row_index] = result
return result != row
return False
def update(self, direction):
"""
Updates the game according to the specified movement in
the desired direction to move the board. If the movement
is possible, inserts a new cell with a value from the
possible initial values in a random empty cell of the board.
"""
changed = False
if direction == UP:
changed = self.move_up()
elif direction == DOWN:
changed = self.move_down()
elif direction == LEFT:
changed = self.move_left()
elif direction == RIGHT:
changed = self.move_right()
if changed:
self.insert_random_new_cell()
def move_left(self):
"""
Moves the board to the left, combining the numbers that can be combined
and moving them to the left.
"""
changed = False
for row in range(ROWS):
if self.combine_row(row):
changed = True
return changed
def move_right(self):
"""
Moves the board to the right, combining the numbers that can
be combined and moving them to the right.
"""
self.invert_board()
changed = self.move_left()
self.invert_board()
return changed
def move_up(self):
"""
Moves the board to the up, combining the numbers that
can be combined and moving them to the up.
"""
self.transpose_board()
changed = self.move_left()
self.transpose_board()
return changed
def move_down(self):
"""
Moves the board to the down, combining the numbers that can be
combined and moving them to the down.
"""
self.transpose_board()
changed = self.move_right()
self.transpose_board()
return changed
def invert_board(self):
"""
Inverts the rows of the board.
"""
self.board = [list(reversed(row)) for row in self.board]
def transpose_board(self):
"""
Transposes the rows and columns of the board.
"""
self.board = [[row[i] for row in self.board] for i in range(COLUMNS)]
def won(self):
"""
Returns True if the game is won, False otherwise.
"""
for i in range(ROWS):
if WIN_NUMBER in self.board[i]:
return True
return False
def lost(self):
"""
Returns True if the game is lost, False otherwise.
"""
if not self.possible_horizontal_moves() and not self.possible_vertical_moves():
return True
return False
def possible_vertical_moves(self):
"""
Returns True if there are vertical moves possible, False otherwise.
"""
self.transpose_board()
result = self.possible_horizontal_moves()
self.transpose_board()
return result
def possible_horizontal_moves(self):
"""
Returns True if there are horizontal moves possible, False otherwise.
"""
for i in range(ROWS):
if self.possible_moves_in_row(self.board[i]):
return True
return False
def possible_moves_in_row(self, row):
"""
Returns True if there are possible moves in the row, False otherwise.
"""
i = 0
while i < len(row):
if row[i] == EMPTY:
return True
if i < len(row) - 1 and row[i] == row[i + 1]:
return True
i += 1
return False
def get_best_score():
"""
Returns the best score stored in the file.
"""
try:
with open("utils/best_score.json", "r", encoding="utf-8") as file:
data = json.load(file)
return data.get("best_score", 0)
except FileNotFoundError:
return 0
def draw_background():
"""
Draw the background and board background.
"""
gamelib.draw_rectangle(
0,
0,
WINDOW_SIZE[0],
WINDOW_SIZE[1],
fill=BACKGROUND_COLOR,
outline=BACKGROUND_COLOR,
)
gamelib.draw_rectangle(
BOARD_X_MARGIN - CELL_PADDING,
BOARD_Y_MARGIN - CELL_PADDING,
BOARD_X_MARGIN + CELL_SIZE * ROWS + CELL_PADDING * (ROWS - 1) + CELL_PADDING,
BOARD_Y_MARGIN
+ CELL_SIZE * COLUMNS
+ CELL_PADDING * (COLUMNS - 1)
+ CELL_PADDING,
fill=BOARD_BACKGROUND_COLOR,
outline=BOARD_BACKGROUND_COLOR,
)
def draw_new_game_button():
"""
Draw the new game button.
"""
gamelib.draw_rectangle(
NEW_GAME_BUTTON[0],
NEW_GAME_BUTTON[1],
NEW_GAME_BUTTON[2],
NEW_GAME_BUTTON[3],
fill=NEW_GAME_BUTTON_COLOR,
activeoutline="black",
activewidth=2,
outline="black",
)
gamelib.draw_text(
"New Game",
(NEW_GAME_BUTTON[0] + NEW_GAME_BUTTON[2]) / 2,
(NEW_GAME_BUTTON[1] + NEW_GAME_BUTTON[3]) / 2,
size=14,
fill="white",
font="Helvetica",
)
def draw_score_section(title, value, x_position):
"""
Draw the score section.
"""
gamelib.draw_rectangle(
x_position,
Y_SCORE_POSITION[0],
x_position + CELL_SIZE,
Y_SCORE_POSITION[1],
fill=NEW_GAME_BUTTON_COLOR,
outline=NEW_GAME_BUTTON_COLOR,
)
gamelib.draw_text(
title,
(x_position + x_position + CELL_SIZE) / 2,
Y_SCORE_POSITION[0] + TEXT_PADDING,
size=14,
fill="white",
font="Helvetica",
)
gamelib.draw_text(
str(value),
(x_position + x_position + CELL_SIZE) / 2,
Y_SCORE_POSITION[1] - TEXT_PADDING,
size=16,
fill="white",
font="Helvetica",
)
def draw_score(score, best):
"""
Draw the score and best sections.
"""
draw_score_section("SCORE", score, BOARD_X_MARGIN - CELL_PADDING)
draw_score_section("BEST", best, BOARD_X_MARGIN + CELL_SIZE + CELL_PADDING)
def draw_board(board):
"""
Draw the game board.
"""
for i, row in enumerate(board):
for j, value in enumerate(row):
x_1, y_1 = (
BOARD_X_MARGIN + j * CELL_SIZE + CELL_PADDING * j,
BOARD_Y_MARGIN + i * CELL_SIZE + CELL_PADDING * i,
)
x_2, y_2 = x_1 + CELL_SIZE, y_1 + CELL_SIZE
gamelib.draw_rectangle(
x_1,
y_1,
x_2,
y_2,
fill=COLORS.get(value, DEFAULT_CELL_COLOR),
outline=COLORS.get(value, DEFAULT_CELL_COLOR),
)
if value != EMPTY:
gamelib.draw_text(
str(value),
(x_1 + x_2) / 2,
(y_1 + y_2) / 2,
fill=NUMBERS_COLOR,
bold=True,
size=20,
font="Helvetica",
)