From d373e96bd6f09e4eb4560a1e1f0175efb9b0df53 Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Fri, 22 Sep 2023 13:10:58 +0200 Subject: [PATCH] Code for the Hangman game --- hangman-tui/README.md | 3 + hangman-tui/source_code/hangman.py | 182 ++++++++++++++++++++++++++ hangman-tui/source_code/words.txt | 200 +++++++++++++++++++++++++++++ 3 files changed, 385 insertions(+) create mode 100644 hangman-tui/README.md create mode 100644 hangman-tui/source_code/hangman.py create mode 100644 hangman-tui/source_code/words.txt diff --git a/hangman-tui/README.md b/hangman-tui/README.md new file mode 100644 index 0000000000..4a1f63ebcd --- /dev/null +++ b/hangman-tui/README.md @@ -0,0 +1,3 @@ +# Build a Hangman Game for the Command Line in Python + +This folder provides the code examples for the Real Python tutorial [Build a Hangman Game for the Command Line in Python](https://realpython.com/python-hangman/). diff --git a/hangman-tui/source_code/hangman.py b/hangman-tui/source_code/hangman.py new file mode 100644 index 0000000000..03e2785eb2 --- /dev/null +++ b/hangman-tui/source_code/hangman.py @@ -0,0 +1,182 @@ +import string +from random import choice + +MAX_INCORRECT_GUESSES = 6 + + +def select_word(): + with open("words.txt", mode="r") as words: + word_list = words.readlines() + return choice(word_list).strip() + + +def get_player_input(guessed_letters): + while True: + player_input = input("Guess a letter: ").lower() + if _validate_input(player_input, guessed_letters): + break + return player_input + + +def _validate_input(player_input, guessed_letters): + return ( + len(player_input) == 1 + and player_input in string.ascii_lowercase + and player_input not in guessed_letters + ) + + +def join_guessed_letters(guessed_letters): + return " ".join(sorted(guessed_letters)) + + +def build_guessed_word(target_word, guessed_letters): + current_letters = [] + for letter in target_word: + if letter in guessed_letters: + current_letters.append(letter) + else: + current_letters.append("_") + return " ".join(current_letters) + + +def draw_hanged_man(wrong_guesses): + hanged_man = [ + r""" + ----- + | | + | + | + | + | + | + | + | + | +------- +""", + r""" + ----- + | | + O | + | + | + | + | + | + | + | +------- +""", + r""" + ----- + | | + O | + --- | + | | + | | + | + | + | + | +------- +""", + r""" + ----- + | | + O | + --- | +/ | | + | | + | + | + | + | +------- +""", + r""" + ----- + | | + O | + --- | +/ | \ | + | | + | + | + | + | +------- +""", + r""" + ----- + | | + O | + --- | +/ | \ | + | | + --- | +/ | +| | + | +------- +""", + r""" + ----- + | | + O | + --- | +/ | \ | + | | + --- | +/ \ | +| | | + | +------- +""", + ] + + print(hanged_man[wrong_guesses]) + + +def game_over(wrong_guesses, target_word, guessed_letters): + if wrong_guesses == MAX_INCORRECT_GUESSES: + return True + if set(target_word) <= guessed_letters: + return True + return False + + +if __name__ == "__main__": + # Initial setup + target_word = select_word() + guessed_letters = set() + guessed_word = build_guessed_word(target_word, guessed_letters) + wrong_guesses = 0 + print("Welcome to Hangman!") + + # Game loop + while not game_over(wrong_guesses, target_word, guessed_letters): + draw_hanged_man(wrong_guesses) + print(f"Your word is: {guessed_word}") + print( + "Current guessed letters: " + f"{join_guessed_letters(guessed_letters)}\n" + ) + + player_guess = get_player_input(guessed_letters) + if player_guess in target_word: + print("Great guess!") + else: + print("Sorry, it's not there.") + wrong_guesses += 1 + + guessed_letters.add(player_guess) + guessed_word = build_guessed_word(target_word, guessed_letters) + + # Game over + draw_hanged_man(wrong_guesses) + if wrong_guesses == MAX_INCORRECT_GUESSES: + print("Sorry, you lost!") + else: + print("Congrats! You did it!") + print(f"Your word was: {target_word}") diff --git a/hangman-tui/source_code/words.txt b/hangman-tui/source_code/words.txt new file mode 100644 index 0000000000..df56d8e219 --- /dev/null +++ b/hangman-tui/source_code/words.txt @@ -0,0 +1,200 @@ +ugliest +close +dog +massive +hollow +cultured +seashore +explode +dizzy +minister +incompetent +thoughtless +harbor +messy +dance +children +zesty +clean +ball +nostalgic +plan +week +strap +board +slope +bat +steep +mourn +cat +girl +ancient +street +mice +dare +wasteful +tub +limping +whimsical +eager +eggs +detail +experience +beds +train +place +cows +admit +rare +respect +loose +group +enjoy +internal +macabre +imported +superb +crooked +confused +hug +feigned +unkempt +coal +meddle +hapless +country +zealous +sick +pray +lake +tiny +key +empty +labored +delirious +ants +need +omniscient +onerous +damp +subtract +sack +connection +toad +gather +record +new +trashy +flow +river +sparkling +kneel +daughter +glue +allow +raspy +eminent +weak +wrong +pretend +receipt +celery +plain +fire +heal +damaging +honorable +foot +ignorant +substance +box +crime +giant +learned +itchy +smoke +likeable +station +jaded +innocent +dead +straw +tray +chin +pack +geese +guess +wealthy +slippery +book +curly +swing +cure +flowers +rate +ignore +insidious +necessary +snakes +entertaining +rich +comb +lamentable +fuel +camera +multiply +army +exist +sulky +brief +worried +third +magical +wary +laborer +end +somber +authority +rainstorm +anxious +purpose +agreeable +spiky +toe +mixed +waiting +hungry +lopsided +flagrant +windy +ground +slap +please +white +hurry +governor +abandoned +reject +spiritual +abrasive +hunt +weather +endurable +hobbies +occur +bake +print +tire +juicy +blush +listen +trousers +daffy +scarecrow +rude +stem +bustling +nail +sneeze +bellicose +overdose