Skip to content

Commit 378c5ed

Browse files
committed
feat(models): Enhanced data classes with more methods and frozen class
1 parent c0ce33a commit 378c5ed

File tree

5 files changed

+82
-21
lines changed

5 files changed

+82
-21
lines changed

src/mastermind/core/models/game_configuration.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,24 @@
33
from mastermind.core.models.game_mode import GameMode
44

55

6-
@dataclass
6+
@dataclass(frozen=True)
77
class GameConfiguration:
88
"""
99
Dataclass for the configuration of a game.
1010
1111
This class defines the settings that determine how a game is structured, including the number of colors, dots, allowed attempts, and game mode.
1212
1313
Attributes:
14-
number_of_colors (int): The number of colors a dot can be.
15-
number_of_dots (int): The number of dots in a code.
16-
attempts_allowed (int): The maximum number of attempts allowed for the code cracker to guess the code.
17-
game_mode (GameMode): The game mode determine who is Player 1 and Player 2.
14+
NUMBER_OF_COLORS (int): The number of colors a dot can be.
15+
NUMBER_OF_DOTS (int): The number of dots in a code.
16+
ATTEMPTS_ALLOWED (int): The maximum number of attempts allowed for the code to guess the code.
17+
GAME_MODE (GameMode): The game mode determine who is Player 1 and Player 2.
1818
"""
1919

20-
number_of_colors: int
21-
number_of_dots: int
22-
attempts_allowed: int
23-
game_mode: GameMode
20+
NUMBER_OF_COLORS: int
21+
NUMBER_OF_DOTS: int
22+
ATTEMPTS_ALLOWED: int
23+
GAME_MODE: GameMode
24+
25+
def __str__(self):
26+
return f"{self.NUMBER_OF_COLORS}x{self.NUMBER_OF_DOTS}, {self.ATTEMPTS_ALLOWED} attempts in {self.GAME_MODE} mode"

src/mastermind/core/models/game_entities.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
from mastermind.core.controllers.players import Player
44

55

6-
@dataclass
6+
@dataclass(frozen=True)
77
class GameEntities:
88
"""
99
Dataclass for the entities of a game (such as players or those from plugins).
1010
1111
This class represents the key participants in a game, specifically the players involved in setting and breaking the code. It serves as a structured way to manage and reference these entities throughout the game's lifecycle.
1212
1313
Attributes:
14-
code_setter (Player): The player responsible for setting the code.
15-
code_breaker (Player): The player tasked with breaking the code.
14+
CODE_SETTER (Player): The player responsible for setting the code.
15+
CODE_BREAKER (Player): The player tasked with breaking the code.
1616
"""
1717

18-
code_setter: Player
19-
code_breaker: Player
18+
CODE_SETTER: Player
19+
CODE_BREAKER: Player
20+
21+
def __str__(self):
22+
return f"Code Setter: {self.CODE_SETTER.__qualname__}, Code Breaker: {self.CODE_BREAKER.__qualname__}"

src/mastermind/core/models/game_round.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22
from typing import Tuple
33

44

5-
@dataclass
5+
@dataclass(frozen=True)
66
class GameRound:
77
"""
88
Dataclass for a single round of the game.
99
1010
This class encapsulates the guess made during a round and the corresponding feedback received.
1111
1212
Attributes:
13-
guess (Tuple[int, ...]): A tuple representing the player's guess.
14-
feedback (Tuple[int, int]): A tuple containing feedback information, typically indicating the number of correct guesses and their positions.
13+
GUESS (Tuple[int, ...]): A tuple representing the player's guess.
14+
FEEDBACK (Tuple[int, int]): A tuple containing feedback information, typically indicating the number of correct guesses and their positions.
15+
16+
Examples:
17+
>>> round = GameRound(GUESS=(1, 2, 3, 4), FEEDBACK=(2, 1))
18+
>>> print(round.GUESS)
19+
(1, 2, 3, 4)
20+
>>> print(round.FEEDBACK)
21+
(2, 1)
1522
"""
1623

17-
guess: Tuple[int, ...]
18-
feedback: Tuple[int, int]
24+
GUESS: Tuple[int, ...]
25+
FEEDBACK: Tuple[int, int]

src/mastermind/core/models/game_state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class GameState:
1111
Attributes:
1212
game_started (bool): A flag indicating if the game has started.
1313
game_over (bool): A flag indicating if the game has ended.
14-
winner (Player): The player who won the game, if any. Only top level player (CodeSetter or CodeCracker) is allowed.
14+
winner (Player): The player who won the game, if any. Only top level player (CodeSetter or Codebreaker) is allowed.
1515
"""
1616

1717
game_started: bool

src/mastermind/core/models/gameboard.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import dataclass
2-
from typing import Deque
2+
from typing import Deque, Generator, Tuple
33

44
from mastermind.core.models.game_round import GameRound
55

@@ -16,3 +16,51 @@ class GameBoard:
1616
"""
1717

1818
game_rounds: Deque[GameRound]
19+
20+
def __len__(self) -> int:
21+
"""
22+
Returns the number of game rounds in the game board.
23+
24+
Returns:
25+
int: Number of game rounds in the game board.
26+
27+
Examples:
28+
>>> game_board = GameBoard(game_rounds=[GameRound(guess=(1, 2, 3, 4), result=(1, 0)), GameRound(guess=(3, 4, 5, 6), result=(2, 1))])
29+
>>> len(game_board)
30+
2
31+
"""
32+
return len(self.game_rounds)
33+
34+
@property
35+
def guesses(self) -> Generator[Tuple[int, ...], None, None]:
36+
"""
37+
Returns a generator of all guesses made in the game to allow for easy iteration.
38+
39+
Returns:
40+
Generator[Tuple[int, ...], None, None]: _description_
41+
42+
Examples:
43+
>>> game_board = GameBoard(game_rounds=[GameRound(guess=(1, 2, 3, 4), result=(1, 0)), GameRound(guess=(3, 4, 5, 6), result=(2, 1))])
44+
>>> for guess in game_board.guesses:
45+
... print(guess)
46+
(1, 2, 3, 4)
47+
(3, 4, 5, 6)
48+
"""
49+
return (round.GUESS for round in self.game_rounds)
50+
51+
@property
52+
def feedbacks(self) -> Generator[Tuple[int, int], None, None]:
53+
"""
54+
Returns a generator of all feedbacks received in the game to allow for easy iteration.
55+
56+
Yields:
57+
Generator[Tuple[int], None, None]: A generator of all feedbacks received in the game.
58+
59+
Examples:
60+
>>> game_board = GameBoard(game_rounds=[GameRound(guess=(1, 2, 3, 4), result=(1, 0)), GameRound(guess=(3, 4, 5, 6), result=(2, 1))])
61+
>>> for feedback in game_board.feedbacks:
62+
... print(feedback)
63+
(1, 0)
64+
(2, 1)
65+
"""
66+
return (round.FEEDBACK for round in self.game_rounds)

0 commit comments

Comments
 (0)