-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.py
72 lines (66 loc) · 2.24 KB
/
grid.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
from main.square import Square
from collections import deque
class Grid:
def __init__(self, height, width):
self.height = height
self.width = width
self.grid = self.create_grid(height, width)
self.adjacent = [
(1, 0),
(1, 1),
(0, 1),
(-1, 1),
(-1, 0),
(-1, -1),
(0, -1),
(1, -1)
]
def create_grid(self, height, width):
result = []
for y in range(height):
row = []
for x in range(width):
row.append(Square((y, x)))
result.append(row)
return result
def calculate_adjacent(self, case):
count = 0
for adj in self.adjacent:
visiting_height = case.pos[0] + adj[0]
visiting_width = case.pos[1] + adj[1]
if (
visiting_height >= 0
and visiting_width >= 0
and visiting_width < self.width
and visiting_height < self.height
):
if self.grid[case.pos[0] + adj[0]][case.pos[1] + adj[1]].isbomb == True:
count += 1
return count
def reveal(self, square):
counter = 0
visited = set()
queue = deque()
queue.append(square.pos)
while queue:
current = queue[0]
for adj in self.adjacent:
visit_y = current[0] + adj[0]
visit_x = current[1] + adj[1]
if (visit_y, visit_x) in visited:
continue
visited.add((visit_y, visit_x))
if (
visit_y >= 0
and visit_x >= 0
and visit_x < self.width
and visit_y < self.height
):
if self.grid[visit_y][visit_x].isbomb == False:
if self.grid[visit_y][visit_x].isreveal == False:
self.grid[visit_y][visit_x].isreveal = True
counter += 1
if self.grid[visit_y][visit_x].counter == 0:
queue.append((visit_y, visit_x))
queue.popleft()
return counter