diff --git a/TP5/minesweeper/minesweeper.py b/TP5/minesweeper/minesweeper.py index 0d4925d75196e6ba0fe0e0a7887a966676e8c18e..9caa1020eb922a3dfb0b3c115d7eecb6f44a3e68 100644 --- a/TP5/minesweeper/minesweeper.py +++ b/TP5/minesweeper/minesweeper.py @@ -46,7 +46,7 @@ def neighborhood(x: int, y: int, width: int, height: int) -> list[tuple[int, int return the list of coordinates of the neighbors of position (x,y) in a grid of size width*height - précondition: 0 <= x < width and 0 <= y < height + préconditi $$$ game = Minesweeper(20, 10, 4)on: 0 <= x < width and 0 <= y < height examples: @@ -62,18 +62,13 @@ def neighborhood(x: int, y: int, width: int, height: int) -> list[tuple[int, int [(2, 8), (2, 9), (3, 8), (4, 8), (4, 9)] """ neighbors = [] - for distance_x in range(-1,2): - for distance_y in range(-1,2): - x_1,y_1 = x + distance_x, y + distance_y - if 0 <= x_1 < width and 0 <= y_1 < height and (x_1,y_1) != (x,y): - neighbors.append((x_1,y_1)) + for i in range(x-1,x+2): + for j in range(y-1,y+2): + if (i,j)!=(x,y) and 0<=i<width and 0<=j<height: + neighbors.append((i,j)) return neighbors - - - - - + class Minesweeper(): """ $$$ game = Minesweeper(20, 10, 4) @@ -90,6 +85,14 @@ class Minesweeper(): False $$$ """ + + + + + + + + def __init__(self, width: int=30, height: int=20, nbombs: int=99): """ @@ -114,19 +117,18 @@ class Minesweeper(): $$$ game.state == GameState.unfinished True """ - self.width= width - self.height= height - self.nbombs= nbombs - self.state= GameState.unfinished - self.grid = [[cell() for i in range(width)] for j in range(height)] + self.width = width + self.height = height + self.nbombs = nbombs + self.state = GameState.unfinished n=0 while n<nbombs: - y=random.randint(0,width-1) - x=random.randint(0,height-1) - if not self.grid[x][y].is_bomb: - self._put_a_bomb_at(x,y) + y=randint(0,width-1) + x=randint(0,height-1) + if not sel.grid[x][y].is_bomb: + self.put_a_bomb_at(x,y) n=n+1 - + def get_cell(self, x: int, y: int) -> Cell: """ @@ -138,7 +140,7 @@ class Minesweeper(): $$$ sum(1 for x in range(20) for y in range(10) if game.get_cell(x, y).is_bomb) 4 """ - ... + return self._grid[y][x] def _put_a_bomb_at(self, x: int, y: int): """ @@ -163,7 +165,19 @@ class Minesweeper(): $$$ all(game.get_cell(x, y).nbombs_in_neighborhood == 1 for x, y in voisins) True """ - ... + if 0<=x<self.width and 0<=y<self.height: + case=self.grid[y][x] + if not case.nbombs_in_neighborhood: + case.nbombs_in_neighborhood=True + + for i in range(x-1,x+2): + for j in range(y-1,y+2): + if 0<=i<self.width and 0<=j<self.height: + case_voisine=self.grid[j][i] + if not case_voisine.nbombs_in_neighborhood: + case_voisine.nbombs_in_neighborhood+=1 + + def all_cells_are_revealed_or_bomb(self) -> bool: """ @@ -210,7 +224,14 @@ class Minesweeper(): $$$ game.state GameState.losing """ - ... + res=True + for i in range(self.width): + for j in range(self.height): + cell=self.grid[j][i] + if not (cell.is_revealed and cell.is_bomb): + res=False + return res + if (__name__ == '__main__'):