Skip to content

Commit

Permalink
Merge pull request #29 from kirkland-signature/show-guesses
Browse files Browse the repository at this point in the history
Track guesses and prompt when player has reused a guess
  • Loading branch information
anantdark authored Oct 11, 2022
2 parents 98d4db6 + 5780875 commit 8ea6c80
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions hangman-cli/hangman.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,30 @@ def welcome_screen():
def hangman(word):
welcome_screen()
system("clear")
used_letters = []
lives = 6
board = "*"*len(word)

def prompt_with_message(message):
print(f"{message}\nYou still have {lives} lives. Guess again!")
return input()

while lives:
print(f'You have {lives} lives. Guess a word. \nCurrent board\n{board}')
print(f'You have {lives} lives. Guess a word. \nCurrent board',
f'(guessed letters: {" ".join(used_letters)})' if used_letters else '',
f'\n{board}')
guess = input()
# If guess is more than 1 character, prompt to try again
while len(guess) != 1:
print(
f"You can only guess with 1 character\nYou still have {lives} lives. Guess again!")
guess = input()
guess = prompt_with_message("You can only guess with 1 character")
# If character exist in word, then don't lose a life
character_exist = False
# If the player has reused a guess, prompt to try again
while guess in used_letters:
guess = prompt_with_message("You have already used this letter.")

used_letters += guess

for i in range(len(word)):
if guess == word[i]:
# Keep everything before and after index but replace at index
Expand Down

0 comments on commit 8ea6c80

Please sign in to comment.