-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.py
259 lines (234 loc) · 10.3 KB
/
board.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
import pygame
import random
from constants import *
# Contains the Board class consisting of the
# framework used to keep track of the current state
# of the board along with all the calculations used
# when playing the game
START_VALS = [MULTIPLIER, MULTIPLIER * MULTIPLIER]
class Board:
# constructor
def __init__(self, num_blocks: int = STARTING_BLOCKS) -> None:
self.board = [[0 for j in range(COLS)] for i in range(ROWS)]
for i in range(num_blocks):
self.add_block()
# self.board = [[2,32,128, 2048],[0, 0, 4, 64],[0, 4, 8, 4],[0, 0, 0, 4]]
# draws the background of the game window
def draw_bg(self, window: pygame.Surface, block_width: int, block_height: int) -> None:
window.fill(COLORS['border'])
for i in range(COLS):
for j in range(ROWS):
x_offset = i * block_width + i * W_BORDER_WEIGHT + W_BORDER_WEIGHT
y_offset = j * block_height + j * H_BORDER_WEIGHT + H_BORDER_WEIGHT
rect = (x_offset, y_offset, block_width, block_height)
pygame.draw.rect(window, COLORS['bg'], rect, border_radius=BLOCK_RADIUS)
# get the font size based on the value of the block
def get_font_size(self, num: int) -> int:
if num < 100:
return 60
if num < 1000:
return 55
elif num < 10000:
return 40
elif num < 100000:
return 25
return 10
# get the font color based on the value of the block
def get_font_color(self, num: int) -> tuple:
if num <= MULTIPLIER ** 2:
return COLORS['dark_text']
return WHITE
#draws the text of a block onto the screen
def draw_text(self, window: pygame.Surface, rect: pygame.Rect, val: int):
font_size = self.get_font_size(val)
font_color = self.get_font_color(val)
font = pygame.font.Font(FONT, font_size)
text = font.render(str(val), True, font_color)
text_x = rect.center[0] - text.get_rect().width // 2
text_y = rect.center[1] - text.get_rect().height // 2 - 5
text_placement = (text_x, text_y)
window.blit(text, text_placement)
# draw all the blocks on the board
def draw_blocks(self, window: pygame.Surface, block_width: int, block_height: int) -> None:
for i in range(COLS):
for j in range(ROWS):
val = self.board[j][i]
if val != 0:
x_offset = i * block_width + i * W_BORDER_WEIGHT + W_BORDER_WEIGHT
y_offset = j * block_height + j * H_BORDER_WEIGHT + H_BORDER_WEIGHT
rect = pygame.Rect(x_offset, y_offset, block_width, block_height)
pygame.draw.rect(window, BLOCK_COLORS[val], rect, border_radius=BLOCK_RADIUS)
if B_TEXT == True: self.draw_text(window, rect, val)
# draw the board on the game window
def draw(self, window: pygame.Surface) -> None:
block_width = (WIDTH - (W_BORDER_WEIGHT * (COLS+1))) / COLS
block_height = (HEIGHT - (H_BORDER_WEIGHT * (ROWS+1))) / ROWS
self.draw_bg(window, block_width, block_height)
self.draw_blocks(window, block_width, block_height)
# returns true if there are no possible moves to make
def no_moves(self) -> bool:
for i in range(ROWS):
for j in range(COLS):
if self.board[i][j] == 0:
return False
if i > 0:
if self.board[i-1][j] == self.board[i][j]:
return False
if i < ROWS-1:
if self.board[i+1][j] == self.board[i][j]:
return False
if j > 0:
if self.board[i][j-1] == self.board[i][j]:
return False
if j < COLS-1:
if self.board[i][j+1] == self.board[i][j]:
return False
return True
#returns true if all the spaces on the board are occupied
def is_full(self) -> bool:
for i in range(ROWS):
for j in range(COLS):
if self.board[i][j] == 0:
return False
return True
# get the closest block to the left of a given block
# returns the coordinates of the block found or the given block if no block found
def get_closest_left(self, x: int, y: int, board: list) -> list:
for i in reversed(range(y)):
if board[x][i] != 0:
return [x, i]
return [x, y]
# shift the contents of the board to the left combining blocks of the same value
def shift_left(self) -> int:
new_board = [row[:] for row in self.board]
merged_points = []
for i in range(ROWS):
for j in range(1, COLS):
point = self.get_closest_left(i, j, new_board)
if point == [i, j]:
new_board[i][0] = new_board[i][j]
new_board[i][j] = 0
else:
x, y = point[0], point[1]
if new_board[x][y] == new_board[i][j] and (x, y) not in merged_points:
new_board[x][y] *= MULTIPLIER
merged_points.append((x, y))
new_board[i][j] = 0
elif y+1 != j:
new_board[x][y+1] = new_board[i][j]
new_board[i][j] = 0
if new_board != self.board:
self.board = new_board
return 1
else:
return 0
# get the closest block to the right of a given block
# returns the coordinates of the block found or the given block if no block found
def get_closest_right(self, x: int, y: int, board: list) -> list:
for i in range(y+1, COLS):
if board[x][i] != 0:
return [x, i]
return [x, y]
# shift the contents of the board to the right combining blocks of the same value
def shift_right(self) -> int:
new_board = [row[:] for row in self.board]
merged_points = []
for i in range(ROWS):
for j in reversed(range(COLS-1)):
point = self.get_closest_right(i, j, new_board)
if point == [i, j]:
new_board[i][COLS-1] = new_board[i][j]
new_board[i][j] = 0
else:
x, y = point[0], point[1]
if new_board[x][y] == new_board[i][j] and (x, y) not in merged_points:
new_board[x][y] *= MULTIPLIER
merged_points.append((x, y))
new_board[i][j] = 0
elif y-1 != j:
new_board[x][y-1] = new_board[i][j]
new_board[i][j] = 0
if new_board != self.board:
self.board = new_board
return 1
else:
return 0
# get the closest block closeset above of a given block
# returns the coordinates of the block found or the given block if no block found
def get_closest_up(self, x: int, y: int, board: list) -> list:
for i in reversed(range(x)):
if board[i][y] != 0:
return [i, y]
return [x, y]
# shift the contents of the board up combining blocks of the same value
def shift_up(self) -> int:
new_board = [row[:] for row in self.board]
merged_points = []
for j in range(COLS):
for i in range(1, ROWS):
point = self.get_closest_up(i, j, new_board)
if point == [i, j]:
new_board[0][j] = new_board[i][j]
new_board[i][j] = 0
else:
x, y, = point[0], point[1]
if new_board[x][y] == new_board[i][j] and (x, y) not in merged_points:
new_board[x][y] *= MULTIPLIER
merged_points.append((x, y))
new_board[i][j] = 0
elif x+1 != i:
new_board[x+1][y] = new_board[i][j]
new_board[i][j] = 0
if new_board != self.board:
self.board = new_board
return 1
else:
return 0
# get the closest block closeset below of a given block
# returns the coordinates of the block found or the given block if no block found
def get_closest_down(self, x: int, y: int, board: list) -> list:
for i in range(x+1, ROWS):
if board[i][y] != 0:
return [i, y]
return [x, y]
# shift the contents of the board down combining blocks of the same value
def shift_down(self) -> int:
new_board = [row[:] for row in self.board]
merged_points = []
for j in range(COLS):
for i in reversed(range(ROWS-1)):
point = self.get_closest_down(i, j, new_board)
if point == [i, j]:
new_board[ROWS-1][j] = new_board[i][j]
new_board[i][j] = 0
else:
x, y = point[0], point[1]
if new_board[x][y] == new_board[i][j] and (x, y) not in merged_points:
new_board[x][y] *= MULTIPLIER
merged_points.append((x, y))
new_board[i][j] = 0
elif x-1 != i:
new_board[x-1][y] = new_board[i][j]
new_board[i][j] = 0
if new_board != self.board:
self.board = new_board
return 1
else:
return 0
# add a new block to the board at a random free position
def add_block(self) -> None:
while True:
i = random.randint(0, ROWS-1)
j = random.randint(0, COLS-1)
if self.board[i][j] == 0:
break
self.board[i][j] = random.choice(START_VALS)
# clear the board and add 'num_blocks' to the newly cleared board
def clear(self, num_blocks: int = STARTING_BLOCKS) -> None:
self.board = [[0 for j in range(COLS)] for i in range(ROWS)]
for i in range(num_blocks):
self.add_block()
def __repr__(self) -> str:
return '\n'.join([''.join(['{:5}'.format(item) for item in row]) for row in self.board])
def __eq__(self, board: list):
return self.board == board