mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 15:37:51 -08:00
Multiple Rounds
This commit is contained in:
@@ -19,11 +19,13 @@ class Canvas:
|
|||||||
for x in range(len(row)):
|
for x in range(len(row)):
|
||||||
row[x] = fill
|
row[x] = fill
|
||||||
|
|
||||||
def draw(self):
|
def render(self):
|
||||||
|
lines = []
|
||||||
for line in self._buffer:
|
for line in self._buffer:
|
||||||
# Joining by the empty string ('') smooshes all of the
|
# Joining by the empty string ('') smooshes all of the
|
||||||
# individual characters together as one line.
|
# individual characters together as one line.
|
||||||
print(''.join(line))
|
lines.append(''.join(line))
|
||||||
|
return '\n'.join(lines)
|
||||||
|
|
||||||
def put(self, s, x, y):
|
def put(self, s, x, y):
|
||||||
# In an effort to avoid distorting the drawn image, only write the
|
# In an effort to avoid distorting the drawn image, only write the
|
||||||
@@ -133,48 +135,76 @@ def play():
|
|||||||
print('HANGMAN')
|
print('HANGMAN')
|
||||||
print('CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n')
|
print('CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n')
|
||||||
|
|
||||||
canvas = Canvas()
|
words_available = set(WORDS)
|
||||||
draw_gallows(canvas)
|
|
||||||
|
|
||||||
word = random.choice(WORDS)
|
|
||||||
letters_used = set()
|
|
||||||
guesses_count = 0
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
print('HERE ARE THE LETTERS YOU USED:')
|
|
||||||
print(', '.join(sorted(letters_used)))
|
|
||||||
print('\n')
|
|
||||||
|
|
||||||
print(revealed_word(word, letters_used))
|
if len(words_available) == 0:
|
||||||
|
print('YOU DID ALL THE WORDS!!')
|
||||||
|
break
|
||||||
|
|
||||||
print('WHAT IS YOUR GUESS', end=QUESTION_PROMPT)
|
# Initize game state for this round
|
||||||
guess = input().upper()
|
canvas = Canvas()
|
||||||
|
draw_gallows(canvas)
|
||||||
|
|
||||||
if guess in letters_used:
|
word = random.choice(list(words_available))
|
||||||
print('YOU GUESSED THAT LETTER BEFORE!')
|
letters_used = set()
|
||||||
continue
|
guesses_count = 0
|
||||||
|
fail_count = 0
|
||||||
|
|
||||||
if guess not in word:
|
while True:
|
||||||
comment, draw_function = PHASES[guesses_count]
|
print('HERE ARE THE LETTERS YOU USED:')
|
||||||
print('\n\nSORRY, THAT LETTER ISN\'T IN THE WORD.')
|
print(', '.join(sorted(letters_used)))
|
||||||
print(comment)
|
print('\n')
|
||||||
draw_function(canvas)
|
|
||||||
canvas.draw()
|
print(revealed_word(word, letters_used))
|
||||||
|
print('\n')
|
||||||
|
|
||||||
|
print('WHAT IS YOUR GUESS', end=QUESTION_PROMPT)
|
||||||
|
guess = input().upper()
|
||||||
|
|
||||||
|
if guess in letters_used:
|
||||||
|
print('YOU GUESSED THAT LETTER BEFORE!')
|
||||||
|
continue
|
||||||
|
|
||||||
guesses_count += 1
|
guesses_count += 1
|
||||||
if guesses_count == len(PHASES):
|
|
||||||
print('SORRY, YOU LOSE. THE WORD WAS', word)
|
|
||||||
break
|
|
||||||
|
|
||||||
letters_used.add(guess)
|
if guess not in word:
|
||||||
print('\n' + revealed_word(word, letters_used))
|
comment, draw_function = PHASES[fail_count]
|
||||||
|
print('\n\nSORRY, THAT LETTER ISN\'T IN THE WORD.')
|
||||||
|
print(comment)
|
||||||
|
draw_function(canvas)
|
||||||
|
print(canvas.render())
|
||||||
|
|
||||||
print('\nWHAT IS YOUR GUESS FOR THE WORD', end=QUESTION_PROMPT)
|
fail_count += 1
|
||||||
guessed_word = input().upper()
|
if fail_count == len(PHASES):
|
||||||
|
print('SORRY, YOU LOSE. THE WORD WAS', word)
|
||||||
|
print('YOU MISSED THAT ONE. DO YOU', end=' ')
|
||||||
|
break
|
||||||
|
|
||||||
if guessed_word != word:
|
letters_used.add(guess)
|
||||||
print('WRONG. TRY ANOTHER LETTER.')
|
if '-' not in revealed_word(word, letters_used):
|
||||||
continue
|
print("YOU FOUND THE WORD!")
|
||||||
|
words_available.remove(word)
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
print('\n' + revealed_word(word, letters_used))
|
||||||
|
print('\n\nWHAT IS YOUR GUESS FOR THE WORD', end=QUESTION_PROMPT)
|
||||||
|
guessed_word = input().upper()
|
||||||
|
|
||||||
|
if guessed_word != word:
|
||||||
|
print('WRONG. TRY ANOTHER LETTER.\n')
|
||||||
|
continue
|
||||||
|
|
||||||
|
print('RIGHT!! IT TOOK YOU {} GUESSES!'.format(guesses_count))
|
||||||
|
words_available.remove(word)
|
||||||
|
break
|
||||||
|
|
||||||
|
print('WANT ANOTHER WORD', end=QUESTION_PROMPT)
|
||||||
|
reply = input().upper()
|
||||||
|
if reply != 'YES':
|
||||||
|
break
|
||||||
|
|
||||||
|
print('\nIT\'S BEEN FUN! BYE FOR NOW.')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user