Added gamemode hangman!

This commit is contained in:
Lyx committed 2025-03-23 09:41:54 +01:00
1 parent effbbeae61
commit 9c8d3fe1a0
3 files changed
+88 -5

No files matched your search

+4 -1
View File
@@ -49,4 +49,7 @@ def clear():
# for mac and linux(here, os.name is 'posix') # for mac and linux(here, os.name is 'posix')
else: else:
_ = system('clear') _ = system('clear')
def is_valid_format(binary_string):
return bool(re.fullmatch(r"\([01]{5}\)", binary_string))
+80 -3
View File
@@ -17,8 +17,9 @@
from wordlist import words from wordlist import words
import random import random
from os import system, name from os import system, name
from functions import binarian from functions import binarian, is_valid_format, clear
from functions import clear import re
def norToBin(level): def norToBin(level):
wordsInLevel = words[level].copy() wordsInLevel = words[level].copy()
@@ -47,4 +48,80 @@ def binToNor(level):
print(f'Yes the word {word} does translate to {userIn}!') print(f'Yes the word {word} does translate to {userIn}!')
else: else:
print(f'No, the word {word} translates to {correctTranslation}, not {userIn}. Better study some more!') print(f'No, the word {word} translates to {correctTranslation}, not {userIn}. Better study some more!')
input("Hit ENTER to continue") 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()
+4 -1
View File
@@ -16,10 +16,11 @@
from submenus import entoBin, binToEn, translateMenu from submenus import entoBin, binToEn, translateMenu
from functions import clear from functions import clear
from gamemodes import hangman
import readchar import readchar
def main_menu(): 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 selected = 0
while True: while True:
@@ -47,6 +48,8 @@ def main_menu():
binToEn() binToEn()
elif options[selected] == "Translate": elif options[selected] == "Translate":
translateMenu() translateMenu()
elif options[selected] == "Hangman":
hangman()
if __name__ == "__main__": if __name__ == "__main__":
main_menu() main_menu()