added load / save system
This commit is contained in:
parent
5bdffd40de
commit
75d1c1fa92
3 changed files with 30 additions and 42 deletions
1
NOTE.md
1
NOTE.md
|
@ -2,7 +2,6 @@ A faire :
|
||||||
|
|
||||||
|
|
||||||
1. Principal :
|
1. Principal :
|
||||||
- Sauvegarde
|
|
||||||
- Historique
|
- Historique
|
||||||
- Documenter
|
- Documenter
|
||||||
|
|
||||||
|
|
|
@ -17,20 +17,24 @@ class Board:
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
width: int, height: int = None,
|
width: int = None,
|
||||||
|
height: int = None,
|
||||||
|
|
||||||
boats: np.array = None,
|
boats: np.array = None,
|
||||||
bombs: np.array = None) -> None:
|
bombs: np.array = None) -> None:
|
||||||
|
|
||||||
self.height: int = width
|
if (width is None or height is None) and (boats is None or bombs is None):
|
||||||
self.width: int = width if height is None else height
|
raise ValueError(f"{self.__class__}: width and height or boats and bombs should be set.")
|
||||||
|
|
||||||
# associate the boats and the bombs to array
|
# associate the boats and the bombs to array
|
||||||
self.boats: np.array = np.zeros((self.height, self.width), dtype=np.ushort) if boats is None else boats
|
self.boats: np.array = np.zeros((height, width), dtype=np.ushort) if boats is None else boats
|
||||||
self.bombs: np.array = np.ones((self.height, self.width), dtype=np.bool_) if bombs is None else bombs
|
self.bombs: np.array = np.ones((height, width), dtype=np.bool_) if bombs is None else bombs
|
||||||
|
|
||||||
|
# récupère la hauteur et la largeur
|
||||||
|
self.height, self.width = self.boats.shape
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"<{self.__class__.__name__} width={self.width}, height={self.height}>"
|
return f"<{self.__class__.__name__} width={self.width} height={self.height}>"
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return str(self.get_matrice())
|
return str(self.get_matrice())
|
||||||
|
@ -116,34 +120,38 @@ class Board:
|
||||||
|
|
||||||
return self.boats * self.bombs # Remove the position that have been bombed
|
return self.boats * self.bombs # Remove the position that have been bombed
|
||||||
|
|
||||||
|
def get_score(self) -> int:
|
||||||
|
"""
|
||||||
|
:return: le score du joueur. (Nombre de bateau cassé)
|
||||||
|
"""
|
||||||
|
|
||||||
|
boat_total: int = np.count_nonzero(self.boats)
|
||||||
|
boat_left: int = np.count_nonzero(self.get_matrice())
|
||||||
|
|
||||||
|
return boat_total - boat_left
|
||||||
|
|
||||||
def to_json(self) -> dict:
|
def to_json(self) -> dict:
|
||||||
return {
|
return {
|
||||||
"columns": self.width,
|
"boats": self.boats.tolist(),
|
||||||
"rows": self.height,
|
|
||||||
"boats": [[boat.to_json(), position] for boat, position in self.boats.items()],
|
|
||||||
"bombs": self.bombs.tolist()
|
"bombs": self.bombs.tolist()
|
||||||
}
|
}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_json(cls, json_: dict) -> "Board":
|
def from_json(cls, json_: dict) -> "Board":
|
||||||
return Board(
|
return Board(
|
||||||
width=json_["columns"],
|
boats=np.array(json_["boats"], dtype=np.ushort),
|
||||||
height=json_["rows"],
|
|
||||||
boats={Boat.from_json(boat_json): tuple(position) for boat_json, position in json_["boats"]},
|
|
||||||
bombs=np.array(json_["bombs"], dtype=np.bool_)
|
bombs=np.array(json_["bombs"], dtype=np.bool_)
|
||||||
)
|
)
|
||||||
|
|
||||||
def __copy__(self):
|
def __copy__(self):
|
||||||
return self.__class__(
|
return self.__class__(
|
||||||
height=self.height,
|
|
||||||
width=self.width,
|
|
||||||
boats=self.boats.copy(),
|
boats=self.boats.copy(),
|
||||||
bombs=self.bombs.copy(),
|
bombs=self.bombs.copy(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
board = Board(5)
|
board = Board(5, 10)
|
||||||
board.add_boat(Boat(3, Orientation.VERTICAL), (4, 0))
|
board.add_boat(Boat(3, Orientation.VERTICAL), (4, 0))
|
||||||
board.add_boat(Boat(4, Orientation.HORIZONTAL), (1, 4))
|
board.add_boat(Boat(4, Orientation.HORIZONTAL), (1, 4))
|
||||||
print(board.bomb((4, 1)))
|
print(board.bomb((4, 1)))
|
||||||
|
|
|
@ -208,14 +208,13 @@ class Game(Scene):
|
||||||
self._my_turn = my_turn # is it the player turn ?
|
self._my_turn = my_turn # is it the player turn ?
|
||||||
self._boat_ready_ally: bool = False # does the player finished placing his boat ?
|
self._boat_ready_ally: bool = False # does the player finished placing his boat ?
|
||||||
self._boat_ready_enemy: bool = False # does the opponent finished placing his boat ?
|
self._boat_ready_enemy: bool = False # does the opponent finished placing his boat ?
|
||||||
self._boat_broken_ally: int = 0
|
|
||||||
self._boat_broken_enemy: int = 0
|
|
||||||
|
|
||||||
if len(boats_length) == 0: # s'il n'y a pas de bateau à placé
|
if len(boats_length) == 0: # s'il n'y a pas de bateau à placé
|
||||||
self._boat_ready_ally = True # défini l'état de notre planche comme prête
|
self._boat_ready_ally = True # défini l'état de notre planche comme prête
|
||||||
PacketBoatPlaced().send_connection(connection) # indique à l'adversaire que notre planche est prête
|
PacketBoatPlaced().send_connection(connection) # indique à l'adversaire que notre planche est prête
|
||||||
|
|
||||||
self._refresh_turn_text()
|
self._refresh_turn_text()
|
||||||
|
self._refresh_score_text()
|
||||||
|
|
||||||
# refresh
|
# refresh
|
||||||
|
|
||||||
|
@ -236,26 +235,12 @@ class Game(Scene):
|
||||||
"L'adversaire place ses bombes..."
|
"L'adversaire place ses bombes..."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def _refresh_score_text(self):
|
||||||
|
self.score_ally.text = str(self.grid_enemy.board.get_score())
|
||||||
|
self.score_enemy.text = str(self.grid_ally.board.get_score())
|
||||||
|
|
||||||
# property
|
# property
|
||||||
|
|
||||||
@property
|
|
||||||
def boat_broken_ally(self):
|
|
||||||
return self._boat_broken_ally
|
|
||||||
|
|
||||||
@boat_broken_ally.setter
|
|
||||||
def boat_broken_ally(self, boat_broken_ally: int):
|
|
||||||
self._boat_broken_ally = boat_broken_ally
|
|
||||||
self.score_ally.text = str(self._boat_broken_ally)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def boat_broken_enemy(self):
|
|
||||||
return self._boat_broken_enemy
|
|
||||||
|
|
||||||
@boat_broken_enemy.setter
|
|
||||||
def boat_broken_enemy(self, boat_broken_enemy: int):
|
|
||||||
self._boat_broken_enemy = boat_broken_enemy
|
|
||||||
self.score_enemy.text = str(self._boat_broken_enemy)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def my_turn(self):
|
def my_turn(self):
|
||||||
return self._my_turn
|
return self._my_turn
|
||||||
|
@ -365,9 +350,7 @@ class Game(Scene):
|
||||||
# envoie le résultat à l'autre joueur
|
# envoie le résultat à l'autre joueur
|
||||||
PacketBombState(position=packet.position, bomb_state=bomb_state).send_connection(self.connection)
|
PacketBombState(position=packet.position, bomb_state=bomb_state).send_connection(self.connection)
|
||||||
|
|
||||||
if bomb_state.success:
|
self._refresh_score_text() # le score a changé, donc rafraichi son texte
|
||||||
# si la bombe a touché un bateau, incrémente le score
|
|
||||||
self.boat_broken_enemy += 1
|
|
||||||
|
|
||||||
if bomb_state is BombState.WON:
|
if bomb_state is BombState.WON:
|
||||||
# si l'ennemie à gagner, alors l'on a perdu
|
# si l'ennemie à gagner, alors l'on a perdu
|
||||||
|
@ -382,9 +365,7 @@ class Game(Scene):
|
||||||
# on rejoue uniquement si la bombe à toucher
|
# on rejoue uniquement si la bombe à toucher
|
||||||
self.my_turn = packet.bomb_state.success
|
self.my_turn = packet.bomb_state.success
|
||||||
|
|
||||||
if packet.bomb_state.success:
|
self._refresh_score_text() # le score a changé, donc rafraichi son texte
|
||||||
# si la bombe à toucher, incrémente le score
|
|
||||||
self.boat_broken_ally += 1
|
|
||||||
|
|
||||||
# place la bombe sur la grille ennemie visuelle
|
# place la bombe sur la grille ennemie visuelle
|
||||||
self.grid_enemy.place_bomb(packet.position, force_touched=packet.bomb_state.success)
|
self.grid_enemy.place_bomb(packet.position, force_touched=packet.bomb_state.success)
|
||||||
|
|
Loading…
Reference in a new issue