diff --git a/aufgabe4/hangman.py b/aufgabe4/hangman.py new file mode 100755 index 0000000000000000000000000000000000000000..4664a45501e44271f1685231eb3fe1c28f6863ad --- /dev/null +++ b/aufgabe4/hangman.py @@ -0,0 +1,113 @@ +#!/usr/bin/env python3 + + +import random +import colorama as c + +COL = [c.Fore.RED, c.Fore.GREEN, c.Fore.YELLOW, c.Fore.BLUE, c.Fore.MAGENTA, c.Fore.CYAN, c.Fore.WHITE ] + +def get_random_word(): + + words = ["algebra", "suitcase", "knives", "ninjas", "shampoo", "outdoor", "copycat", "bystander", "skillfulness", "biometrical"] + + return random.choice(words) + + +def the_secret(word, exclude_chars=[]): + + for char in word: + if char in exclude_chars: + continue + word = word.replace(char, '_') + + print(" The Word: {0}".format(word)) + print() + return word + + +def wants_replay(): + play_again = input('Do you want to play again? [y/n]: ') + print() + + if play_again == 'y': + replay = True + else: + replay = False + + return replay + +def print_win(): + print(c.Fore.GREEN + " ~ You won! ~ " + c.Style.RESET_ALL) + print() + +def print_lost(): + print(c.Fore.RED + " ~ You lost! ~ " + c.Style.RESET_ALL) + print() + + +def get_user_guess(): + + guess = str(input("Guess a letter: ")) + + while len(guess) > 1 or not guess.isalpha(): + guess = str(input("Please enter a single letter: ")) + + print() + return guess + + +def print_hello(): + + title = "Welcome to Hangman" + n = "" + + for char in title: + n = n + char + random.choice(COL) + + title = n + c.Style.RESET_ALL + + print() + print(" ~~~~ {} ~~~~ ".format(title)) + print(" Let's play ") + print() + + +def main(): + + replay = True + print_hello() + + while replay: + + attempts = 5 + the_word = get_random_word() + exclude_chars = [] + + while attempts: + + sec = the_secret(the_word, exclude_chars) + + if sec == the_word: + print_win() + break + + user_guess = get_user_guess() + + if user_guess in the_word: + print(c.Fore.GREEN + "Right" + c.Style.RESET_ALL) + print() + exclude_chars.append(user_guess) + else: + attempts -= 1 + print(c.Fore.RED + "Nope" + c.Style.RESET_ALL) + print("Tries left: {}".format(attempts)) + print() + + if not attempts: + print_lost() + break + + replay = wants_replay() + +if __name__ == '__main__': + main()