The problem:
Making a simple hangman game. This is a continuation of the following question: The right way to hang a man. There I just tried to make the ascii-gallow. Now I have tried to write the rest of the code for the hangman game, using the ideas from the answer.
I have tried very hard to produce the following output
_______
| |
|
|
|
|
-------------
Solution: _ _ _ _ _ _ _
Letters used:
Guess a letter cowboy! :
every time the user guesses a letter. This was hard as it varies when
to update the gallow
. Converting the generator to a list solved this problem, but it made the code less readable. I was wondering if it was possible to achieve the ascii above, without such a messy code.
After some suggestions in chat I tried to improve the printing in my code. Particularly I tried
ascii = '''\
{gown}
Solution: {secret}
Letters used: {used}
'''
print ascii.format(gown=gown, secret = ' '.join(temp_secret_word), used = ' '.join(guessed_letters))
Firstly this did produce an output, but I had two problems with it. Firstly it messed up the start of the top of the gallow.
secondly I am really unsure if it is clearer what the code does. I thought having lines longer than 80 lines were considered bad practice.
Questions:
The above discussion boils down into two questions. Recently I have been trying writing more readable code, and minimizing the unnecessary parts.
- How readable is the code? (Particularly logic, unnecessary variable names, reasonable names?, too few/too many functions?)
hangman_game()
uses many prints and wierd logic can I achieve the same output with less clutter?
The code:
import string
SECRET_WORD = 'Hangman'.upper()
ALPHABET = list(string.ascii_uppercase)
HEIGHT = 7
WIDTH = 13
INDENT = 3
_GALLOW_MODIFIER = {
1: (' O ', 2),
2: (' | ', 3),
3: ('/| ', 3),
4: ('/|\\', 3),
5: ('/ ', 4),
6: ('/ \\', 4),
}
def create_gallow(width, height):
half_width = width // 2
gallow = ['{}{}{}'.format(' '*INDENT, ' '*half_width, '|')]*height
gallow[0] = '{}{}'.format(' '*INDENT, '_' * (half_width+1))
gallow[1] = '{}{}{}{}'.format(' '*INDENT, '|', ' '*(half_width-1), '|')
gallow[-1] = '{}{}'.format(' '*INDENT, '-' * width)
return gallow
def next_gallow(width, height):
gallow = create_gallow(width, height)
half_width = width // 2
yield '\n'.join(gallow)
for attempt in range(1, 7):
pattern, row = _GALLOW_MODIFIER[attempt]
gallow[row] = '{}{}{}{}'.format(
' '*(INDENT-1), pattern, ' '*(half_width-len(pattern)+1), '|')
yield '\n'.join(gallow)
def temp_secret(guessed_letters):
temp_secret_word = ['_']*len(SECRET_WORD)
for index, letter in enumerate(SECRET_WORD):
if letter in guessed_letters:
temp_secret_word[index] = letter
return temp_secret_word
def manage_user_input(guessed_letters):
valid_guess = False
while not valid_guess:
guess = raw_input(' Guess a letter cowboy! : ').upper()
if guess not in ALPHABET:
print 'That is not in the alphabet cowboy, please try again!'
elif guess in guessed_letters:
print 'You have already tried to guess', guess, 'cowboy, please try again!'
else:
valid_guess = True
return guess
def hangman_game():
temp_secret_word = ['_']*len(SECRET_WORD)
guessed_letters = []
gallows = list(next_gallow(WIDTH, HEIGHT))
for gallow in gallows[0:-1]:
print gallow
print
print ' Solution: ' + ' '.join(temp_secret_word)
print
print ' Letters used: ' + ' '.join(guessed_letters)
guess = manage_user_input(guessed_letters)
guessed_letters.append(guess)
while guess in SECRET_WORD:
temp_secret_word = temp_secret(guessed_letters)
print '\n You guessed correctly cowboy!'
print
print gallow
print
print ' Solution: ' + ' '.join(temp_secret_word)
print
print ' Letters used: ' + ' '.join(guessed_letters)
if ''.join(temp_secret_word) == SECRET_WORD:
print '\n You won cowboy! '
return
guess = manage_user_input(guessed_letters)
guessed_letters.append(guess)
print '\n You guessed incorrectly cowboy!'
print gallows[-1]
print
print ' Solution: ' + ' '.join(temp_secret_word)
print
print ' Letters used: ' + ' '.join(guessed_letters)
print
print ' You died cowboy! '
return
if __name__ == '__main__':
hangman_game()
insert_correct_guess
to not change in place. Other than that the only changes I made was changing the name of one function, and swap places of a few functions. Always hard to know wheter someone has started writing answers or not. \$\endgroup\$