Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. from random import choice
  2. from time import sleep
  3.  
  4.  
  5. class Hangman:
  6. """
  7. A Hangman class.
  8. """
  9. _HANGMAN = (
  10. """
  11. ------
  12. | |
  13. |
  14. |
  15. |
  16. |
  17. |
  18. |
  19. |
  20. ----------
  21. """,
  22. """
  23. ------
  24. | |
  25. | O
  26. |
  27. |
  28. |
  29. |
  30. |
  31. |
  32. ----------
  33. """,
  34. """
  35. ------
  36. | |
  37. | O
  38. | -+-
  39. |
  40. |
  41. |
  42. |
  43. |
  44. ----------
  45. """,
  46. """
  47. ------
  48. | |
  49. | O
  50. | /-+-
  51. |
  52. |
  53. |
  54. |
  55. |
  56. ----------
  57. """,
  58. """
  59. ------
  60. | |
  61. | O
  62. | /-+-/
  63. |
  64. |
  65. |
  66. |
  67. |
  68. ----------
  69. """,
  70. """
  71. ------
  72. | |
  73. | O
  74. | /-+-/
  75. | |
  76. |
  77. |
  78. |
  79. |
  80. ----------
  81. """,
  82. """
  83. ------
  84. | |
  85. | O
  86. | /-+-/
  87. | |
  88. | |
  89. | |
  90. | |
  91. |
  92. ----------
  93. """,
  94. """
  95. ------
  96. | |
  97. | O
  98. | /-+-/
  99. | |
  100. | |
  101. | | |
  102. | | |
  103. |
  104. ----------
  105. """)
  106.  
  107. _WORDS = ("APPLE", "ORACLE", "MIMO", "TESLA")
  108. _POSITIVE_SAYINGS = ("Well done!", "Awesome!", "You Legend!")
  109.  
  110. def __init__(self):
  111. """
  112. The Python constructor for this class.
  113. """
  114. self._word = choice(self._WORDS)
  115. self._so_far = "-" * len(self._word)
  116. self._used = []
  117. self._wrong_answers = 0
  118.  
  119. def play(self):
  120. """
  121. This is the main driver of the game.
  122. Plays the game.
  123. """
  124. self._reset_game()
  125. self._start_game()
  126.  
  127. # The amount of incorrect answers should be no greater than the length
  128. # of HANGMAN.
  129. #
  130. # Use the length of HANGMAN to ensure there's no index
  131. # overflow error when printing current progress.
  132. while self._wrong_answers < len(self._HANGMAN) and self._so_far != self._word:
  133. self._print_current_progress()
  134. guess = self._user_guess()
  135. self._check_answer(guess)
  136.  
  137. self._print_result()
  138. self._play_again()
  139.  
  140. # ---------------------------------
  141. # "Private" methods
  142.  
  143. def _check_answer(self, guess):
  144. """
  145. Checks to see if the user's guess is correct.
  146. :param guess: User's guess
  147. """
  148. if guess in self._word:
  149. print(choice(self._POSITIVE_SAYINGS), "...Updating word so far...")
  150.  
  151. for i in range(len(self._word)):
  152. if guess == self._word[i]:
  153. # so_far is spliced this way:
  154. # so_far [from the start : up until, but not including the
  155. # position of the correctly guessed letter]
  156. # + guessed letter
  157. # + so_far [from the position next to the
  158. # correctly guessed letter : to the end]
  159. self._so_far = self._so_far[:i] + guess + self._so_far[i+1:]
  160.  
  161. else:
  162. print("INCORRECT! Try again!")
  163. self._wrong_answers += 1
  164.  
  165. def _play_again(self):
  166. """
  167. Asks the user if he or she would like to play again.
  168. If the user wants to play again, calls play().
  169. Otherwise, thanks the user for playing.
  170. """
  171.  
  172. print("Would you like to play again?")
  173. user_input = input("Enter Y for yes or N for no: ").upper()
  174.  
  175. if user_input == "Y":
  176. self.play()
  177.  
  178. else:
  179. print()
  180. print("Thank you for playing!")
  181.  
  182. def _print_current_progress(self):
  183. """
  184. Prints the current progress of the game.
  185. """
  186. print()
  187. print(self._HANGMAN[self._wrong_answers])
  188. print("Word so far: ", self._so_far)
  189. print("Letters used: ", sorted(self._used))
  190.  
  191. def _print_result(self):
  192. """
  193. Prints the result (win or lose).
  194. """
  195. sleep(1)
  196. print()
  197. print("Calculating result...")
  198. sleep(1)
  199. print()
  200. if self._wrong_answers == len(self._HANGMAN):
  201. print("UNLUCKY! Better luck next time!")
  202. else:
  203. print("WINNER! Congratulations!")
  204.  
  205. def _reset_game(self):
  206. """
  207. Resets the game by calling the constructor.
  208. """
  209. self.__init__()
  210.  
  211. def _start_game(self):
  212. """
  213. Starts the game by printing an introduction
  214. and asks the user to hit the ENTER key to continue.
  215. """
  216. print()
  217. print("\t\tWelcome to Hangman!")
  218. print()
  219. input("Press Enter to START:")
  220.  
  221. def _user_guess(self):
  222. """
  223. Asks for the user to guess a letter.
  224. :returns: User's guessed letter.
  225. """
  226. guess = input("Guess a letter: ").upper()
  227. sleep(1) # Time delay - allows user friendly reading
  228. print()
  229.  
  230. while guess in self._used:
  231. print("Try again... You've already used this letter")
  232. guess = input("Guess a letter: ").upper()
  233. sleep(1)
  234. print()
  235.  
  236. self._used.append(guess)
  237.  
  238. return guess
  239.  
  240. if __name__ == '__main__':
  241. game = Hangman()
  242.  
  243. game.play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement