Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Mon Aug 05 04:50:15 2019
  5.  
  6. @author: jsandeman
  7.  
  8. Hangman app.
  9.  
  10. This app implements the classic Hangman game, where one player chooses
  11. a word at random and another attempts to guess the word. The guesser may
  12. guess a word at any time, but usually guesses letters to obtain enough clues
  13. to guess the word. If the letter guess is correct, the positions of the letters are
  14. revealed. If the guess is wrong, another segment of a stick figure hanging
  15. from the gallows is drawn. If the drawing is completed before the player
  16. guesses the word, they loose, otherwise, they win.
  17.  
  18. The course exercise specifies that if a letter occurs more than once in
  19. the word, the player must guess each instance, which is different from
  20. the standard rules above. I decided to implement the standard rules as
  21. it poses some additional challenges for the logic and data srtuctures
  22. required.
  23.  
  24. The implementation of this app centers around ascertaining the positions
  25. of each letter in the randomly chosen word. A set is created of each
  26. unique letter, and then the word string is scanned. Each index for each
  27. letter is stored in a list in a dictionary where the keys are the letters.
  28. """
  29. import random, csv
  30.  
  31.  
  32. with open("hangman_word_list.csv") as word_file:
  33. word_data = list(csv.reader(word_file, delimiter=','))
  34. word_list = [word for row in word_data for word in row]
  35.  
  36.  
  37. ### The choice() method selects an item from a list at random
  38. word_to_guess = random.choice(word_list)
  39. word_len = len(word_to_guess)
  40.  
  41. ### The current state of the player's guesses are represented by a
  42. ### list. Guessed letters are stored in their proper positions and
  43. ### unguessed letters are represented by the underscore character.
  44. ###
  45. word_guess_state = []
  46. for i in range(0,word_len):
  47. word_guess_state.append("_")
  48.  
  49. ### This version of hangman does not render a drawing of the gallows.
  50. ### Instead, it simply keeps track of an equivalent number of guesses
  51. ### assuming the following body parts:
  52. ### Head, right eye, left eye, mouth, body, left arm, right arm, left leg,
  53. ### right leg (total 9)
  54. ###
  55. errors_left = 9
  56.  
  57. ### Create a set of the unique letters in the word to be guessed. Then
  58. ### loop through the word and create a dictionary with the letters as
  59. ### keys and a list of the indices where they appear as the values
  60. ###
  61. letters = set(word_to_guess)
  62. letter_indices = {}
  63. for c in letters:
  64. for i, l in enumerate(word_to_guess):
  65. if c == l:
  66. if c not in letter_indices:
  67. letter_indices[c] = [i]
  68. else:
  69. letter_indices[c].append(i)
  70.  
  71.  
  72. ### Now it's time to play! The player may guess either a letter
  73. ### or the whole word
  74. ###
  75.  
  76. while True:
  77. guess = input("Guess a letter or the whole word: ")
  78. guess = guess.lower()
  79. if len(guess) > 1:
  80. if guess == word_to_guess:
  81. print("Congratulations! You won!")
  82. break
  83. else:
  84. if guess not in letters:
  85. print("This letter in not in the word!")
  86. errors_left -= 1
  87. if errors_left < 1:
  88. print("You're out of guesses! Better luck next time.")
  89. print("The word was: " + word_to_guess)
  90. break
  91. else:
  92. print("You have " + str(errors_left) + " guesses remaining.")
  93. print(" ".join(word_guess_state))
  94. else:
  95. print("Good guess!")
  96. indices = letter_indices[guess]
  97. for i in indices:
  98. word_guess_state[i] = guess
  99. print(" ".join(word_guess_state))
  100. if "_" not in word_guess_state:
  101. print("Congratulations, you won!")
  102. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement