| 7 | 7 | |
| 8 | 8 | class Hangman: |
| 9 | 9 | def __init__(self, word: str): |
| 10 | # Edge Case: Empty word initialization |
| 11 | if not word: |
| 12 | raise ValueError("Word cannot be empty") |
| 13 | |
| 14 | self.original_word = word |
| 15 | self.word = word.lower() |
| 10 | 16 | self.remaining_guesses = 10 |
| 11 | 17 | self.status = STATUS_ONGOING |
| 18 | self.guessed_chars = set() |
| 19 | self.correctly_guessed_chars = set() |
| 12 | 20 | |
| 13 | 21 | def guess(self, char: str) -> None: |
| 14 | | pass |
| 15 | | |
| 22 | # Edge Case: Game already ended |
| 23 | if self.status != STATUS_ONGOING: |
| 24 | raise ValueError("The game has already ended.") |
| 25 | |
| 26 | # Edge Case: Invalid input character (non-alphabetic) |
| 27 | if not char.isalpha(): |
| 28 | raise ValueError("Guess must be an alphabetic character") |
| 29 | |
| 30 | char = char.lower() |
| 31 | |
| 32 | # Edge Case: Repeated letter guesses |
| 33 | if char in self.guessed_chars: |
| 34 | self.remaining_guesses -= 1 |
| 35 | else: |
| 36 | self.guessed_chars.add(char) |
| 37 | if char in self.word: |
| 38 | self.correctly_guessed_chars.add(char) |
| 39 | else: |
| 40 | self.remaining_guesses -= 1 |
| 41 | |
| 42 | # Check for win/lose conditions |
| 43 | # Check lose condition first (no guesses remaining) |
| 44 | if self.remaining_guesses <= 0: |
| 45 | self.status = STATUS_LOSE |
| 46 | # Check win condition (all unique letters guessed) |
| 47 | elif all(c in self.correctly_guessed_chars for c in set(self.word)): |
| 48 | self.status = STATUS_WIN |
| 49 | |
| 16 | 50 | def get_masked_word(self) -> str: |
| 17 | | pass |
| 18 | | |
| 51 | # Edge Case: Empty word |
| 52 | if not self.word: |
| 53 | return "" |
| 54 | |
| 55 | # Need to preserve original case, so check against lowercase version |
| 56 | return ''.join(c if c.lower() in self.correctly_guessed_chars else '_' for c in self.original_word) |
| 57 | |
| 19 | 58 | def get_status(self) -> str: |
| 20 | | pass |
| 59 | return self.status |
| 60 | |
| 61 | # Handled Edge Cases: Game already ended, Invalid input character (non-alphabetic), Repeated letter guesses, Empty word initialization, Empty word |