Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- class Hangman(object):
- HANGMANPARTS = ['''
- -----
- | |
- |
- |
- |
- |
- =========''', '''
- -----
- | |
- O |
- |
- |
- |
- =========''', '''
- -----
- | |
- O |
- | |
- |
- |
- =========''', '''
- -----
- | |
- O |
- /| |
- |
- |
- =========''', '''
- -----
- | |
- O |
- /|\ |
- |
- |
- =========''', '''
- -----
- | |
- O |
- /|\ |
- / |
- |
- =========''', '''
- -----
- | |
- O |
- /|\ |
- / \ |
- |
- =========''']
- #dictionary = '/usr/share/dict/american'
- def printHangmanBoard(HANGMANPARTS,x):
- print(HANGMANPARTS[x])
- def __init__(self, level=5, non_ascii=False, dictionary='/Users/Surya Sampath/Desktop/final project/test.txt'):
- dictionary = open(dictionary,'r')
- self._dictionary = dictionary
- def getRandomWord(self, wordList):
- newWordList = []
- for word in wordList:
- if len(word) >= 6:
- newWordList.append(word) #append will add to the array
- wordIndex = random.randint(0, len(newWordList) - 1)
- return newWordList[wordIndex]
- def displayBoard(self, HANGMANPARTS, missedLetters, correctLetters, mysteryWord):
- print(HANGMANPARTS[len(missedLetters)])
- print()
- print('Missed letters:', end=' ')
- for letter in missedLetters:
- print(letter, end=' ')
- print()
- blanks = '_' * len(mysteryWord)
- for i in range(len(mysteryWord)):
- if mysteryWord[i] in correctLetters:
- blanks = blanks[:i] + mysteryWord[i] + blanks[i+1:]
- for letter in blanks:
- print(letter, end=' ')
- print()
- def getGuess(self, alreadyGuessed):
- while True:
- print('Guess a letter.')
- guess = input()
- guess = guess.lower()
- if len(guess) != 1:
- print('Please enter a single letter.')
- elif guess in alreadyGuessed:
- print('You have already guessed that letter. Choose again.')
- elif guess not in 'abcdefghijklmnopqrstuvwxyz':
- print('Please enter a LETTER.')
- else:
- return guess
- def playAgain(self):
- keepPlay = input("Do you want to play again?")
- if keepPlay == "yes":
- self.play()
- elif keepPlay == "no":
- exit()
- def play(self):
- print('Welcome to HANGMAN')
- missedLetters = ''
- correctLetters = ''
- mysteryWord = self.getRandomWord(self._dictionary)
- gameIsDone = False
- while True:
- self.displayBoard(self.HANGMANPARTS, missedLetters, correctLetters, mysteryWord)
- guess = self.getGuess(missedLetters + correctLetters)
- if guess in mysteryWord:
- correctLetters = correctLetters + guess
- foundAllLetters = True
- for i in range(len(mysteryWord)):
- if mysteryWord[i] not in correctLetters:
- foundAllLetters = False
- break #break terminates the innermost loop
- if foundAllLetters:
- print('Yes! The mystery word is "' + mysteryWord + '"! You have won!')
- gameIsDone = True
- else:
- missedLetters = missedLetters + guess
- if len(missedLetters) == len(self.HANGMANPARTS) - 1:
- self.displayBoard(self.HANGMANPARTS, missedLetters, correctLetters, mysteryWord)
- print('You have run out of guesses!\nAfter ' + str(len(missedLetters)) + ' missed guesses and ' + str(len(correctLetters)) + ' correct guesses, the word was "' + mysteryWord + '"')
- gameIsDone = True
- if gameIsDone:
- if self.playAgain():
- missedLetters = ''
- correctLetters = ''
- gameIsDone = False
- mysteryWord = self.getRandomWord(self._dictionary)
- else:
- break
- h = Hangman()
- h.play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement