From 9e70d681d6a76391bec3d9c26d1c6a24e77d9f91 Mon Sep 17 00:00:00 2001 From: honey-khatri Date: Thu, 23 Oct 2025 15:12:20 +0530 Subject: [PATCH] added hangman game --- .../Programs/Python/Hangman/READme.md | 36 +++++ .../Programs/Python/Hangman/hangmangame..py | 146 ++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 Domains/CompetitiveProgramming/Programs/Python/Hangman/READme.md create mode 100644 Domains/CompetitiveProgramming/Programs/Python/Hangman/hangmangame..py diff --git a/Domains/CompetitiveProgramming/Programs/Python/Hangman/READme.md b/Domains/CompetitiveProgramming/Programs/Python/Hangman/READme.md new file mode 100644 index 00000000..606e454b --- /dev/null +++ b/Domains/CompetitiveProgramming/Programs/Python/Hangman/READme.md @@ -0,0 +1,36 @@ +**contributor** honey-khatri + + +๐ŸŽฎ Hangman Game in Python +๐Ÿงฉ Overview +Welcome to the classic word-guessing challenge โ€” Hangman! This command-line Python game tests your vocabulary and deduction skills as you try to guess a hidden word before the hangman drawing is completed. +๐Ÿ“ฆ Features +- ๐ŸŽจ ASCII art hangman stages +- ๐Ÿ”ค Random word selection from a built-in list +- โœ… Tracks correct and missed guesses +- ๐Ÿ” Replay option after each game +- ๐Ÿง  Input validation for smooth gameplay +๐Ÿ› ๏ธ How to Run +Make sure Python is installed, then run: +python hangman.py + + +๐ŸŽฎ How to Play +- A word is randomly selected. +- Guess one letter at a time. +- Correct guesses reveal letters in the word. +- Incorrect guesses add parts to the hangman. +- Win by guessing the word before the drawing is complete. +- Lose if the hangman is fully drawn before you solve the word. +๐Ÿงฑ Code Structure +| | | +| get_random_word() | | +| display_board() | | +| get_guess() | | +| play_again() | | +| hangman_game() | | + + +๐Ÿงช Sample Word List +["python", "programming", "hangman", "computer", "challenge", "developer"] + diff --git a/Domains/CompetitiveProgramming/Programs/Python/Hangman/hangmangame..py b/Domains/CompetitiveProgramming/Programs/Python/Hangman/hangmangame..py new file mode 100644 index 00000000..ad597ff6 --- /dev/null +++ b/Domains/CompetitiveProgramming/Programs/Python/Hangman/hangmangame..py @@ -0,0 +1,146 @@ +import random + +# ASCII art for the hangman stages +HANGMAN_PICS = [ + """ + +---+ + | | + | + | + | + | +=========""", + """ + +---+ + | | + O | + | + | + | +=========""", + """ + +---+ + | | + O | + | | + | + | +=========""", + """ + +---+ + | | + O | + /| | + | + | +=========""", + """ + +---+ + | | + O | + /|\\ | + | + | +=========""", + """ + +---+ + | | + O | + /|\\ | + / | + | +=========""", + """ + +---+ + | | + O | + /|\\ | + / \\ | + | +=========""" +] + +# List of words for the game +word_list = ["python", "programming", "hangman", "computer", "challenge", "developer"] + +def get_random_word(word_list): + """Chooses a random word from the given list.""" + return random.choice(word_list).lower() + +def display_board(HANGMAN_PICS, missed_letters, correct_letters, secret_word): + """Displays the current state of the game board.""" + print(HANGMAN_PICS[len(missed_letters)]) + print() + + print("Missed letters:", end=" ") + for letter in missed_letters: + print(letter, end=" ") + print() + + blanks = "_" * len(secret_word) + + for i in range(len(secret_word)): # Replace blanks with correctly guessed letters + if secret_word[i] in correct_letters: + blanks = blanks[:i] + secret_word[i] + blanks[i+1:] + + for letter in blanks: # Show the secret word with blanks and spaces + print(letter, end=" ") + print() + +def get_guess(already_guessed): + """Gets a valid letter guess from the player.""" + while True: + guess = input("Guess a letter: ").lower() + if len(guess) != 1: + print("Please enter a single letter.") + elif guess in already_guessed: + print("You have already guessed that letter. Choose again.") + elif not 'a' <= guess <= 'z': + print("Please enter a letter.") + else: + return guess + +def play_again(): + """Asks the player if they want to play again.""" + print("Do you want to play again? (yes or no)") + return input().lower().startswith('y') + +def hangman_game(): + """Main function to run the Hangman game.""" + print("H A N G M A N") + missed_letters = [] + correct_letters = [] + secret_word = get_random_word(word_list) + game_is_done = False + + while not game_is_done: + display_board(HANGMAN_PICS, missed_letters, correct_letters, secret_word) + + guess = get_guess(missed_letters + correct_letters) + + if guess in secret_word: + correct_letters.append(guess) + # Check if the player has won + found_all_letters = True + for i in range(len(secret_word)): + if secret_word[i] not in correct_letters: + found_all_letters = False + break + if found_all_letters: + print(f"Yes! The secret word is '{secret_word}'! You have won!") + game_is_done = True + else: + missed_letters.append(guess) + # Check if player has lost + if len(missed_letters) == len(HANGMAN_PICS) - 1: + display_board(HANGMAN_PICS, missed_letters, correct_letters, secret_word) + print(f"You have run out of guesses! The word was '{secret_word}'. You lose!") + game_is_done = True + + if play_again(): + hangman_game() + else: + print("Thanks for playing!") + +if __name__ == "__main__": + hangman_game() \ No newline at end of file