diff --git a/functions.py b/functions.py index e68a685..b7fae4f 100644 --- a/functions.py +++ b/functions.py @@ -49,4 +49,7 @@ def clear(): # for mac and linux(here, os.name is 'posix') else: - _ = system('clear') \ No newline at end of file + _ = system('clear') + +def is_valid_format(binary_string): + return bool(re.fullmatch(r"\([01]{5}\)", binary_string)) \ No newline at end of file diff --git a/gamemodes.py b/gamemodes.py index 395e92f..44fc399 100644 --- a/gamemodes.py +++ b/gamemodes.py @@ -17,8 +17,9 @@ from wordlist import words import random from os import system, name -from functions import binarian -from functions import clear +from functions import binarian, is_valid_format, clear +import re + def norToBin(level): wordsInLevel = words[level].copy() @@ -47,4 +48,80 @@ def binToNor(level): print(f'Yes the word {word} does translate to {userIn}!') else: print(f'No, the word {word} translates to {correctTranslation}, not {userIn}. Better study some more!') - input("Hit ENTER to continue") \ No newline at end of file + input("Hit ENTER to continue") + +def hangman(): + clear() + def display_man(wrong_guesses): + for line in hangman_art[wrong_guesses]: + print(line) + + def display_hint(hint): + print(" ".join(hint)) + + def display_answer(answer): + print(" ".join(answer)) + + #Get a random word + word = random.choice(words[random.randint(1,5)]) + answer = binarian(word) + answer = [chunk + ")" for chunk in answer.split(")") if chunk] + hint = ["(xxxxx)"] * len(answer) + wrong_guesses = 0 + guessed_letters = set() + is_running = True + + hangman_art = { + 0: (" ", + " ", + " "), + 1: (" o ", + " ", + " "), + 2: (" o ", + " | ", + " "), + 3: (" o ", + "/| ", + " "), + 4: (" o ", + "/|\\", + " "), + 5: (" o ", + "/|\\", + "/ "), + 6: (" o ", + "/|\\", + "/ \\")} + + while is_running: + display_man(wrong_guesses) + display_hint(hint) + guess = input("Enter a letter\n>") + if not is_valid_format(guess): + print("Invalid input") + continue + + if guess in guessed_letters: + print(f"{guess} is already guessed") + continue + + guessed_letters.add(guess) + + if guess in answer: + for i in range(len(answer)): + if answer[i] == guess: + hint[i] = guess + else: + wrong_guesses += 1 + + if hint == answer: + display_answer(f"You win, the word was {word}! You had {wrong_guesses}.") + input("Hit ENTER to continue") + is_running = False + elif hint == len(hangman_art)-1: + display_man(wrong_guesses) + print(f"You lose! The word was {word}! You had {wrong_guesses}.") + input("Hit ENTER to continue") + is_running = False + clear() \ No newline at end of file diff --git a/main.py b/main.py index 98908a9..436316e 100644 --- a/main.py +++ b/main.py @@ -16,10 +16,11 @@ from submenus import entoBin, binToEn, translateMenu from functions import clear +from gamemodes import hangman import readchar def main_menu(): - options = ["Translate from English to Binarian","Translate from Binarian to English","Translate","Exit"] + options = ["Translate from English to Binarian","Translate from Binarian to English","Hangman","Translate","Exit"] selected = 0 while True: @@ -47,6 +48,8 @@ def main_menu(): binToEn() elif options[selected] == "Translate": translateMenu() + elif options[selected] == "Hangman": + hangman() if __name__ == "__main__": main_menu()