Skip to content

Commit f9d485d

Browse files
committed
refactor: make win/lose message a class constant
1 parent 025bbb9 commit f9d485d

File tree

4 files changed

+18
-24
lines changed

4 files changed

+18
-24
lines changed

src/mastermind/players/abstract_player.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,21 @@ def undo(self) -> None:
4141

4242

4343
class CodeCracker(Player, ABC):
44-
def __init__(
45-
self,
46-
player_logic: "PlayerLogic", # noqa: F821 # type: ignore
47-
win_msg: str,
48-
lose_msg: str, # noqa: F821 # type: ignore
49-
) -> None: # type: ignore # noqa: F821
50-
super().__init__(player_logic)
51-
self._win_message = win_msg
52-
self._lose_message = lose_msg
44+
@property
45+
@abstractmethod
46+
def _WIN_MESSAGE(self) -> str:
47+
pass
48+
49+
@property
50+
@abstractmethod
51+
def _LOSE_MESSAGE(self) -> str:
52+
pass
5353

5454
def win_message(self) -> None:
55-
print(self._win_message.format(step=len(self.game_state)))
55+
print(self._WIN_MESSAGE.format(step=len(self.game_state)))
5656

5757
def lose_message(self) -> None:
58-
print(self._lose_message.format(step=len(self.game_state)))
58+
print(self._LOSE_MESSAGE.format(step=len(self.game_state)))
5959

6060
@abstractmethod
6161
def obtain_guess(self) -> tuple:

src/mastermind/players/ai_player.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ def get_feedback(self, guess: tuple) -> tuple:
2121

2222

2323
class AICodeCracker(CodeCracker):
24-
def __init__(self, player_logic: "PlayerLogic") -> None: # type: ignore # noqa: F821
25-
win_message = "Congratulations! You won in {step} steps!"
26-
lose_message = "Sorry, you lost. The secret code was {step}."
27-
super().__init__(player_logic, win_message, lose_message)
24+
_WIN_MESSAGE = "Congratulations! You won in {step} steps!"
25+
_LOSE_MESSAGE = "Sorry, you lost. The secret code was {step}."
2826

2927
def obtain_guess(self) -> tuple:
3028
# TODO: Implement AI solver logic to generate guess.

src/mastermind/players/human_player.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,8 @@ def get_feedback(self, guess: tuple) -> tuple:
6969

7070

7171
class HumanCodeCracker(CodeCracker):
72-
def __init__(self, player_logic: "PlayerLogic") -> None: # type: ignore # noqa: F821
73-
win_message = "Congratulations! You won in {step} steps!"
74-
lose_message = "Sorry, you lost. The secret code was {step}."
75-
super().__init__(player_logic, win_message, lose_message)
72+
_WIN_MESSAGE = "Congratulations! You won in {step} steps!"
73+
_LOSE_MESSAGE = "Sorry, you lost. The secret code was {step}."
7674

7775
def obtain_guess(self) -> Union[tuple, str]:
7876
valid_guess = ValidCombination(

tests/players/test_abstract_player.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,12 @@ def test_undo(self, mock_get_feedback, mock_set_secret_code):
6868
class TestCodeCracker(unittest.TestCase):
6969
class ConcreteCodeCracker(CodeCracker):
7070
obtain_guess = lambda: None # noqa: E731
71+
_WIN_MESSAGE = "You win in {step} steps!"
72+
_LOSE_MESSAGE = "You lose after {step} steps."
7173

7274
def setUp(self):
7375
self.game = Game(6, 4, 10, "HvH")
74-
self.win_msg = "You win in {step} steps!"
75-
self.lose_msg = "You lose after {step} steps."
76-
self.code_cracker = self.ConcreteCodeCracker(
77-
self.game._player_logic, self.win_msg, self.lose_msg
78-
)
76+
self.code_cracker = self.ConcreteCodeCracker(self.game._player_logic)
7977

8078
@patch.object(ConcreteCodeCracker, "obtain_guess")
8179
def test_undo(self, mock_obtain_guess):

0 commit comments

Comments
 (0)