From 10723a52553ac2eb773b65203ce1f3d7a0fcc731 Mon Sep 17 00:00:00 2001 From: LupaDevStudio Date: Fri, 31 May 2024 16:46:06 +0200 Subject: [PATCH] add letter limitation and improve find solutions performance --- screens/game.py | 13 ++++++++----- tools/constants.py | 5 +++++ tools/linconym.py | 33 +++++++++++++++++++-------------- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/screens/game.py b/screens/game.py index 191ab03..ebf52d2 100644 --- a/screens/game.py +++ b/screens/game.py @@ -30,7 +30,8 @@ GAMEPLAY_DICT, TUTORIAL, GAME_TUTORIAL_DICT, - USER_STATUS_DICT + USER_STATUS_DICT, + MAX_NB_LETTERS ) from screens.custom_widgets import ( LinconymScreen, @@ -182,7 +183,7 @@ def check_disable_keyboard(self): self.ids.keyboard_layout.activate_delete_button() # Disable the letters is the word is already filled - if len(self.new_word) >= len(self.current_word) + 1: + if len(self.new_word) >= len(self.current_word) + 1 or len(self.new_word) >= MAX_NB_LETTERS: self.ids.keyboard_layout.disable_letters() else: self.ids.keyboard_layout.activate_letters() @@ -225,7 +226,8 @@ def touch_letter(self, letter): # Add the new letter to the current word else: - self.new_word += letter + if len(self.new_word) < MAX_NB_LETTERS: + self.new_word += letter # Disable/Enable the keyboard and the submit button in consequence self.check_disable_keyboard() @@ -237,7 +239,7 @@ def touch_letter(self, letter): def build_word(self): x_center = 0.5 number_mandatory_letters = len(self.current_word) - 1 - number_letters = number_mandatory_letters + 2 + number_letters = min(number_mandatory_letters + 2, MAX_NB_LETTERS) next_letter_counter = len(self.new_word) size_letter = 0.09 horizontal_padding = 0.1 - size_letter @@ -505,7 +507,8 @@ def display_success_popup(self, end_level_dict): popup = LevelUpPopup( primary_color=self.primary_color, secondary_color=self.secondary_color, - number_lincoins_won=compute_lincoins_when_level_up(USER_DATA.user_profile["status"]), + number_lincoins_won=compute_lincoins_when_level_up( + USER_DATA.user_profile["status"]), has_changed_status=has_changed_status, size_hint=size_hint_popup, current_status=current_status, diff --git a/tools/constants.py b/tools/constants.py index 6c1d2d4..c6181c0 100644 --- a/tools/constants.py +++ b/tools/constants.py @@ -635,3 +635,8 @@ def get_lincoin_image_amount(number_lincoins): return PATH_ICONS + "lincoin_4.png" else: return PATH_ICONS + "lincoin_5.png" + + +### Max nb of letters in word ### + +MAX_NB_LETTERS = 10 diff --git a/tools/linconym.py b/tools/linconym.py index f07ad8b..c5cb781 100644 --- a/tools/linconym.py +++ b/tools/linconym.py @@ -36,7 +36,8 @@ REWARD_INTERSTITIAL, REWARD_AD, ANDROID_MODE, - IOS_MODE + IOS_MODE, + MAX_NB_LETTERS ) from tools.basic_tools import ( dichotomy, @@ -284,13 +285,13 @@ def count_different_letters(word1: str, word2: str) -> int: return nb_different_letters -def count_common_letters(word1: str, word2: str) -> int: - res = len(word1) - count_different_letters(word1, word2) - # TEMP - res += (len(word2) - len(word1)) // 2 - if res < 0: - res = 0 - return res +def compute_similarity_score(word1: str, word2: str) -> int: + res = count_different_letters(word1, word2) + res += abs(len(word2) - len(word1)) + score = MAX_NB_LETTERS - res + if score < 0: + score = 0 + return score def level_has_saved_data(level_dict: dict): @@ -330,6 +331,10 @@ def is_valid(new_word: str, current_word: str, skip_in_english_words: bool = Fal True if the word is valid, False otherwise. """ + # Limit the number of letters + if len(new_word) > MAX_NB_LETTERS: + return False + # Check if the new word derives from the current word if len(new_word) - len(current_word) == 0: # Shuffle and change one letter case @@ -418,10 +423,10 @@ def find_solutions(start_word: str, end_word: str, english_words: list = ENGLISH while not_found: current_position = None - for i in sorted(pile.keys()): + for i in sorted(pile.keys(), reverse=True): if len(pile[i]) > 0: - # print(i) - current_position = pile[i].pop(0) + print(i) + current_position = pile[i].pop(-1) break if current_position is None: @@ -434,7 +439,7 @@ def find_solutions(start_word: str, end_word: str, english_words: list = ENGLISH for word in next_words: if word not in words_found or word == end_word: - temp_nb_common_letters = count_common_letters(word, end_word) + similarity_score = compute_similarity_score(word, end_word) new_position = current_position + (new_word_id,) position_to_word_id[new_position] = len(words_found) words_found.append(word) @@ -445,7 +450,7 @@ def find_solutions(start_word: str, end_word: str, english_words: list = ENGLISH print(convert_position_to_wordlist( final_position, position_to_word_id, words_found)) else: - pile_id = -temp_nb_common_letters + len(new_position) + pile_id = similarity_score - len(new_position) # print(pile_id) if pile_id in pile: pile[pile_id].append(new_position) @@ -1171,5 +1176,5 @@ def on_level_completed(self): # fill_daily_games_with_solutions() # print(is_valid("brain", "crane")) - #find_solutions("mermaid", "narwhal", ENGLISH_WORDS_DICTS["34k"]) + find_solutions("mermaid", "narwhal", ENGLISH_WORDS_DICTS["280k"]) pass