Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.37 KB | None | 0 0
  1. import random
  2.  
  3. class Hangman(object):
  4.     HANGMANPARTS = ['''
  5.  
  6.  
  7.   -----
  8.  
  9.   |   |
  10.  
  11.       |
  12.  
  13.       |
  14.  
  15.       |
  16.  
  17.       |
  18.  
  19.   =========''', '''
  20.  
  21.  
  22.  
  23.   -----
  24.  
  25.   |   |
  26.  
  27.   O   |
  28.  
  29.       |
  30.  
  31.       |
  32.  
  33.       |
  34.  
  35.   =========''', '''
  36.  
  37.   -----
  38.  
  39.   |   |
  40.  
  41.   O   |
  42.  
  43.   |   |
  44.  
  45.       |
  46.  
  47.       |
  48.  
  49.   =========''', '''
  50.  
  51.  
  52.  
  53.   -----
  54.  
  55.   |   |
  56.  
  57.   O   |
  58.  
  59.  /|   |
  60.  
  61.       |
  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.  
  86.   |   |
  87.  
  88.   O   |
  89.  
  90.  /|\ |
  91.  
  92.  /    |
  93.  
  94.       |
  95.  
  96.   =========''', '''
  97.  
  98.   -----
  99.  
  100.   |   |
  101.  
  102.   O   |
  103.  
  104.  /|\ |
  105.  
  106.  / \ |
  107.  
  108.       |
  109.  
  110.   =========''']
  111.     #dictionary = '/usr/share/dict/american'
  112.      
  113.     def printHangmanBoard(HANGMANPARTS,x):
  114.         print(HANGMANPARTS[x])
  115.  
  116.     def __init__(self, level=5, non_ascii=False, dictionary='/Users/Surya Sampath/Desktop/final project/test.txt'):
  117.         dictionary = open(dictionary,'r')
  118.         self._dictionary = dictionary
  119.  
  120.  
  121.     def getRandomWord(self, wordList):
  122.          newWordList = []
  123.         for word in wordList:
  124.             if len(word) >= 6:
  125.                      newWordList.append(word) #append will add to the array
  126.         wordIndex = random.randint(0, len(newWordList) - 1)
  127.         return newWordList[wordIndex]
  128.  
  129.     def displayBoard(self, HANGMANPARTS, missedLetters, correctLetters, mysteryWord):
  130.          print(HANGMANPARTS[len(missedLetters)])
  131.         print()
  132.  
  133.         print('Missed letters:', end=' ')
  134.         for letter in missedLetters:
  135.             print(letter, end=' ')
  136.  
  137.         print()
  138.  
  139.         blanks = '_' * len(mysteryWord)
  140.  
  141.         for i in range(len(mysteryWord)):
  142.  
  143.             if mysteryWord[i] in correctLetters:
  144.  
  145.                 blanks = blanks[:i] + mysteryWord[i] + blanks[i+1:]
  146.  
  147.  
  148.         for letter in blanks:
  149.  
  150.             print(letter, end=' ')
  151.         print()
  152.     def getGuess(self, alreadyGuessed):
  153.          while True:
  154.             print('Guess a letter.')
  155.             guess = input()
  156.             guess = guess.lower()
  157.             if len(guess) != 1:
  158.                 print('Please enter a single letter.')
  159.             elif guess in alreadyGuessed:
  160.                 print('You have already guessed that letter. Choose again.')
  161.             elif guess not in 'abcdefghijklmnopqrstuvwxyz':
  162.                 print('Please enter a LETTER.')
  163.             else:
  164.                 return guess
  165.  
  166.     def playAgain(self):
  167.         keepPlay = input("Do you want to play again?")
  168.         if keepPlay == "yes":
  169.             self.play()
  170.         elif keepPlay == "no":
  171.             exit()
  172.  
  173.     def play(self):
  174.          print('Welcome to HANGMAN')
  175.         missedLetters = ''
  176.         correctLetters = ''
  177.         mysteryWord = self.getRandomWord(self._dictionary)
  178.          gameIsDone = False
  179.  
  180.         while True:
  181.  
  182.             self.displayBoard(self.HANGMANPARTS, missedLetters, correctLetters, mysteryWord)
  183.             guess = self.getGuess(missedLetters + correctLetters)
  184.              if guess in mysteryWord:
  185.                 correctLetters = correctLetters + guess
  186.  
  187.                 foundAllLetters = True
  188.                 for i in range(len(mysteryWord)):
  189.                     if mysteryWord[i] not in correctLetters:
  190.                         foundAllLetters = False
  191.                         break #break terminates the innermost loop
  192.                 if foundAllLetters:
  193.                     print('Yes! The mystery word is "' + mysteryWord + '"! You have won!')
  194.                     gameIsDone = True
  195.             else:
  196.                 missedLetters = missedLetters + guess
  197.  
  198.                 if len(missedLetters) == len(self.HANGMANPARTS) - 1:
  199.  
  200.                     self.displayBoard(self.HANGMANPARTS, missedLetters, correctLetters, mysteryWord)
  201.                      print('You have run out of guesses!\nAfter ' + str(len(missedLetters)) + ' missed guesses and ' + str(len(correctLetters)) + ' correct guesses, the word was "' + mysteryWord + '"')
  202.                     gameIsDone = True
  203.  
  204.             if gameIsDone:
  205.                 if self.playAgain():
  206.                      missedLetters = ''
  207.                     correctLetters = ''
  208.                     gameIsDone = False
  209.                     mysteryWord = self.getRandomWord(self._dictionary)
  210.                 else:
  211.                     break
  212.  
  213. h = Hangman()
  214. h.play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement