Skip to content
Snippets Groups Projects
Commit 530700c1 authored by Angy Wallot's avatar Angy Wallot
Browse files

Merge branch 'main' of gitlab-ssh.univ-lille.fr:angy.wallot.etu/ap-wallot-angy

parents 8035ae64 86668bc2
Branches
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
""" """
:mod:`minesweeper` module :mod:`minesweeper` module
:author: Wallot Angy :author: KINADINOVA Dariya
:date: 14/02/24 :date: 14/02/24
...@@ -61,12 +61,13 @@ def neighborhood(x: int, y: int, width: int, height: int) -> list[tuple[int, int ...@@ -61,12 +61,13 @@ def neighborhood(x: int, y: int, width: int, height: int) -> list[tuple[int, int
$$$ neighborhood(3, 9, 10, 10) $$$ neighborhood(3, 9, 10, 10)
[(2, 8), (2, 9), (3, 8), (4, 8), (4, 9)] [(2, 8), (2, 9), (3, 8), (4, 8), (4, 9)]
""" """
tout_les_voisins = [(x-1,y-1),(x-1,y),(x-1,y+1),(x, y-1),(x, y+1),(x+1,y-1),(x+1, y),(x+1, y+1)] res = [ (xn, yn) for xn, yn in [(x-1, y-1), (x-1, y), (x-1, y+1),
res = [] (x, y-1), (x, y+1),(x+1, y-1), (x+1, y),
for (a,b) in tout_les_voisins: (x+1, y+1)]
if a>=0 and b>=0 and a<width and b<height: if 0 <= xn < width and 0 <=yn < height]
res += (a,b),
return res return res
class Minesweeper(): class Minesweeper():
""" """
...@@ -111,8 +112,12 @@ class Minesweeper(): ...@@ -111,8 +112,12 @@ class Minesweeper():
self.width = width self.width = width
self.height = height self.height = height
self.nbombs = nbombs self.nbombs = nbombs
self.grid = [[Cell() for x in range(width)] for y in range(height)]
self.state = GameState.unfinished self.state = GameState.unfinished
self.grid = [[Cell() for x in range(width)] for y in range(height)]
bombs = random.sample(range(width*height), nbombs)
for i in bombs:
x, y = (i % width), i // width
self._put_a_bomb_at(x, y)
def get_cell(self, x: int, y: int) -> Cell: def get_cell(self, x: int, y: int) -> Cell:
""" """
...@@ -124,14 +129,14 @@ class Minesweeper(): ...@@ -124,14 +129,14 @@ class Minesweeper():
$$$ sum(1 for x in range(20) for y in range(10) if game.get_cell(x, y).is_bomb) $$$ sum(1 for x in range(20) for y in range(10) if game.get_cell(x, y).is_bomb)
4 4
""" """
return self.grid[x][y] return self.grid[y][x]
def _put_a_bomb_at(self, x: int, y: int): def _put_a_bomb_at(self, x: int, y: int):
""" """
this method change the cells's state at x, y to a bomb, this method change the cells's state at x, y to a bomb,
if the cell is already a bomb, nothing append. if the cell is already a bomb, nothing happens.
otherwise it change cell's state and increments by one every otherwise it changes cell's state and increments by one every
cells in its neighborhood. cells in its neighborhood.
precondition: 0 <= x < self.width and 0 <= y < self.height precondition: 0 <= x < self.width and 0 <= y < self.height
...@@ -149,7 +154,11 @@ class Minesweeper(): ...@@ -149,7 +154,11 @@ class Minesweeper():
$$$ all(game.get_cell(x, y).nbombs_in_neighborhood == 1 for x, y in voisins) $$$ all(game.get_cell(x, y).nbombs_in_neighborhood == 1 for x, y in voisins)
True True
""" """
... if self.get_cell(x, y).is_bomb == False:
self.get_cell(x, y).set_bomb()
for i, j in neighborhood(x, y, self.width, self.height):
self.get_cell(i, j).incr_number_of_bombs_in_neighborhood()
def all_cells_are_revealed_or_bomb(self) -> bool: def all_cells_are_revealed_or_bomb(self) -> bool:
""" """
...@@ -158,7 +167,12 @@ class Minesweeper(): ...@@ -158,7 +167,12 @@ class Minesweeper():
précondition: none précondition: none
""" """
... for i in range(self.width):
for j in range(self.height):
self.get_cell(i, j)
if not self.get_cell(i, j).is_bomb and not self.get_cell(i, j).is_revealed:
return False
return True
def reveal_all_cells_from(self, x, y): def reveal_all_cells_from(self, x, y):
""" """
...@@ -196,8 +210,19 @@ class Minesweeper(): ...@@ -196,8 +210,19 @@ class Minesweeper():
$$$ game.state $$$ game.state
GameState.losing GameState.losing
""" """
... if self.get_cell(x, y).is_bomb:
self.state = GameState.losing
elif not self.get_cell(x, y).is_revealed:
self.get_cell(x, y).reveal()
if self.get_cell(x, y).nbombs_in_neighborhood == 0:
for i, j in neighborhood(x, y, self.width, self.height):
self.reveal_all_cells_from(i, j)
else:
self.all_cells_are_revealed_or_bomb()
self.state = GameState.winning
# game = Minesweeper(20, 10, 4)
# game.state == GameState.unfinished
if (__name__ == '__main__'): if (__name__ == '__main__'):
import apl1test import apl1test
......
import sys
def main():
a = int(sys.argv[1])
b = int(sys.argv[2])
print(f"la somme de {a} et {b} est {a+b}")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage : python3 somme.py <a> <b>")
else:
main()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment