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
|
from random import randrange
from sys import stderr
from copy import deepcopy
class Board():
def __init__(self, size = 10, bombs = 0):
# Lauta pitää olla vähintään 2x2, jotta on jotain pelattavaa
size = 2 if size < 2 else size
size = 50 if size > 50 else size
self.size = size
# Pommeja pitää olla vähintään yksi, kuten tyhjiäkin
bombs = size*size*size//100 if bombs < 1 else bombs
bombs = size*size-1 if bombs>=size*size else bombs
bombs = 1 if bombs == 0 else bombs
self.bombs = bombs
self.tiles = []
self.masked = []
self.initialize_tiles( size )
self.randomize_bombs( bombs )
self.calculate_neighbours()
def initialize_tiles(self, size):
self.tiles = [[0 for _ in range(size)] for _ in range(size)]
self.masked = [[10 for _ in range(size)] for _ in range(size)]
def randomize_bombs(self, bomb_count):
for _ in range(bomb_count):
while True:
x, y = randrange(0,self.size), randrange(0,self.size)
if self.tiles[x][y] != 0:
continue
self.tiles[x][y]=9
break
def calculate_neighbours(self):
for y in range(self.size):
for x in range(self.size):
if self.tiles[x][y] == 9:
continue
neighbouring_bombs = 0
for nx, ny in self.get_neighbours_coords(x,y):
if self.tiles[nx][ny] == 9:
neighbouring_bombs += 1
self.tiles[x][y] = neighbouring_bombs
def invalid_coordinates(self, x, y):
return x < 0 or x >= self.size or y < 0 or y >= self.size
def get_neighbours_coords(self, x, y, include_home = False):
offsets = (
(-1,-1), (0,-1), (1,-1),
(-1, 0), (0, 0), (1, 0),
(-1, 1), (0, 1), (1, 1)
) if include_home else (
(-1,-1), (0,-1), (1,-1),
(-1, 0), (1, 0),
(-1, 1), (0, 1), (1, 1)
)
coordinates=[]
for dx,dy in offsets:
if not self.invalid_coordinates(x+dx, y+dy):
coordinates.append( (x+dx, y+dy) )
return coordinates
def get_view(self):
view = deepcopy(self.masked)
for y in range(self.size):
for x in range(self.size):
if not view[x][y]:
view[x][y]=self.tiles[x][y]
return view
def is_winning(self):
for y in range(self.size):
for x in range(self.size):
if self.tiles[x][y] != 9 and self.masked[x][y]:
return False
return True
def collect_area(self, x, y, area=set()):
if not area:
area.add((x,y))
to_test = []
for nx, ny in self.get_neighbours_coords(x, y):
if self.tiles[nx][ny] == 0 and (nx,ny) not in area:
to_test.append((nx, ny))
area.add((nx, ny))
for tx, ty in to_test:
area=area.union(self.collect_area(tx, ty, area))
return area
def get_mask(self, x, y):
return self.masked[x][y]
def flag_tile(self, x, y):
if self.invalid_coordinates(x, y):
print("Koordinaatit on pelilaudan ulkopuolella", file=stderr)
return False
if self.masked[x][y] == 0:
print("Ei voi liputtaa avattua ruutua", file=stderr)
return False
match self.masked[x][y]:
case 0:
print("Ei voi liputtaa avattua ruutua", file=stderr)
return False
case 10 | 11:
self.masked[x][y]+=1
return True
case 12:
self.masked[x][y]=10
return True
print("Ruudulla odottamaton lippu", file=stderr)
return False
def make_guess(self, x, y):
if self.invalid_coordinates(x, y):
print("Koordinaatit on pelilaudan ulkopuolella", file=stderr)
return False
if self.masked[x][y] == 0:
print("Ruutu on jo avattu", file=stderr)
return False
self.masked[x][y] = 0
if self.tiles[x][y] == 9:
return False
if self.tiles[x][y] == 0:
for cx, cy in self.collect_area( x, y ):
for nx, ny in self.get_neighbours_coords(cx, cy, True):
self.masked[nx][ny] = 0
return True
if __name__ == "__main__":
def print_matrix(m):
for y in range(len(m)):
for x in range(len(m[0])):
print(end=f"[{m[x][y]:x}]")
print()
b = Board()
b.make_guess(5,5)
print_matrix(b.get_view())
|