| 7 | 7 | |
| 8 | 8 | class Hangman: |
| 9 | 9 | def __init__(self, word: str): |
| 10 | self.word = word |
| 10 | 11 | self.remaining_guesses = 9 |
| 11 | 12 | self.status = STATUS_ONGOING |
| 13 | self.guessed_chars = set() |
| 14 | self.correct_chars = set() |
| 12 | 15 | |
| 13 | 16 | def guess(self, char: str) -> None: |
| 14 | | pass |
| 17 | # Edge Case: Game already ended |
| 18 | if self.status != STATUS_ONGOING: |
| 19 | raise ValueError("The game has already ended.") |
| 20 | |
| 21 | # Edge Case: Character already guessed |
| 22 | if char in self.guessed_chars: |
| 23 | self.remaining_guesses -= 1 |
| 24 | else: |
| 25 | self.guessed_chars.add(char) |
| 26 | # Edge Case: Correct guess |
| 27 | if char in self.word: |
| 28 | self.correct_chars.add(char) |
| 29 | # Edge Case: Incorrect guess |
| 30 | else: |
| 31 | self.remaining_guesses -= 1 |
| 32 | |
| 33 | # Update game status |
| 34 | self._update_status() |
| 35 | |
| 36 | def _update_status(self) -> None: |
| 37 | # Edge Case: Player wins by guessing all characters |
| 38 | if all(char in self.correct_chars for char in self.word): |
| 39 | self.status = STATUS_WIN |
| 40 | # Edge Case: Player loses by running out of guesses |
| 41 | elif self.remaining_guesses < 0: |
| 42 | self.status = STATUS_LOSE |
| 43 | # Edge Case: Game still ongoing |
| 44 | else: |
| 45 | self.status = STATUS_ONGOING |
| 15 | 46 | |
| 16 | 47 | def get_masked_word(self) -> str: |
| 17 | | pass |
| 48 | # Edge Case: Return masked word with underscores for unguessed characters |
| 49 | return ''.join(char if char in self.correct_chars else '_' for char in self.word) |
| 18 | 50 | |
| 19 | 51 | def get_status(self) -> str: |
| 20 | | pass |
| 52 | return self.status |