Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. import random
  2. WORDLIST = ("phone", "laptop", "desktop", "sewer", "television", "never", "guess", "nice", "chair", "car");
  3. WORD = random.choice(WORDLIST)
  4. ACCEPTABLE = ("abcdefghijklmnopqrstuvwxyz")
  5.  
  6. guessed = []
  7. state = 0
  8. hasWon = 0
  9. playedOnce = 0
  10.  
  11. def menu ():
  12. print("""
  13. 1. Easy (9 misses)
  14. 2. Medium (7 misses)
  15. 3. Hard (5 misses)
  16. """)
  17. return int(input("What level do you want to play?:"))
  18.  
  19. def wantsToPlay():
  20. if (not playedOnce):
  21. return 1
  22. l = input("nWould you like to play again? (y/n)")
  23. while (l != "y" and l != "Y" and l != "n" and l != "N"):
  24. l = input("nWould you like to play again? (y/n)")
  25. if (l.lower() == "y"):
  26. return 1
  27. return 0
  28.  
  29. def takeNewLetter():
  30. global state, hasWon
  31. newPrint("So far, you have guessed the following letters...")
  32. for g in guessed:
  33. print(g, end=" ")
  34. letter = input("nnWhat letter would you like to guess next?n")
  35. while (letter in guessed or letter not in ACCEPTABLE):
  36. if (len(letter) > 1):
  37. if (letter.lower() == WORD.lower()):
  38. newPrint("You win!")
  39. hasWon = 1
  40. break
  41. else:
  42. newPrint("Boo... that was wrong... you're dead...")
  43. state = 7
  44. break
  45. else:
  46. if (letter not in ACCEPTABLE):
  47. letter = input("That character is unacceptable. You many only enter lower case letters.n")
  48. else:
  49. letter = input("You have already guessed that letter, try another one...n")
  50. guessed.append(letter)
  51. if (letter not in WORD):
  52. state += 1
  53. return
  54.  
  55. def drawWord():
  56. tempWord = ""
  57. for c in WORD:
  58. if (c in guessed):
  59. tempWord += c + " "
  60. else:
  61. tempWord += "_ "
  62. newPrint(tempWord)
  63. return
  64.  
  65. def drawStickman():
  66. if (state >= 7):
  67. print(" _______")
  68. print("|/ |")
  69. print("| (_)")
  70. print("| |/")
  71. print("| |")
  72. print("| / \")
  73. print("|")
  74. print("|___")
  75. print("Oops. You're dead.")
  76. elif (state == 6):
  77. print(" _______")
  78. print("|/ |")
  79. print("| (_)")
  80. print("| |/")
  81. print("| |")
  82. print("| / ")
  83. print("|")
  84. print("|___")
  85. elif (state == 5):
  86. print(" _______")
  87. print("|/ |")
  88. print("| (_)")
  89. print("| |/")
  90. print("| |")
  91. print("|")
  92. print("|")
  93. print("|___")
  94. elif (state == 4):
  95. print(" _______")
  96. print("|/ |")
  97. print("| (_)")
  98. print("| |/")
  99. print("|")
  100. print("|")
  101. print("|")
  102. print("|___")
  103. elif (state == 3):
  104. print(" _______")
  105. print("|/ |")
  106. print("| (_)")
  107. print("| |")
  108. print("|")
  109. print("|")
  110. print("|")
  111. print("|___")
  112. elif (state == 2):
  113. print(" _______")
  114. print("|/ |")
  115. print("| (_)")
  116. print("|")
  117. print("|")
  118. print("|")
  119. print("|")
  120. print("|___")
  121. elif (state == 2):
  122. print(" _______")
  123. print("|/ |")
  124. print("|")
  125. print("|")
  126. print("|")
  127. print("|")
  128. print("|")
  129. print("|___")
  130. elif (state == 1):
  131. newPrint("As this is your first mistake, I will let you off...")
  132. print(" _______")
  133. print("|/")
  134. print("|")
  135. print("|")
  136. print("|")
  137. print("|")
  138. print("|")
  139. print("|___")
  140. elif (state == 0):
  141. print(" _______")
  142. print("|/")
  143. print("|")
  144. print("|")
  145. print("|")
  146. print("|")
  147. print("|")
  148. print("|___")
  149.  
  150. def hasGuessed():
  151. if (hasWon == 1):
  152. return 1
  153. if (state >= 7):
  154. return 1
  155. for c in WORD:
  156. if (c not in guessed):
  157. return 0
  158. if (len(guessed) == 0):
  159. return 0
  160. return 1
  161.  
  162. def setup_game():
  163. newPrint("Welcome to the Hangman game!")
  164. newPrint("I have chosen a random word from my super secret list, try to guess it before your stickman dies!")
  165.  
  166. def newPrint(message, both = 1):
  167. msg = "n" + message
  168. if (both != 1):
  169. msg += "n"
  170. print(msg)
  171.  
  172. def main():
  173. global guessed, hasWon, state, playedOnce, WORD, WORDLIST
  174. setup_game()
  175. newPrint("My word is " + str(len(WORD)) + " letters long.")
  176. while (wantsToPlay() == 1):
  177. WORD = random.choice(WORDLIST)
  178. guessed = []
  179. playedOnce = 1
  180. hasWon = 0
  181. state = 0
  182. while (hasGuessed() == 0 and state < 7):
  183. drawStickman()
  184. drawWord()
  185. takeNewLetter()
  186. drawStickman()
  187. newPrint("My word was " + WORD)
  188.  
  189. main()
  190.  
  191. def menu():
  192. print("""
  193. 1. Easy (9 misses)
  194. 2. Medium (7 misses)
  195. 3. Hard (5 misses)
  196. """)
  197. while True:
  198. level = input("What level do you want to play?:")
  199. if level in ('1', '2', '3'):
  200. return {'1': 9, '2': 7, '3': 5}[level]
  201. print('Wrong answer!')
  202.  
  203. def main():
  204. global guessed, hasWon, state, playedOnce, WORD, WORDLIST
  205. setup_game()
  206. allowed_misses = menu()
  207. ...
  208. while (hasGuessed() == 0 and state < allowed_misses)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement