def set_adjacent_mine_count(self): """method that calculate the adjacent mine of a cell""" for position in self.grid_coords: x, y = position if self.grid[y][x] >= 0: grid_value = sum(map(self.is_mine, get_adjacent.get_adjacent(position))) self.grid[y][x] = grid_value
def _set_adjacent_mine_count(self) -> None: """Sets cell values to the number of their adjacent mines.""" for position in self.grid_coords: x, y = position if self.grid[y][x] >= 0: grid_value = sum(map(self.is_mine, get_adjacent(position))) self.grid[y][x] = grid_value
def reveal_adjacent(self, index: Tuple[int, int]) -> None: """Reveals the 8 adjacent cells to the input cell's index.""" for coords in get_adjacent(index): if (0 <= coords[0] <= self.width - 1 and 0 <= coords[1] <= self.height - 1): cell_value = self.model.get_cell_value(coords) self.reveal_cell(coords, cell_value)
def test_get_adjacent(self): coordinate_1 = (5, 7) adj_coords_1 = get_adjacent(coordinate_1) self.assertEqual(len(adj_coords_1), 8) for c in [(4, 7), (6, 7), (4, 6), (6, 8), (6, 6), (5, 6), (4, 8), (5, 8)]: self.assertIn(c, adj_coords_1)
def reveal_zeroes(self, index): """method that add zeroes cells in model and reveal cell in view""" self.reveal_cell(index, 0) for coords in get_adjacent.get_adjacent(index): if (0 <= coords[0] <= self.width - 1 and self.height - 1 >= coords[1] >= 0 == self.model.get_cell_value(coords) and coords not in self.model.get_cells_revealed()): self.reveal_zeroes(coords) elif (0 <= coords[0] <= self.width - 1 and self.height - 1 >= coords[1] >= 0 != self.model.get_cell_value(coords) and coords not in self.model.get_cells_revealed()): cell_value = self.model.get_cell_value(coords) self.reveal_cell(coords, cell_value)
def reveal_zeroes(self, index: Tuple[int, int]) -> None: """Reveals all adjacent cells just until a mine is reached.""" val = self.model.get_cell_value(index) if val == 0: self.reveal_cell(index, val) self.reveal_adjacent(index) for coords in get_adjacent(index): if (0 <= coords[0] <= self.width - 1 and self.height - 1 >= coords[1] >= 0 == self.model.get_cell_value(coords) and coords not in self.model.get_revealed_zeroes()): self.model.get_revealed_zeroes().add(coords) self.reveal_zeroes(coords)
def test_set_adjacent_mine_count(self): def is_mine(coords: tuple) -> bool: try: if coords[0] >= 0 and coords[1] >= 0: return self.grid[coords[1]][coords[0]] == -1 else: return False except IndexError: return False coordinates = [] while len(coordinates) <= 50: coord = random.choice(self.model.grid_coords) if self.model.get_cell_value(coord) == self.model.mine_value(): self.assertEqual(self.model.is_mine(coord), True) continue coordinates.append(coord) for coordinate in coordinates: self.assertEqual(self.grid[coordinate[1]][coordinate[0]], sum(map(is_mine, get_adjacent(coordinate))))
img = voronoi_plot_2d(vor) plt.triplot(Coords.values[:, 0], Coords.values[:, 1], Del_tri.simplices.copy()) plt.plot(Coords.values[:, 0], Coords.values[:, 1], "o") fig = plt.figure() fig.add_subplot() #%% #Determining node adjacency based on neighboring Thiessen polygons: from get_adjacent import get_adjacent #Given a node with known coordinates: #point = (0, 0) point = (Coords[0][0], Coords[1][0]) get_adjacent(point, vor) #[145, 142, 6, 167, 206, 144] #%% #All adjacencies all_adjacencies = [] i = 0 for i in range(len(Coords)): point = (Coords[0][i], Coords[1][i]) adj_nodes = get_adjacent(point, vor) all_adjacencies.append(adj_nodes) #%% #Generate necessary lists for operations to be performed reg = vor.point_region