From 9bfd8071b2e3c0adf27bbd0ba6628fc008efde92 Mon Sep 17 00:00:00 2001 From: shukebeta Date: Sun, 21 May 2023 15:25:43 +1200 Subject: [PATCH 1/4] UI improvement --- .gitignore | 2 +- color_text.py | 39 +++++++++++++ main.py | 152 +++++++++++++++++++++++++------------------------- 3 files changed, 115 insertions(+), 78 deletions(-) create mode 100644 color_text.py diff --git a/.gitignore b/.gitignore index 68bc17f..2dc53ca 100644 --- a/.gitignore +++ b/.gitignore @@ -157,4 +157,4 @@ cython_debug/ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ +.idea/ diff --git a/color_text.py b/color_text.py new file mode 100644 index 0000000..7e53dbb --- /dev/null +++ b/color_text.py @@ -0,0 +1,39 @@ +import os +if os.name == 'nt': + os.system('color') + + +def colored(text, custom_color, bold=False, underline=False, wholeline=False): + color = { + 'GREY': '\033[90m', + 'GRAY': '\033[90m', + 'RED': '\033[91m', + 'GREEN': '\033[92m', + 'YELLOW': '\033[93m', + 'BLUE': '\033[94m', + 'PURPLE': '\033[95m', + 'CYAN': '\033[96m', + 'ENDC': '\033[0m', + 'BOLD': '\033[1m', + 'UNDERLINE': '\033[4m', + } + if bold: + text = color['BOLD'] + text + if underline: + text = color['UNDERLINE'] + text + custom_color = str(custom_color).upper() + if custom_color in color: + text = color[custom_color] + text + elif str(custom_color).isdigit(): + text = '\033[' + custom_color + 'm' + text + if wholeline: + return text + return text + color['ENDC'] + + +if __name__ == '__main__': + for x in range(90,107): + print(colored("hello, world" + " " + str(x), x, True, True, True)) + print(colored("hello, world" + " " + str(x), x, True, False, True)) + print(colored("hello, world" + " " + str(x), x, False, True, True)) + print(colored("hello, world" + " " + str(x), x, False, False, True)) diff --git a/main.py b/main.py index 7621ef9..bfadcf7 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,7 @@ import random +from color_text import colored + +LINE_LENGTH = 51 def get_random_word(): @@ -6,47 +9,42 @@ def get_random_word(): return random.choice(words.readlines()).strip() -def create_blank_word(word): +def show_blank_word(word): return " ".join("_" * len(word)) -def menu_text(live_number): - print("-" * 51) - print(" " * 21 + "MAIN MENU") - print("-" * 51) +def draw_main_menu(live_number): + draw_menu_title("MAIN MENU") print("Enter 1 to play") print(f"Enter 2 to set number of lives, currently {live_number}") print("Enter 0 to quit") -def live_text(): - print("-" * 51) - print(" " * 23 + "LIVES") - print("-" * 51) +def draw_menu_title(title): + draw_color_line('red') + print(" " * int((LINE_LENGTH - len(title)) / 2) + title) + draw_color_line('red') -def game_text(): - print("-" * 50) - print(" " * 23 + "GAME") - print("-" * 50) +def draw_color_line(color, char='-'): + print(colored(char * LINE_LENGTH, color, True)) -def game_options(word, guessed_characters, lives_left): - print("The word is:") - print(word) +def show_game_options(word, guessed_characters, lives_left): print(f"You have tried these options: {guessed_characters}") + lives_left = colored(str(lives_left), 'red', True) print(f"You currently have {lives_left} lives.") print("Enter a character (or word, if you think you know it)") print("Enter 0 to quit") -def get_user_menu_choice(): - user_choice = input("what is your choice: ") - while not user_choice.isnumeric() or not -1 < int(user_choice) < 3: - user_choice = input("what is your choice: ") - if int(user_choice) == 0: +def get_user_input(): + user_input = '' + while not user_input.isnumeric() or not -1 < int(user_input) < 3: + user_input = input("what is your choice: ") + if int(user_input) == 0: quit() - return int(user_choice) + return int(user_input) def get_user_choice(length): @@ -73,7 +71,7 @@ def change_live_number(current_live_number): def duplicate_character_text(duplicate_character, guessed_character): print(f"You have already guessed {duplicate_character}") print(f"You already guessed these {guessed_character}") - new_choice = get_user_choice() + new_choice = get_user_choice(word_length) return new_choice @@ -109,9 +107,7 @@ def game_over_check(remaining_live): def game_over_text_lose(correct_word, guessed_letters): - print("-" * 50) - print("-" * 50) - print("Game Over, you Lost") + draw_menu_title("Game Over, you Lost") print("The correct word is") print(correct_word) print(f"You have tried these options: {guessed_letters}") @@ -119,61 +115,63 @@ def game_over_text_lose(correct_word, guessed_letters): def game_over_text_win(remaining_lives, correct_word): - print("-" * 50) - print("-" * 50) - print("Game Over, you Win") + draw_menu_title("Game Over, you Win") print("The correct word is") print(correct_word) print(f"you still have {remaining_lives} lives left") quit() -word = get_random_word() -blank_word = create_blank_word(word) -word_length = len(word) -word = " ".join(word) -live = 10 -valid_user_choice = [1, 2, 0] -letters_guessed = [] -character_user_guess = "" -word_user_guess = "" - -menu_text(live) -user_choice = get_user_menu_choice() -print(word) - -while user_choice == 2: - live_text() - live = change_live_number(live) - menu_text(live) - user_choice = get_user_menu_choice() - -while True: - game_text() - game_options(blank_word, letters_guessed, live) - user_choice = get_user_choice(word_length) - - while user_choice in letters_guessed: - user_choice = duplicate_character_text(user_choice, letters_guessed) - letters_guessed.append(user_choice) - if len(user_choice) == 1: - if user_choice in word: - character_position = get_character_position(user_choice, word) - blank_word = replace_character(character_position, blank_word, user_choice) - else: - live = wrong_guess_text(user_choice, live) - if not game_over_check(live): - print(f"you now have {live} lives left.") - else: - if " ".join(user_choice) == word: - blank_word = " ".join(user_choice) - else: - live = wrong_guess_text(user_choice, live) - if not game_over_check(live): - print(f"you now have {live} lives left.") - - if word == blank_word: - game_over_text_win(live, word) +if __name__ == '__main__': + import sys + word = get_random_word() + if len(sys.argv) > 1: + print(word) + blank_word = show_blank_word(word) + word_length = len(word) + word = " ".join(word) + max_lives = 10 + valid_user_choice = [1, 2, 0] + letters_guessed = [] + character_user_guess = "" + word_user_guess = "" + + draw_main_menu(max_lives) + user_choice = get_user_input() + print(word) - elif game_over_check(live): - game_over_text_lose(word, letters_guessed) + while user_choice == 2: + draw_menu_title("SET MAX LIVES") + max_lives = change_live_number(max_lives) + draw_main_menu(max_lives) + user_choice = get_user_input() + + while True: + draw_menu_title("GAME") + show_game_options(blank_word, letters_guessed, max_lives) + user_choice = get_user_choice(word_length) + + while user_choice in letters_guessed: + user_choice = duplicate_character_text(user_choice, letters_guessed) + letters_guessed.append(user_choice) + if len(user_choice) == 1: + if user_choice in word: + character_position = get_character_position(user_choice, word) + blank_word = replace_character(character_position, blank_word, user_choice) + else: + max_lives = wrong_guess_text(user_choice, max_lives) + if not game_over_check(max_lives): + print(f"you now have {max_lives} lives left.") + else: + if " ".join(user_choice) == word: + blank_word = " ".join(user_choice) + else: + max_lives = wrong_guess_text(user_choice, max_lives) + if not game_over_check(max_lives): + print(f"you now have {max_lives} lives left.") + + if word == blank_word: + game_over_text_win(max_lives, word) + + elif game_over_check(max_lives): + game_over_text_lose(word, letters_guessed) From be7b071b0976d348dde3603e7feaa4971b9cc9be Mon Sep 17 00:00:00 2001 From: shukebeta Date: Wed, 24 May 2023 20:05:52 +1200 Subject: [PATCH 2/4] version 2 from replit.com --- main-replit.py | 201 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 main-replit.py diff --git a/main-replit.py b/main-replit.py new file mode 100644 index 0000000..2075864 --- /dev/null +++ b/main-replit.py @@ -0,0 +1,201 @@ +import random +import math + + +def get_random_word(length): + try: + with open("words.txt", "r") as words: + filtered_words = [word for word in words + if len(word.strip()) == length] + return " ".join(random.choice(filtered_words).strip()) + except: + print_title("ERROR") + print("Something went wrong when opening the file.") + print("Please check if there is a file named word.txt.") + quit() + + +def create_blank_word(length): + return " ".join("_" * length) + + +def menu_options(live_number, length): + print("Enter 1 to play") + print(f"Enter 2 to set number of lives, currently {live_number}") + print(f"Enter 3 to set number of character in word, currently {length}") + print("Enter 0 to quit") + + +def print_title(text): + print("-" * 50) + length = 25 - math.floor(len(text) / 2) + print(" " * length + text) + print("-" * 50) + + +def game_options(word, guessed_characters, lives_left): + print("The word is:") + print(word) + print(f"You have tried these options: {guessed_characters}") + print(f"You currently have {lives_left} lives.") + print("Enter a character (or word, if you think you know it)") + print("Enter 0 to quit") + + +def get_user_menu_choice(): + user_choice = input("what is your choice: ") + while not user_choice.isnumeric() or not -1 < int(user_choice) < 4: + user_choice = input("what is your choice: ") + if int(user_choice) == 0: + quit() + return int(user_choice) + + +def get_user_choice(length): + user_input = "" + while not user_input.isalpha() and not user_input.isnumeric(): + user_input = input("What is your choice: ") + if user_input.isalpha(): + if len(user_input) == 1 or len(user_input) == length: + return user_input + if user_input.isnumeric() and int(user_input) == 0: + quit() + user_input = "" + + +def change_live_number(current_live_number): + print(f"Currently you have {current_live_number} lives.") + new_live_number = input("How many lives do you want: ") + while not new_live_number.isnumeric() or not int(new_live_number) > 0: + new_live_number = input("How many lives do you want: ") + print(f"You now have {new_live_number} lives.") + return int(new_live_number) + + +def change_word_length(current_word_length): + print(f"The word length is {current_word_length} characters") + new_length = input("How many characters do you want in your word: ") + while not new_length.isnumeric() or not 3 < int(new_length) < 15: + print("please enter a number larger than 3 and smaller than 15") + new_length = input("How many characters do you want in your word: ") + print(f"the word length is now {new_length} characters") + return int(new_length) + + +def duplicate_character_text(duplicate_character, guessed_character): + print(f"You have already guessed {duplicate_character}") + print(f"You already guessed these {guessed_character}") + new_choice = get_user_choice(word_length) + return new_choice + + +def get_character_index(character, word): + i = 0 + result = [] + while i < len(word): + if character == word[i]: + result.append(i) + i += 1 + return result + + +def replace_character(index, word, character): + replaced_word = '' + i = 0 + while i < len(word): + if i in index: + replaced_word += character + else: + replaced_word += word[i] + i += 1 + return replaced_word + + +def wrong_guess_text(wrong_char, lives): + print(f"the character {wrong_char}, is not in the word") + return lives - 1 + + +def correct_guess_text(character): + print("-" * 50) + print(f"the character {character} is in the word") + + +def game_over_check(remaining_live): + return remaining_live <= 0 + + +def game_over_text_lose(correct_word, guessed_letters): + print("-" * 50) + print("-" * 50) + print("Game Over, you Lost") + print("The correct word is") + print(correct_word) + print(f"You have tried these options: {guessed_letters}") + quit() + + +def game_over_text_win(remaining_lives, correct_word): + print("-" * 50) + print("-" * 50) + print("Game Over, you Win") + print("The correct word is") + print(correct_word) + print(f"you still have {remaining_lives} lives left") + quit() + + +word_length = 6 +live = 10 + +print_title("MAIN MENU") +menu_options(live, word_length) +user_choice = get_user_menu_choice() + + +while 1 < user_choice < 4: + if user_choice == 3: + print_title("LENGTH") + word_length = change_word_length(word_length) + else: + print_title("LIVE") + live = change_live_number(live) + print_title("MAIN MENU") + menu_options(live, word_length) + user_choice = get_user_menu_choice() + + +word = get_random_word(word_length) +blank_word = create_blank_word(word_length) +letters_guessed = [] + +while True: + print_title("GAME") + game_options(blank_word, letters_guessed, live) + user_choice = get_user_choice(word_length) + + while user_choice in letters_guessed: + user_choice = duplicate_character_text(user_choice, letters_guessed) + letters_guessed.append(user_choice) + if len(user_choice) == 1: + if user_choice in word: + character_index = get_character_index(user_choice, word) + blank_word = replace_character(character_index, blank_word, user_choice) + correct_guess_text(user_choice) + else: + live = wrong_guess_text(user_choice, live) + if not game_over_check(live): + print(f"you now have {live} lives left.") + else: + if " ".join(user_choice) == word: + blank_word = " ".join(user_choice) + else: + live = wrong_guess_text(user_choice, live) + if not game_over_check(live): + print(f"you now have {live} lives left.") + + if word == blank_word: + game_over_text_win(live, word) + + elif game_over_check(live): + game_over_text_lose(word, letters_guessed) From 139b162d1292c200d5e3b430d251942c505a89e0 Mon Sep 17 00:00:00 2001 From: shukebeta Date: Wed, 24 May 2023 21:04:23 +1200 Subject: [PATCH 3/4] improve get_random_word --- main-replit.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/main-replit.py b/main-replit.py index 2075864..ddb0cb1 100644 --- a/main-replit.py +++ b/main-replit.py @@ -1,17 +1,22 @@ import random import math +import os + +dict_file_path = os.path.split(os.path.realpath(__file__))[0] +DICTIONARY_FILE = dict_file_path + os.sep + 'words.txt' + def get_random_word(length): try: - with open("words.txt", "r") as words: - filtered_words = [word for word in words - if len(word.strip()) == length] - return " ".join(random.choice(filtered_words).strip()) + with open(DICTIONARY_FILE, "r") as words: + words = [word.strip() for word in words] + filtered_words = [word for word in words if len(word) == length] + return " ".join(random.choice(filtered_words)) except: print_title("ERROR") - print("Something went wrong when opening the file.") - print("Please check if there is a file named word.txt.") + print("Something went wrong when opening the dictionary file.") + print("Please check {file} existence".format(file=DICTIONARY_FILE)) quit() From f291ca3b2e7d918d0026eaafc073119a6f4d2ec7 Mon Sep 17 00:00:00 2001 From: shukebeta Date: Fri, 26 May 2023 18:07:11 +1200 Subject: [PATCH 4/4] latest changes --- main.py | 193 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 111 insertions(+), 82 deletions(-) diff --git a/main.py b/main.py index bfadcf7..ddb0cb1 100644 --- a/main.py +++ b/main.py @@ -1,54 +1,63 @@ import random -from color_text import colored +import math +import os -LINE_LENGTH = 51 +dict_file_path = os.path.split(os.path.realpath(__file__))[0] +DICTIONARY_FILE = dict_file_path + os.sep + 'words.txt' -def get_random_word(): - with open("words.txt", "r") as words: - return random.choice(words.readlines()).strip() + +def get_random_word(length): + try: + with open(DICTIONARY_FILE, "r") as words: + words = [word.strip() for word in words] + filtered_words = [word for word in words if len(word) == length] + return " ".join(random.choice(filtered_words)) + except: + print_title("ERROR") + print("Something went wrong when opening the dictionary file.") + print("Please check {file} existence".format(file=DICTIONARY_FILE)) + quit() -def show_blank_word(word): - return " ".join("_" * len(word)) +def create_blank_word(length): + return " ".join("_" * length) -def draw_main_menu(live_number): - draw_menu_title("MAIN MENU") +def menu_options(live_number, length): print("Enter 1 to play") print(f"Enter 2 to set number of lives, currently {live_number}") + print(f"Enter 3 to set number of character in word, currently {length}") print("Enter 0 to quit") -def draw_menu_title(title): - draw_color_line('red') - print(" " * int((LINE_LENGTH - len(title)) / 2) + title) - draw_color_line('red') - +def print_title(text): + print("-" * 50) + length = 25 - math.floor(len(text) / 2) + print(" " * length + text) + print("-" * 50) -def draw_color_line(color, char='-'): - print(colored(char * LINE_LENGTH, color, True)) - -def show_game_options(word, guessed_characters, lives_left): +def game_options(word, guessed_characters, lives_left): + print("The word is:") + print(word) print(f"You have tried these options: {guessed_characters}") - lives_left = colored(str(lives_left), 'red', True) print(f"You currently have {lives_left} lives.") print("Enter a character (or word, if you think you know it)") print("Enter 0 to quit") -def get_user_input(): - user_input = '' - while not user_input.isnumeric() or not -1 < int(user_input) < 3: - user_input = input("what is your choice: ") - if int(user_input) == 0: +def get_user_menu_choice(): + user_choice = input("what is your choice: ") + while not user_choice.isnumeric() or not -1 < int(user_choice) < 4: + user_choice = input("what is your choice: ") + if int(user_choice) == 0: quit() - return int(user_input) + return int(user_choice) def get_user_choice(length): - user_input = "a1" + user_input = "" while not user_input.isalpha() and not user_input.isnumeric(): user_input = input("What is your choice: ") if user_input.isalpha(): @@ -56,18 +65,28 @@ def get_user_choice(length): return user_input if user_input.isnumeric() and int(user_input) == 0: quit() - user_input = "a1" + user_input = "" def change_live_number(current_live_number): print(f"Currently you have {current_live_number} lives.") new_live_number = input("How many lives do you want: ") - while not new_live_number.isnumeric() or int(new_live_number) <= 0: + while not new_live_number.isnumeric() or not int(new_live_number) > 0: new_live_number = input("How many lives do you want: ") print(f"You now have {new_live_number} lives.") return int(new_live_number) +def change_word_length(current_word_length): + print(f"The word length is {current_word_length} characters") + new_length = input("How many characters do you want in your word: ") + while not new_length.isnumeric() or not 3 < int(new_length) < 15: + print("please enter a number larger than 3 and smaller than 15") + new_length = input("How many characters do you want in your word: ") + print(f"the word length is now {new_length} characters") + return int(new_length) + + def duplicate_character_text(duplicate_character, guessed_character): print(f"You have already guessed {duplicate_character}") print(f"You already guessed these {guessed_character}") @@ -75,7 +94,7 @@ def duplicate_character_text(duplicate_character, guessed_character): return new_choice -def get_character_position(character, word): +def get_character_index(character, word): i = 0 result = [] while i < len(word): @@ -102,12 +121,19 @@ def wrong_guess_text(wrong_char, lives): return lives - 1 +def correct_guess_text(character): + print("-" * 50) + print(f"the character {character} is in the word") + + def game_over_check(remaining_live): return remaining_live <= 0 def game_over_text_lose(correct_word, guessed_letters): - draw_menu_title("Game Over, you Lost") + print("-" * 50) + print("-" * 50) + print("Game Over, you Lost") print("The correct word is") print(correct_word) print(f"You have tried these options: {guessed_letters}") @@ -115,63 +141,66 @@ def game_over_text_lose(correct_word, guessed_letters): def game_over_text_win(remaining_lives, correct_word): - draw_menu_title("Game Over, you Win") + print("-" * 50) + print("-" * 50) + print("Game Over, you Win") print("The correct word is") print(correct_word) print(f"you still have {remaining_lives} lives left") quit() -if __name__ == '__main__': - import sys - word = get_random_word() - if len(sys.argv) > 1: - print(word) - blank_word = show_blank_word(word) - word_length = len(word) - word = " ".join(word) - max_lives = 10 - valid_user_choice = [1, 2, 0] - letters_guessed = [] - character_user_guess = "" - word_user_guess = "" - - draw_main_menu(max_lives) - user_choice = get_user_input() - print(word) +word_length = 6 +live = 10 - while user_choice == 2: - draw_menu_title("SET MAX LIVES") - max_lives = change_live_number(max_lives) - draw_main_menu(max_lives) - user_choice = get_user_input() - - while True: - draw_menu_title("GAME") - show_game_options(blank_word, letters_guessed, max_lives) - user_choice = get_user_choice(word_length) - - while user_choice in letters_guessed: - user_choice = duplicate_character_text(user_choice, letters_guessed) - letters_guessed.append(user_choice) - if len(user_choice) == 1: - if user_choice in word: - character_position = get_character_position(user_choice, word) - blank_word = replace_character(character_position, blank_word, user_choice) - else: - max_lives = wrong_guess_text(user_choice, max_lives) - if not game_over_check(max_lives): - print(f"you now have {max_lives} lives left.") +print_title("MAIN MENU") +menu_options(live, word_length) +user_choice = get_user_menu_choice() + + +while 1 < user_choice < 4: + if user_choice == 3: + print_title("LENGTH") + word_length = change_word_length(word_length) + else: + print_title("LIVE") + live = change_live_number(live) + print_title("MAIN MENU") + menu_options(live, word_length) + user_choice = get_user_menu_choice() + + +word = get_random_word(word_length) +blank_word = create_blank_word(word_length) +letters_guessed = [] + +while True: + print_title("GAME") + game_options(blank_word, letters_guessed, live) + user_choice = get_user_choice(word_length) + + while user_choice in letters_guessed: + user_choice = duplicate_character_text(user_choice, letters_guessed) + letters_guessed.append(user_choice) + if len(user_choice) == 1: + if user_choice in word: + character_index = get_character_index(user_choice, word) + blank_word = replace_character(character_index, blank_word, user_choice) + correct_guess_text(user_choice) + else: + live = wrong_guess_text(user_choice, live) + if not game_over_check(live): + print(f"you now have {live} lives left.") + else: + if " ".join(user_choice) == word: + blank_word = " ".join(user_choice) else: - if " ".join(user_choice) == word: - blank_word = " ".join(user_choice) - else: - max_lives = wrong_guess_text(user_choice, max_lives) - if not game_over_check(max_lives): - print(f"you now have {max_lives} lives left.") - - if word == blank_word: - game_over_text_win(max_lives, word) - - elif game_over_check(max_lives): - game_over_text_lose(word, letters_guessed) + live = wrong_guess_text(user_choice, live) + if not game_over_check(live): + print(f"you now have {live} lives left.") + + if word == blank_word: + game_over_text_win(live, word) + + elif game_over_check(live): + game_over_text_lose(word, letters_guessed)