Guest User

hangman

a guest
Jun 7th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | None | 0 0
  1. import string
  2.  
  3. _GOWN_MODIFIER = {
  4.     1: (' O ', 2),
  5.     2: (' | ', 3),
  6.     3: ('/| ', 3),
  7.     4: ('/|\\', 3),
  8.     5: ('/  ', 4),
  9.     6: ('/ \\', 4),
  10. }
  11.  
  12.  
  13. SECRET_WORD = 'Hangman'.upper()
  14. ALPHABET = list(string.ascii_uppercase)
  15. HEIGHT = 7
  16. WIDTH = 13
  17.  
  18.  
  19. def insert_correct_guess(guess, temp_secret_word):
  20.     for index, letter in enumerate(SECRET_WORD):
  21.         if letter == guess:
  22.             temp_secret_word[index] = letter
  23.     return temp_secret_word
  24.  
  25.  
  26. def hangman_game():
  27.     temp_secret_word = ['_']*len(SECRET_WORD)
  28.     guessed_letters = []
  29.    
  30.     can_guess = False
  31.     for gown in wrong_answer(WIDTH, HEIGHT):
  32.        
  33.         if not can_guess:
  34.             print gown
  35.            
  36.         while can_guess:
  37.  
  38.             print
  39.             print ' '.join(temp_secret_word)
  40.             print
  41.             print ' Guessed: ' + ' '.join(guessed_letters)
  42.  
  43.             guess = manage_user_input(temp_secret_word)
  44.             guessed_letters.append(guess)
  45.            
  46.             if guess not in SECRET_WORD:
  47.                 print '\n You guessed incorrectly cowboy!'
  48.                 break
  49.            
  50.             print '\n You guessed correctly cowboy!'
  51.             print gown
  52.            
  53.             temp_secret_word = insert_correct_guess(guess, temp_secret_word)
  54.                            
  55.             if ''.join(temp_secret_word) == SECRET_WORD:
  56.                 print ' You won cowboy! '
  57.                 return
  58.        
  59.         if can_guess:
  60.             print gown
  61.         can_guess = True
  62.        
  63.     print ' You died cowboy! '    
  64.     return
  65.  
  66.  
  67. def manage_user_input(guessed_letters):
  68.     valid_guess = False
  69.     while not valid_guess:
  70.         guess = raw_input(' Guess a letter cowboy! : ').upper()
  71.         if guess not in ALPHABET:
  72.             print 'That is not in the alphabet cowboy, please try again!'
  73.         elif guess in guessed_letters:
  74.             print 'You have already tried to guess', guess, 'cowboy, please try again!'
  75.         else:
  76.             valid_guess = True
  77.     return guess
  78.    
  79.    
  80. def wrong_answer(width, height):
  81.     gown = create_gown(width, height)
  82.     half_width = width // 2
  83.  
  84.     yield '\n'.join(gown)
  85.     for attempt in range(1, 7):
  86.         pattern, row = _GOWN_MODIFIER[attempt]
  87.         gown[row] = '{}{}{}{}'.format(' '*(INDENT-1), pattern, ' '*(half_width-len(pattern)+1), '|')
  88.         yield '\n'.join(gown)
  89.  
  90. INDENT = 3
  91.  
  92. def create_gown(width, height):
  93.     half_width = width // 2
  94.     gown = ['{}{}{}'.format(' '*INDENT, ' '*half_width, '|')]*height
  95.     gown[0] = '{}{}'.format(' '*INDENT, '_' * (half_width+1))
  96.     gown[1] = '{}{}{}{}'.format(' '*INDENT, '|',' '*(half_width-1), '|')
  97.     gown[-1] = '{}{}'.format(' '*INDENT, '-' * width)
  98.     return gown
  99.  
  100.  
  101. if __name__ == '__main__':
  102.     hangman_game()
Advertisement
Add Comment
Please, Sign In to add comment