Advertisement
bwukki

Untitled

Nov 10th, 2017
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.63 KB | None | 0 0
  1. """
  2.   Hangman
  3.     Plays a basic game of hangman
  4.    
  5.   Author:  Curtis Michels
  6.   Version: too many to count
  7.   Date:    11/10/2017
  8. """
  9.  
  10. import random
  11.  
  12. def insertLetter(spaces, letter, pos):
  13.     pos = pos * 2
  14.     spaces = spaces[0:pos] + letter + spaces[pos + 1:]
  15.     return spaces
  16.  
  17. def makeArt():
  18.     art = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
  19.     art[0] = '         +---------+'
  20.     art[1] = '         |         |'
  21.     art[2] = '                   |'
  22.     art[3] = '                   |'
  23.     art[4] = '                   |'
  24.     art[5] = '                   |'
  25.     art[6] = '                   |'
  26.     art[7] = '                   |'
  27.     art[8] = '                   |'
  28.     art[9] = '                   |'
  29.     art[10] = '                   |'
  30.     art[11] = '                   |'
  31.     art[12] = '                   |'
  32.     art[13] = '                   |'
  33.     art[14] = '                   |'
  34.     art[15] = ' ------------------+'
  35.     return art
  36.  
  37. def readWords():
  38.     wordList = []
  39.     with open('wordlist.10000.txt') as wordFile: #wordlist can be found by Google searching "mit wordlist.10000"
  40.         count = 0
  41.         for line in wordFile:
  42.             if len(line) > 6:
  43.                 wordList.append(line[0:-1])
  44.            
  45.     return wordList
  46.  
  47. def printArt(art):
  48.     for line in art:
  49.         print(line)
  50.  
  51. def setParts():
  52.     parts = {
  53.         1 : head,
  54.         2 : body,
  55.         3 : leftArm,
  56.         4 : rightArm,
  57.         5 : leftLeg,
  58.         6 : rightLeg,
  59.         }
  60.     return parts
  61.  
  62. def head(art):
  63.     art[2] = '       +---+       |'
  64.     art[3] = '       +   +       |'
  65.     art[4] = '       +---+       |'
  66.  
  67. def body(art):
  68.     art[5] = '         +         |'
  69.     art[6] = '      +-----+      |'
  70.     art[7] = '      +     +      |'
  71.     art[8] = '      +     +      |'
  72.     art[9] = '      +     +      |'
  73.     art[10] = '      +-----+      |'
  74.  
  75.  
  76. def leftArm(art):
  77.     art[7] = ' -----+     +      |'
  78.     pass
  79.  
  80. def rightArm(art):
  81.     art[7] = ' -----+     +----- |'
  82.     pass
  83.  
  84. def leftLeg(art):
  85.     art[11] = '      |            |'
  86.     art[12] = '      |            |'
  87.     art[13] = '      |            |'
  88.     pass
  89.  
  90. def rightLeg(art):
  91.     art[11] = '      |     |      |'
  92.     art[12] = '      |     |      |'
  93.     art[13] = '      |     |      |'
  94.     pass
  95.  
  96.  
  97.  
  98. def init():
  99.     bodyParts = setParts()
  100.     art = makeArt()
  101.     words = readWords()
  102.     word = words[random.randint(0,len(words))] #chooses a random word from the wordlist
  103.     spaces = "_ " * len(word)
  104.     return bodyParts, art, word, spaces
  105.  
  106. def guess(word, spaces,art,bodyParts):
  107.     misses = 0
  108.     correct = 0
  109.     letters = len(word)
  110.     limit = 6
  111.     win = 0
  112.     previousGuesses = ""
  113.     #print(word) <- for debugging
  114.     while True:
  115.         guess = str.lower(input("letter? "))
  116.         while (guess in previousGuesses): #blocks user from inputting the same thing multiple times to win
  117.             print('You cannot guess the same letter twice')
  118.             guess = str.lower(input("letter? "))
  119.         while (len(guess) != 1 or guess == ' ' ): #makes sure the input is only one character long and not null
  120.             print('Please guess one letter.')
  121.             guess = str.lower(input("letter? "))
  122.         previousGuesses = previousGuesses + guess
  123.         if misses == 5:
  124.             misses = misses+1
  125.             win = 0
  126.             bodyParts[misses](art)
  127.             printArt(art)
  128.             break
  129.         if correct == letters-1:
  130.             win = 1
  131.             finalprint = list(spaces)
  132.             finalprint[len(finalprint)-1] = guess #adds final correct letter
  133.             finalprint[len(finalprint)-2] = '' #deletes the _ character
  134.             art[16] = ''.join(finalprint)
  135.             printArt(art);
  136.             break
  137.         if guess in word:
  138.             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
  139.             for i in range(len(pos)):
  140.                 spaces = insertLetter(spaces,guess,pos[i])
  141.             print(guess,'is in the word!')
  142.             correct = correct + len(pos)
  143.         else:
  144.             print("That letter is not in the word")
  145.             misses = misses + 1
  146.         art[16] = spaces
  147.         if misses > 0:
  148.             bodyParts[misses](art)
  149.         printArt(art);
  150.     return win
  151.        
  152. def main():
  153.     bodyParts, art, word, spaces = init()
  154.     status = guess(word,spaces,art,bodyParts)
  155.     if status == 0:
  156.         print('Y o u L o s e')
  157.     if status == 1:
  158.         print('Y o u W i n !')
  159.     print('Press any key to play again')
  160.     input()
  161.     main();
  162.    
  163. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement