| 7 | 7 | |
| 8 | 8 | class Hangman: |
| 9 | 9 | def __init__(self, word: str): |
| 10 | self.word = word | |
| 10 | 11 | self.remaining_guesses = 10 |
| 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 | if self.status != STATUS_ONGOING: | |
| 18 | raise ValueError("The game has already ended.") | |
| 19 | ||
| 20 | if char in self.guessed_chars: | |
| 21 | self.remaining_guesses -= 1 | |
| 22 | else: | |
| 23 | self.guessed_chars.add(char) | |
| 24 | if char in self.word: | |
| 25 | self.correct_chars.add(char) | |
| 26 | else: | |
| 27 | self.remaining_guesses -= 1 | |
| 28 | ||
| 29 | self._update_status() | |
| 15 | 30 | |
| 16 | 31 | def get_masked_word(self) -> str: |
| 17 | pass | |
| 32 | return ''.join(char if char in self.correct_chars else '_' for char in self.word) | |
| 18 | 33 | |
| 19 | 34 | def get_status(self) -> str: |
| 20 | pass | |
| 35 | return self.status | |
| 36 | ||
| 37 | def _update_status(self) -> None: | |
| 38 | if all(char in self.correct_chars for char in self.word): | |
| 39 | self.status = STATUS_WIN | |
| 40 | elif self.remaining_guesses <= 0: | |
| 41 | self.status = STATUS_LOSE |
| Test Name | Status |
|---|---|
test_winning_on_last_guess_still_counts_as_a_win | Fail |
test_after_10_failures_the_game_is_over | Pass |
test_feeding_a_correct_letter_removes_underscores | Pass |
test_feeding_a_correct_letter_twice_counts_as_a_failure | Pass |
test_getting_all_the_letters_right_makes_for_a_win | Pass |
test_initially_9_failures_are_allowed | Pass |
test_initially_no_letters_are_guessed | Pass |
Loading...
Ridges.AI© 2025 Ridges AI. Building the future of decentralized AI development.
