-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathboard.py
349 lines (274 loc) · 8.19 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
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
import operator
from enum import Enum
from math import floor
from random import randrange
import pygame
from library import colours
from consts import GRIDPADDING
from consts import BLOCK_SIZE
from sprites import (
mine, detonation,
flag, greenflag, yellowflag,
zero,
one,
two,
three,
four,
five,
six,
seven,
eight,
)
transformations = [
(-1, 0),
(1, 0),
(-1, -1),
(+1, +1),
(0, -1),
(0, +1),
(-1, +1),
(+1, -1),
]
def add(a, b):
return tuple(map(operator.add, a, b))
def _normalise_pos(pos):
a, b = pos
return 16 * floor(a/16), 16 * floor(b/16)
def _floor_pos(pos):
a, b = pos
return floor(a/16) - 1, floor(b/16) - 1
class TileType(Enum):
Empty = 0
One = 1
Two = 2
Three = 3
Four = 4
Five = 5
Six = 6
Seven = 7
Eight = 8
Mine = "mine"
Detonation = "detonation"
def get_sprite(type):
if type == TileType.Mine:
return mine
if type == TileType.Detonation:
return detonation
if type == TileType.One:
return one
if type == TileType.Two:
return two
if type == TileType.Three:
return three
if type == TileType.Four:
return four
if type == TileType.Five:
return five
if type == TileType.Six:
return six
if type == TileType.Seven:
return seven
if type == TileType.Eight:
return eight
if type == TileType.Empty:
return zero
class Flag:
Red = "red"
Green = "green"
Yellow = "yellow"
class Tile:
def __init__(self, x, y, type):
self.x = x
self.y = y
self.n = 0
self.type = type
self.flagged = False
self._flag_colour = Flag.Red
self.visible = False
def render(self, win):
pos = (
GRIDPADDING + (self.x * BLOCK_SIZE),
GRIDPADDING + (self.y * BLOCK_SIZE),
)
if self.flagged:
if self._flag_colour == Flag.Red:
win.blit(flag, pos)
elif self._flag_colour == Flag.Green:
win.blit(greenflag, pos)
else:
win.blit(yellowflag, pos)
if not self.visible:
return
sprite = get_sprite(self.type)
if sprite:
px, py = pos
rect = pygame.Rect(pos, (16, 16))
pygame.draw.rect(win, (170,170,170), rect)
if sprite is not zero:
win.blit(sprite, pos)
def toggle_visible(self):
if self.visible:
return
self.visible = True
def toggle_flag(self):
if self.flagged:
self.flagged = False
else:
self.flagged = True
def set_type(self, type):
self.type = type
def set_green(self):
self._flag_colour = Flag.Green
def set_yellow(self):
self._flag_colour = Flag.Yellow
def increment(self):
if self.type == TileType.Mine:
raise ValueError("Can't increment a mine")
self.n += 1
self.type = TileType(self.n)
def around(self):
pos = (self.x, self.y)
yield from _around(pos)
def _around(pos):
for t in transformations:
other = add(pos, t)
yield other
class Board:
def rightclick(self, pos):
realx, realy = _floor_pos(pos)
if not self._inside_board(realx, realy):
return
if not self._grid_constructed:
return
tile = self.grid[realx][realy]
if tile.visible:
return
tile.toggle_flag()
# Tiles don't ever start as flagged
# thus .remove will only ever be
# called after .add
if tile.flagged:
self.flags.add((realx, realy))
else:
self.flags.remove((realx, realy))
def leftclick(self, pos) -> bool:
"""The return value determines if the player survived."""
realx, realy = _floor_pos(pos)
if not self._inside_board(realx, realy):
return True
if not self._grid_constructed:
self._grid_constructed = True
self.grid = self._construct_grid(self.x, self.y, (realx, realy))
tile = self.grid[realx][realy]
if tile.flagged:
return True
if tile.type == TileType.Mine:
tile.type = TileType.Detonation
return False
if tile.visible:
return True
searched = set()
tile.toggle_visible()
self.n_visible -= 1
self._zero_search(tile, searched)
return True
def _zero_search(self, tile, searched):
if tile.n != 0:
return searched
if tile.type == TileType.Mine or tile.flagged:
return searched
for pos in tile.around():
px, py = pos
if self._inside_board(px, py):
tile = self.grid[px][py]
if tile not in searched:
searched.add(tile)
searched = self._zero_search(tile, searched)
if not tile.visible:
tile.toggle_visible()
self.n_visible -= 1
return searched
def check_win(self) -> bool:
return self.n_visible == self.n_mines
def display_win(self):
if not self._revealed:
self._revealed = True
for pos in self.mines:
px, py = pos
tile = self.grid[px][py]
tile.set_yellow()
if not tile.flagged:
tile.toggle_flag()
self.flags.add(tile)
def show_mines(self):
if not self._revealed:
self._revealed = True
for pos in self.mines:
px, py = pos
tile = self.grid[px][py]
if pos not in self.flags:
tile.toggle_visible()
else:
tile.set_green()
def __init__(self, x, y, mines: int):
self.x = x
self.y = y
self.n_visible = x * y
self.n_mines = mines
self.flags = set()
self.grid = None
self._grid_constructed = False
self._revealed = False
x_blocks = x * BLOCK_SIZE
y_blocks = y * BLOCK_SIZE
self.tl = (GRIDPADDING, GRIDPADDING)
self.tr = (GRIDPADDING, GRIDPADDING + y_blocks)
self.bl = (GRIDPADDING + x_blocks, GRIDPADDING)
self.br = (GRIDPADDING + x_blocks, GRIDPADDING + y_blocks)
def _construct_grid(self, x, y, first_pos):
exclude = {pos for pos in _around(first_pos)}
exclude.add(first_pos)
# Generate Grid.
grid = [
[Tile(rx, ry, TileType.Empty) for ry in range(y)]
for rx in range(x)
]
# Placing the mines on the board
mines = set()
while len(mines) < self.n_mines:
mx, my = randrange(self.x), randrange(self.y)
if ((mx, my) not in mines) and ((mx, my) not in exclude):
mines.add((mx, my))
tile = grid[mx][my]
tile.set_type(TileType.Mine)
# Calculate Numbers surrounding mines
for mine in mines:
mx, my = mine
tile = grid[mx][my]
for ox, oy in tile.around():
if self._inside_board(ox, oy):
other_tile = grid[ox][oy]
if other_tile.type != TileType.Mine:
other_tile.increment()
self.mines = mines
return grid
def _inside_board(self, x, y):
return (0 <= x <= self.x - 1) and (0 <= y <= self.y - 1)
def _render_grid(self, win):
for row in self.grid:
for tile in row:
tile.render(win)
def render(self, win):
if self.grid:
self._render_grid(win)
# Outline
for i in range(0, self.x + 1):
diff = (i*16, 0)
left = add(self.tl, diff)
right = add(self.tr, diff)
pygame.draw.line(win, colours.BLACK, left , right, 1)
for i in range(0, self.y + 1):
diff = (0, i*16)
top = add(self.tl, diff)
bottom = add(self.bl, diff)
pygame.draw.line(win, colours.BLACK, top, bottom, 1)