Created
September 1, 2022 10:09
-
-
Save kofrasa/135b17012c3b19dfd2010f3734570888 to your computer and use it in GitHub Desktop.
Simple Hangman Game
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
#!/usr/bin/env python3 | |
import os | |
from random import randint | |
HANGMANPICS = [''' | |
+---+ | |
| | | |
| | |
| | |
| | |
| | |
=========''', ''' | |
+---+ | |
| | | |
O | | |
| | |
| | |
| | |
=========''', ''' | |
+---+ | |
| | | |
O | | |
| | | |
| | |
| | |
=========''', ''' | |
+---+ | |
| | | |
O | | |
/| | | |
| | |
| | |
=========''', ''' | |
+---+ | |
| | | |
O | | |
/|\ | | |
| | |
| | |
=========''', ''' | |
+---+ | |
| | | |
O | | |
/|\ | | |
/ | | |
| | |
=========''', ''' | |
+---+ | |
| | | |
O | | |
/|\ | | |
/ \ | | |
| | |
========='''] | |
# HANGMANPICS = [" +---+\n | |\n |\n |\n |\n |\n=========", | |
# " +---+\n | |\n O |\n |\n |\n |\n=========", | |
# " +---+\n | |\n O |\n | |\n |\n |\n=========", | |
# " +---+\n | |\n O |\n /| |\n |\n |\n=========", | |
# " +---+\n | |\n O |\n /|\ |\n |\n |\n=========", | |
# " +---+\n | |\n O |\n /|\ |\n / |\n |\n=========", | |
# " +---+\n | |\n O |\n /|\ |\n / \ |\n |\n========="] | |
# Word bank of animals | |
words = ('ant baboon badger bat bear beaver camel cat clam cobra cougar ' | |
'coyote crow deer dog donkey duck eagle ferret fox frog goat ' | |
'goose hawk lion lizard llama mole monkey moose mouse mule newt ' | |
'otter owl panda parrot pigeon python rabbit ram rat raven ' | |
'rhino salmon seal shark sheep skunk sloth snake spider ' | |
'stork swan tiger toad trout turkey turtle weasel whale wolf ' | |
'wombat zebra ').split() | |
def clear(): return os.system('clear') | |
def print_hangman(letters, k): | |
print(HANGMANPICS[k] + "\n") | |
print(f"Misses: {k}\n") | |
print(" ".join(letters)+"\n") | |
def play(): | |
# select random word | |
w = words[randint(0, len(words))] | |
letters = ['_'] * len(w) | |
counter = 0 | |
guesses = set([]) | |
print(f"\nYou are given a word with {len(w)} letters. Start guessing\n") | |
print_hangman(letters, counter) | |
while counter < len(HANGMANPICS)-1 and '_' in letters: | |
g = input("\nguess> ") | |
if not g: | |
continue | |
if g not in guesses and g in w: | |
for i in range(len(w)): | |
if g == w[i]: | |
letters[i] = g | |
clear() | |
print_hangman(letters, counter) | |
# TODO: add affirmations | |
else: | |
counter += 1 | |
clear() | |
print_hangman(letters, counter) | |
guesses.add(g) | |
if '_' not in letters: | |
print("WINNER!!!\n") | |
else: | |
print(f"You have been hanged :(. The word is '{w}'.\n") | |
def main(): | |
print(""" | |
Starting HANGMAN game: | |
Guess the word one letter at a time in six (6) attempts or hang. | |
""") | |
while True: | |
print(""" | |
Options: | |
========= | |
n - new game | |
q - quit game | |
""") | |
action = input("Select option> ") | |
if action == "q": | |
print("Exiting game.") | |
break | |
if action == "n": | |
play() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment