MCPcopy Index your code
hub / github.com/rawpython/remi / build_mine_matrix

Method build_mine_matrix

examples/minefield_app.py:189–206  ·  view source on GitHub ↗

random fill cells with mines and increments nearest mines num in adiacent cells

(self, w, h, minenum)

Source from the content-addressed store, hash-verified

187 self.set_root_widget(self.main_container)
188
189 def build_mine_matrix(self, w, h, minenum):
190 """random fill cells with mines and increments nearest mines num in adiacent cells"""
191 self.minecount = 0
192 matrix = [[Cell(30, 30, x, y, self) for x in range(w)] for y in range(h)]
193 for i in range(0, minenum):
194 x = random.randint(0, w - 1)
195 y = random.randint(0, h - 1)
196 if matrix[y][x].has_mine:
197 continue
198
199 self.minecount += 1
200 matrix[y][x].has_mine = True
201 for coord in [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]:
202 _x, _y = coord
203 if not self.coord_in_map(x + _x, y + _y, w, h):
204 continue
205 matrix[y + _y][x + _x].add_nearest_mine()
206 return matrix
207
208 def no_mine(self, cell):
209 """opens nearest cells that are not near a mine"""

Callers 1

new_gameMethod · 0.95

Calls 3

coord_in_mapMethod · 0.95
CellClass · 0.85
add_nearest_mineMethod · 0.80

Tested by

no test coverage detected