uccjshrimpton

Hangman

Mar 7th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.31 KB | None | 0 0
  1. def Title():
  2.     print("""
  3. __    __       ___      .__   __.   _______ .___  ___.      ___      .__   __.
  4. |  |  |  |     /   \    |  \ |  |  /  _____||   \/   |     /   \    |  \ |  |
  5. |  |__|  |    /  ^  \   |   \|  | |  |  __  |  \ /  |    /  ^  \   |   \|  |
  6. |   __   |   /  /_\ \  |  . `  | |  | |_ | |  |\/|  |   /  /_\ \  |  . `  |
  7. |  |  |  |  /  _____  \ |  |\  | |  |__| | |  |  |  |  /  _____  \ |  |\  |
  8. |__|  |__| /__/     \__\ |__| \__|  \______| |__|  |__| /__/     \__\ |__| \__|
  9.                                                                                                    
  10. """)
  11.  
  12. def Win():
  13.     print("""
  14. ____    ____  __    ______ .___________.  ______   .______     ____    ____
  15. \  \ /   / |  |  /      ||           | /  __  \ |   _  \   \  \ /   /
  16. \  \/   /  |  | |  ,----'`---|  |----`|  |  |  | |  |_)  |    \  \/   /  
  17.  \     /   |  | |  |         |  |     |  |  |  | |      /      \_    _/  
  18.   \   /    |  | |  `----.    |  |     |  `--'  | |  |\ \----.   |  |    
  19.    \__/     |__|  \______|    |__|      \______/  | _| `._____|   |__|    
  20.                                                                            
  21. """)
  22.  
  23. def Lose():
  24.     print("""
  25. _______   _______  _______  _______     ___   .___________.
  26. |       \ |   ____||   ____||   ____|   /   \ |           |
  27. |  .--.  ||  |__   |  |__   |  |__     /  ^  \ `---|  |----`
  28. |  |  |  ||   __|  |   __|  |   __|   /  /_\ \   |  |    
  29. |  '--'  ||  |____ |  |     |  |____ /  _____  \  |  |    
  30. |_______/ |_______||__|     |_______/__/     \__\ |__|    
  31.                                                                                        
  32. """)
  33.  
  34. def Zero():
  35.     print("""
  36. 0000000000000
  37. 0           0
  38. 0
  39. 0
  40. 0
  41. 0
  42. 0
  43. 0
  44. 0
  45. 0
  46. 0
  47. 0
  48. 0
  49. 0
  50. 0
  51. """)
  52.  
  53. def One():
  54.     print("""
  55. 0000000000000
  56. 0           0
  57. 0           1
  58. 0          1 1
  59. 0           1
  60. 0
  61. 0
  62. 0
  63. 0
  64. 0
  65. 0
  66. 0
  67. 0
  68. 0
  69. 0
  70. """)
  71.  
  72. def Two():
  73.     print("""
  74. 0000000000000
  75. 0           0
  76. 0           1
  77. 0          1 1
  78. 0           1
  79. 0           2
  80. 0           2
  81. 0           2
  82. 0
  83. 0
  84. 0
  85. 0
  86. 0
  87. 0
  88. 0
  89. """)
  90.  
  91. def Three():
  92.     print("""
  93. 0000000000000
  94. 0           0
  95. 0           1
  96. 0          1 1
  97. 0           1
  98. 0          32
  99. 0         3 2
  100. 0        3  2
  101. 0
  102. 0
  103. 0
  104. 0
  105. 0
  106. 0
  107. 0
  108. """)
  109.  
  110. def Four():
  111.     print("""
  112. 0000000000000
  113. 0           0
  114. 0           1
  115. 0          1 1
  116. 0           1
  117. 0          324
  118. 0         3 2 4
  119. 0        3  2  4
  120. 0
  121. 0
  122. 0
  123. 0
  124. 0
  125. 0
  126. 0
  127. """)
  128.  
  129. def Five():
  130.     print("""
  131. 0000000000000
  132. 0           0
  133. 0           1
  134. 0          1 1
  135. 0           1
  136. 0          324
  137. 0         3 2 4
  138. 0        3  2  4
  139. 0          5
  140. 0         5
  141. 0        5
  142. 0       5
  143. 0
  144. 0
  145. 0
  146. """)
  147.  
  148. def Six():
  149.     print("""
  150. 0000000000000
  151. 0           0
  152. 0           1
  153. 0          1 1
  154. 0           1
  155. 0          324
  156. 0         3 2 4
  157. 0        3  2  4
  158. 0          5 6
  159. 0         5   6
  160. 0        5     6
  161. 0       5       6
  162. 0
  163. 0
  164. 0
  165. """)
  166.  
  167. def Clear():
  168.     for i in range(50):
  169.         print("\n")
  170.  
  171. def Replay():
  172.     print("\n\nWould you like to play again?")
  173.     sleep(2)
  174.     choice = input("-(Y)es\n-(N)o\n")
  175.     sleep(2)
  176.     if choice.lower() == "y":
  177.         choice = input("\nWould you like to play...\n-(O)ne player\n-(T)wo player\n")
  178.         sleep(2)
  179.         if choice.lower() == "o":
  180.             Clear()
  181.             GameIntro(1)
  182.         elif choice.lower() == "t":
  183.             Clear()
  184.             GameIntro(2)
  185.         else:
  186.             print("You have not entered a valid option, please try again.")
  187.             Replay()
  188.     elif choice.lower() == "n":
  189.         sleep(2)
  190.         print("\nThanks for playing hang man. Good bye.")
  191.         sleep(2)
  192.         quit()
  193.  
  194.     else:
  195.         print("\nYou have not entered a valid option, please try again.")
  196.         Replay()
  197.  
  198. def OnePlayer():
  199.     print("A random word is being chosen for you...")
  200.     randomwords = ["dancing","cheering","swinging","jumping","partying","blasting","crafting","working","jumping","programming","nerding","hopping","playing"]
  201.     word = choice(randomwords)
  202.     return word
  203.    
  204. def TwoPlayer():
  205.     validcount = 0
  206.     validword = False
  207.     while validword == False:
  208.         word = getpass("One player must enter a word for the other player to guess. Don't worry, what you type will not be visible on the screen.\n").lower()
  209.         if len(word) < 5:
  210.             sleep(1)
  211.             print("Your word must be 4 letters or more in length. Please try again.")
  212.             word = input("One player must enter a word for the other player to guess. Don't worry, what you type will not be visible on the screen.\n").lower()
  213.             sleep(1)
  214.         for char in word:
  215.             if char.isalpha() != True:
  216.                 if char == " ":
  217.                     validcount += 1
  218.             else:
  219.                 validcount += 1
  220.         if validcount < len(word):
  221.             validcount = 0
  222.             sleep(1)
  223.             print("\nYour word can only contain letters and spaces. Please try again.\n")
  224.             sleep(1)
  225.         else:
  226.             validword = True
  227.     return word
  228.  
  229. def GameIntro(players):
  230.     if players == 1:
  231.         word = OnePlayer()
  232.     elif players == 2:
  233.         word = TwoPlayer()
  234.     else:
  235.         print("A critical error has occurred. Computer is sorry.")
  236.  
  237.     sleep(2)
  238.     Clear()
  239.     print("\nNow that the word has been determined it's time to begin the game.")
  240.     sleep(2)
  241.     print("You must now begin guess either a single character or the entire word.")
  242.     sleep(2)
  243.     print("\n\nEach guess you make incorrectly will take one of your lives away and put you one step closer to killing the hangman.")
  244.     sleep(2)
  245.     print("Here is your starting point...")
  246.     sleep(2)
  247.     One()
  248.     sleep(8)
  249.     Game(word)
  250.    
  251.  
  252. def Game(word):
  253.     Clear()
  254.     counter = 0
  255.     blanktext = []
  256.     correctguesses = []
  257.     incorrectguesses = []
  258.     totalguesses = 0
  259.     guess = ""
  260.     for char in word:
  261.         blanktext.append("_")
  262.         blanktext.append(" ")
  263.     print("There are "+str(len(word))+" characters in the word.")
  264.     sleep(1)
  265.     print("".join(blanktext))
  266.  
  267.     while totalguesses < 6:
  268.         validcount = 0
  269.         validguess = False
  270.         guess = input("\nPlease guess either a single character or a word.\n")
  271.         sleep(1)
  272.         while validguess == False:
  273.             for char in guess:
  274.                 if char.isalpha() == False:
  275.                     if char == " ":
  276.                         validcount += 1
  277.                 else:
  278.                     validcount += 1
  279.             if validcount < len(guess):
  280.                 print("You have not made a valid guess, please only enter letters or spaces")
  281.                 sleep(1)
  282.                 guess = input("\nPlease guess either a single character or a word.\n")
  283.                 sleep(1)
  284.  
  285.             else:
  286.                 validguess = True
  287.         Clear()
  288.         if len(guess) == 1:
  289.             #evaluation of single character within word-string
  290.             if word.lower().find(guess.lower()) >= 0:
  291.                 if guess.lower() not in correctguesses:
  292.                     counter += 1
  293.                     indexcount = 0
  294.                     for char in word:
  295.                         if char.lower() == guess.lower():
  296.                            blanktext[indexcount*2] = guess
  297.                            correctguesses.append(guess.lower())
  298.                            indexcount += 1
  299.                         else:
  300.                             indexcount += 1
  301.                     if "_" not in blanktext:
  302.                         #Game Won
  303.                         Win()
  304.                         sleep(2)
  305.                         print("You got it right.")
  306.                         sleep(2)
  307.                         print("\nThe word was: "+word)
  308.                         sleep(2)
  309.                         print("\nTotal guesses made:"+str(counter))
  310.                         sleep(2)
  311.                         Replay()
  312.                 else:
  313.                     print("\nYou may not re-use guesses.")
  314.             else:
  315.                 #lose life mechanic
  316.                 if guess.lower() not in incorrectguesses:
  317.                     totalguesses += 1
  318.                     counter += 1
  319.                     incorrectguesses.append(guess.lower())
  320.                     print("\nYou have used up an incorrect guess, the hangman suffers.")
  321.                     sleep(2)
  322.                     if totalguesses == 1:
  323.                         One()
  324.                     elif totalguesses == 2:
  325.                         Two()
  326.                     elif totalguesses == 3:
  327.                         Three()
  328.                     elif totalguesses == 4:
  329.                         Four()
  330.                     elif totalguesses == 5:
  331.                         Five()
  332.                 else:
  333.                     print("\nYou may not re-use guesses.")
  334.  
  335.                 sleep(2)
  336.  
  337.             print("\nLetters left:            "+("".join(blanktext)))
  338.             print("Incorrect guesses used:  "+(",".join(incorrectguesses)))
  339.  
  340.         else:
  341.             #evaluation of entire string compared to word-string
  342.             if guess.lower() == word.lower():
  343.                 counter += 1
  344.                 Win()
  345.                 sleep(2)
  346.                 print("You got it right.")
  347.                 sleep(2)
  348.                 print("\nThe word was: "+word)
  349.                 sleep(2)
  350.                 print("\nTotal guesses made: "+str(counter))
  351.                 sleep(2)
  352.                 Replay()
  353.  
  354.             else:
  355.                 if guess.lower() not in incorrectguesses:
  356.                     counter += 1
  357.                     totalguesses += 1
  358.                     incorrectguesses.append(guess.lower())
  359.                     print("\nYou have used up an incorrect guess, the hangman suffers.")
  360.                     sleep(2)
  361.                     if totalguesses == 1:
  362.                         One()
  363.                     elif totalguesses == 2:
  364.                         Two()
  365.                     elif totalguesses == 3:
  366.                         Three()
  367.                     elif totalguesses == 4:
  368.                         Four()
  369.                     elif totalguesses == 5:
  370.                         Five()
  371.  
  372.             print("\nLetters left:            "+("".join(blanktext)))
  373.             print("Incorrect guesses used:  "+(",".join(incorrectguesses)))
  374.      
  375.     Six()
  376.     sleep(1)
  377.     Lose()
  378.     sleep(2)
  379.     print("\nToo bad, you let the hang man die. Shame on you...")
  380.     sleep(2)
  381.     print("\nTotal guesses made: "+str(counter))
  382.     sleep(2)
  383.     Replay()
  384.  
  385. def Menu():
  386.     Title()
  387.     print("In this Python-based version of the classic game Hangman you are able to choose between playing against someone else or playing against the computer.\n")
  388.     sleep(2)
  389.     choice = input("Would you like to...\n-Play against the (c)omputer.\n-Play against another (p)erson.\n")
  390.     sleep(1)
  391.     while choice.lower() != "c" and choice.lower() != "p":
  392.         print("\nYou have not entered a valid option\n")
  393.         sleep(1)
  394.         choice = input("Would you like to...\n-Play against the (c)omputer.\n-Play against another (p)erson.\n")
  395.     print("")
  396.     if choice == "c":
  397.         GameIntro(1)
  398.     elif choice =="p":
  399.         GameIntro(2)
  400.     else:
  401.         print("A critical error has occurred. Computer is sorry.")
  402.    
  403.  
  404. def Main():
  405.     Menu()
  406.  
  407. from time import sleep
  408. from random import choice
  409. from getpass import getpass
  410.  
  411. Main()
Advertisement
Add Comment
Please, Sign In to add comment