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 :
|
||||
- Sauvegarde
|
||||
- Historique
|
||||
- Documenter
|
||||
|
||||
|
|
|
@ -17,20 +17,24 @@ class Board:
|
|||
|
||||
def __init__(
|
||||
self,
|
||||
width: int, height: int = None,
|
||||
width: int = None,
|
||||
height: int = None,
|
||||
|
||||
boats: np.array = None,
|
||||
bombs: np.array = None) -> None:
|
||||
|
||||
self.height: int = width
|
||||
self.width: int = width if height is None else height
|
||||
if (width is None or height is None) and (boats is None or bombs is None):
|
||||
raise ValueError(f"{self.__class__}: width and height or boats and bombs should be set.")
|
||||
|
||||
# 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.bombs: np.array = np.ones((self.height, self.width), dtype=np.bool_) if bombs is None else bombs
|
||||
self.boats: np.array = np.zeros((height, width), dtype=np.ushort) if boats is None else boats
|
||||
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:
|
||||
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:
|
||||
return str(self.get_matrice())
|
||||
|
@ -116,34 +120,38 @@ class Board:
|
|||
|
||||
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:
|
||||
return {
|
||||
"columns": self.width,
|
||||
"rows": self.height,
|
||||
"boats": [[boat.to_json(), position] for boat, position in self.boats.items()],
|
||||
"boats": self.boats.tolist(),
|
||||
"bombs": self.bombs.tolist()
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, json_: dict) -> "Board":
|
||||
return Board(
|
||||
width=json_["columns"],
|
||||
height=json_["rows"],
|
||||
boats={Boat.from_json(boat_json): tuple(position) for boat_json, position in json_["boats"]},
|
||||
boats=np.array(json_["boats"], dtype=np.ushort),
|
||||
bombs=np.array(json_["bombs"], dtype=np.bool_)
|
||||
)
|
||||
|
||||
def __copy__(self):
|
||||
return self.__class__(
|
||||
height=self.height,
|
||||
width=self.width,
|
||||
boats=self.boats.copy(),
|
||||
bombs=self.bombs.copy(),
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
board = Board(5)
|
||||
board = Board(5, 10)
|
||||
board.add_boat(Boat(3, Orientation.VERTICAL), (4, 0))
|
||||
board.add_boat(Boat(4, Orientation.HORIZONTAL), (1, 4))
|
||||
print(board.bomb((4, 1)))
|
||||
|
|
|
@ -208,14 +208,13 @@ class Game(Scene):
|
|||
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_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é
|
||||
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
|
||||
|
||||
self._refresh_turn_text()
|
||||
self._refresh_score_text()
|
||||
|
||||
# refresh
|
||||
|
||||
|
@ -236,26 +235,12 @@ class Game(Scene):
|
|||
"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
|
||||
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
|
||||
def my_turn(self):
|
||||
return self._my_turn
|
||||
|
@ -365,9 +350,7 @@ class Game(Scene):
|
|||
# envoie le résultat à l'autre joueur
|
||||
PacketBombState(position=packet.position, bomb_state=bomb_state).send_connection(self.connection)
|
||||
|
||||
if bomb_state.success:
|
||||
# si la bombe a touché un bateau, incrémente le score
|
||||
self.boat_broken_enemy += 1
|
||||
self._refresh_score_text() # le score a changé, donc rafraichi son texte
|
||||
|
||||
if bomb_state is BombState.WON:
|
||||
# si l'ennemie à gagner, alors l'on a perdu
|
||||
|
@ -382,9 +365,7 @@ class Game(Scene):
|
|||
# on rejoue uniquement si la bombe à toucher
|
||||
self.my_turn = packet.bomb_state.success
|
||||
|
||||
if packet.bomb_state.success:
|
||||
# si la bombe à toucher, incrémente le score
|
||||
self.boat_broken_ally += 1
|
||||
self._refresh_score_text() # le score a changé, donc rafraichi son texte
|
||||
|
||||
# place la bombe sur la grille ennemie visuelle
|
||||
self.grid_enemy.place_bomb(packet.position, force_touched=packet.bomb_state.success)
|
||||
|
|
Loading…
Reference in a new issue