Last active
December 8, 2022 00:02
-
-
Save Diapolo10/7ba6c7384b021285e524b88abf155f04 to your computer and use it in GitHub Desktop.
Hangman example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LOGO = """ | |
_ | |
| | | |
| |__ __ _ _ __ __ _ _ __ ___ __ _ _ __ | |
| '_ \ / _` | '_ \ / _` | '_ ` _ \ / _` | '_ \ | |
| | | | (_| | | | | (_| | | | | | | (_| | | | | | |
|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_| | |
__/ | | |
|___/ | |
""" | |
PHRASES = ( | |
"Hello, world!", | |
"Life is simply unfair, don't you think?", | |
"I love this world, because you're in it!", | |
"I love you, and all you guys!", | |
"It amuses me!", | |
"Never leave a debt unpaid!", | |
) | |
STAGES = ( | |
""" | |
■■========== | |
|| | | |
|| O | |
||\ /|\ | |
|| \ / \ | |
■■■■■■■■■■■■ | |
""", | |
""" | |
■■========== | |
|| | | |
|| O | |
||\ /|\ | |
|| \ | |
■■■■■■■■■■■■ | |
""", | |
""" | |
■■========== | |
|| | | |
|| O | |
||\ | | |
|| \ | |
■■■■■■■■■■■■ | |
""", | |
""" | |
■■========== | |
|| | | |
|| | |
||\ | |
|| \ | |
■■■■■■■■■■■■ | |
""", | |
""" | |
■■ | |
|| | |
|| | |
||\ | |
|| \ | |
■■■■■■■■■■■■ | |
""", | |
""" | |
|| | |
||\ | |
|| \ | |
■■■■■■■■■■■■ | |
""", | |
""" | |
|| \ | |
■■■■■■■■■■■■ | |
""", | |
""" | |
■■■■■■■■■■■■ | |
""", | |
""" | |
""", | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
from config import LOGO, PHRASES, STAGES | |
def hangman() -> bool: | |
""" | |
Play a game of Hangman | |
The return value is a boolean that indicates if the game was won. | |
""" | |
lives = len(STAGES)-1 | |
correct_phrase = random.choice(PHRASES).strip() | |
player_phrase = ['_' if char.isalnum() else char for char in correct_phrase] | |
guessed_letters = set() | |
while lives > 0: | |
char = input("Guess a character: ").strip().lower() | |
if len(char) > 1: | |
# Player is attempting to guess the entire phrase | |
if char == correct_phrase.lower(): | |
return True | |
elif len(char) == 1: | |
# Player is guessing a single character | |
if char in guessed_letters: | |
print("You already tried that.") | |
continue | |
found = False | |
guessed_letters.add(char) | |
for idx, c in enumerate(correct_phrase.lower()): | |
if c == char: | |
player_phrase[idx] = correct_phrase[idx] # Preserves casing | |
found = True | |
if not found: | |
print("Character not in phrase") | |
lives -= 1 | |
else: | |
continue | |
print(f"Current status:\n{STAGES[lives]}\nCurrent phrase: {''.join(player_phrase)}\nCharacters tried: {guessed_letters}\n") | |
if ''.join(player_phrase) == correct_phrase: | |
return True | |
return False | |
def main(): | |
print(LOGO) | |
while True: | |
if hangman(): | |
print("You won!") | |
else: | |
print("You lost.") | |
if not input("Play again? (no input = no)").strip(): | |
break | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment