Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Hangman
- Plays a basic game of hangman
- Author: Curtis Michels
- Version: too many to count
- Date: 11/10/2017
- """
- import random
- def insertLetter(spaces, letter, pos):
- pos = pos * 2
- spaces = spaces[0:pos] + letter + spaces[pos + 1:]
- return spaces
- def makeArt():
- art = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
- art[0] = ' +---------+'
- art[1] = ' | |'
- art[2] = ' |'
- art[3] = ' |'
- art[4] = ' |'
- art[5] = ' |'
- art[6] = ' |'
- art[7] = ' |'
- art[8] = ' |'
- art[9] = ' |'
- art[10] = ' |'
- art[11] = ' |'
- art[12] = ' |'
- art[13] = ' |'
- art[14] = ' |'
- art[15] = ' ------------------+'
- return art
- def readWords():
- wordList = []
- with open('wordlist.10000.txt') as wordFile: #wordlist can be found by Google searching "mit wordlist.10000"
- count = 0
- for line in wordFile:
- if len(line) > 6:
- wordList.append(line[0:-1])
- return wordList
- def printArt(art):
- for line in art:
- print(line)
- def setParts():
- parts = {
- 1 : head,
- 2 : body,
- 3 : leftArm,
- 4 : rightArm,
- 5 : leftLeg,
- 6 : rightLeg,
- }
- return parts
- def head(art):
- art[2] = ' +---+ |'
- art[3] = ' + + |'
- art[4] = ' +---+ |'
- def body(art):
- art[5] = ' + |'
- art[6] = ' +-----+ |'
- art[7] = ' + + |'
- art[8] = ' + + |'
- art[9] = ' + + |'
- art[10] = ' +-----+ |'
- def leftArm(art):
- art[7] = ' -----+ + |'
- pass
- def rightArm(art):
- art[7] = ' -----+ +----- |'
- pass
- def leftLeg(art):
- art[11] = ' | |'
- art[12] = ' | |'
- art[13] = ' | |'
- pass
- def rightLeg(art):
- art[11] = ' | | |'
- art[12] = ' | | |'
- art[13] = ' | | |'
- pass
- def init():
- bodyParts = setParts()
- art = makeArt()
- words = readWords()
- word = words[random.randint(0,len(words))] #chooses a random word from the wordlist
- spaces = "_ " * len(word)
- return bodyParts, art, word, spaces
- def guess(word, spaces,art,bodyParts):
- misses = 0
- correct = 0
- letters = len(word)
- limit = 6
- win = 0
- previousGuesses = ""
- #print(word) <- for debugging
- while True:
- guess = str.lower(input("letter? "))
- while (guess in previousGuesses): #blocks user from inputting the same thing multiple times to win
- print('You cannot guess the same letter twice')
- guess = str.lower(input("letter? "))
- while (len(guess) != 1 or guess == ' ' ): #makes sure the input is only one character long and not null
- print('Please guess one letter.')
- guess = str.lower(input("letter? "))
- previousGuesses = previousGuesses + guess
- if misses == 5:
- misses = misses+1
- win = 0
- bodyParts[misses](art)
- printArt(art)
- break
- if correct == letters-1:
- win = 1
- finalprint = list(spaces)
- finalprint[len(finalprint)-1] = guess #adds final correct letter
- finalprint[len(finalprint)-2] = '' #deletes the _ character
- art[16] = ''.join(finalprint)
- printArt(art);
- break
- if guess in word:
- pos = [i for i, x in enumerate(word) if x == guess] #Finds every location of the guess in the word, makes an array called pos with those positions
- for i in range(len(pos)):
- spaces = insertLetter(spaces,guess,pos[i])
- print(guess,'is in the word!')
- correct = correct + len(pos)
- else:
- print("That letter is not in the word")
- misses = misses + 1
- art[16] = spaces
- if misses > 0:
- bodyParts[misses](art)
- printArt(art);
- return win
- def main():
- bodyParts, art, word, spaces = init()
- status = guess(word,spaces,art,bodyParts)
- if status == 0:
- print('Y o u L o s e')
- if status == 1:
- print('Y o u W i n !')
- print('Press any key to play again')
- input()
- main();
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement