Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import choice
- from time import sleep
- class Hangman:
- """
- A Hangman class.
- """
- _HANGMAN = (
- """
- ------
- | |
- |
- |
- |
- |
- |
- |
- |
- ----------
- """,
- """
- ------
- | |
- | O
- |
- |
- |
- |
- |
- |
- ----------
- """,
- """
- ------
- | |
- | O
- | -+-
- |
- |
- |
- |
- |
- ----------
- """,
- """
- ------
- | |
- | O
- | /-+-
- |
- |
- |
- |
- |
- ----------
- """,
- """
- ------
- | |
- | O
- | /-+-/
- |
- |
- |
- |
- |
- ----------
- """,
- """
- ------
- | |
- | O
- | /-+-/
- | |
- |
- |
- |
- |
- ----------
- """,
- """
- ------
- | |
- | O
- | /-+-/
- | |
- | |
- | |
- | |
- |
- ----------
- """,
- """
- ------
- | |
- | O
- | /-+-/
- | |
- | |
- | | |
- | | |
- |
- ----------
- """)
- _WORDS = ("APPLE", "ORACLE", "MIMO", "TESLA")
- _POSITIVE_SAYINGS = ("Well done!", "Awesome!", "You Legend!")
- def __init__(self):
- """
- The Python constructor for this class.
- """
- self._word = choice(self._WORDS)
- self._so_far = "-" * len(self._word)
- self._used = []
- self._wrong_answers = 0
- def play(self):
- """
- This is the main driver of the game.
- Plays the game.
- """
- self._reset_game()
- self._start_game()
- # The amount of incorrect answers should be no greater than the length
- # of HANGMAN.
- #
- # Use the length of HANGMAN to ensure there's no index
- # overflow error when printing current progress.
- while self._wrong_answers < len(self._HANGMAN) and self._so_far != self._word:
- self._print_current_progress()
- guess = self._user_guess()
- self._check_answer(guess)
- self._print_result()
- self._play_again()
- # ---------------------------------
- # "Private" methods
- def _check_answer(self, guess):
- """
- Checks to see if the user's guess is correct.
- :param guess: User's guess
- """
- if guess in self._word:
- print(choice(self._POSITIVE_SAYINGS), "...Updating word so far...")
- for i in range(len(self._word)):
- if guess == self._word[i]:
- # so_far is spliced this way:
- # so_far [from the start : up until, but not including the
- # position of the correctly guessed letter]
- # + guessed letter
- # + so_far [from the position next to the
- # correctly guessed letter : to the end]
- self._so_far = self._so_far[:i] + guess + self._so_far[i+1:]
- else:
- print("INCORRECT! Try again!")
- self._wrong_answers += 1
- def _play_again(self):
- """
- Asks the user if he or she would like to play again.
- If the user wants to play again, calls play().
- Otherwise, thanks the user for playing.
- """
- print("Would you like to play again?")
- user_input = input("Enter Y for yes or N for no: ").upper()
- if user_input == "Y":
- self.play()
- else:
- print()
- print("Thank you for playing!")
- def _print_current_progress(self):
- """
- Prints the current progress of the game.
- """
- print()
- print(self._HANGMAN[self._wrong_answers])
- print("Word so far: ", self._so_far)
- print("Letters used: ", sorted(self._used))
- def _print_result(self):
- """
- Prints the result (win or lose).
- """
- sleep(1)
- print()
- print("Calculating result...")
- sleep(1)
- print()
- if self._wrong_answers == len(self._HANGMAN):
- print("UNLUCKY! Better luck next time!")
- else:
- print("WINNER! Congratulations!")
- def _reset_game(self):
- """
- Resets the game by calling the constructor.
- """
- self.__init__()
- def _start_game(self):
- """
- Starts the game by printing an introduction
- and asks the user to hit the ENTER key to continue.
- """
- print()
- print("\t\tWelcome to Hangman!")
- print()
- input("Press Enter to START:")
- def _user_guess(self):
- """
- Asks for the user to guess a letter.
- :returns: User's guessed letter.
- """
- guess = input("Guess a letter: ").upper()
- sleep(1) # Time delay - allows user friendly reading
- print()
- while guess in self._used:
- print("Try again... You've already used this letter")
- guess = input("Guess a letter: ").upper()
- sleep(1)
- print()
- self._used.append(guess)
- return guess
- if __name__ == '__main__':
- game = Hangman()
- game.play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement