Skip to content

Commit

Permalink
add letter limitation and improve find solutions performance
Browse files Browse the repository at this point in the history
  • Loading branch information
LupaDevStudio committed May 31, 2024
1 parent 468b8fc commit 10723a5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
13 changes: 8 additions & 5 deletions screens/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions tools/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 19 additions & 14 deletions tools/linconym.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
REWARD_INTERSTITIAL,
REWARD_AD,
ANDROID_MODE,
IOS_MODE
IOS_MODE,
MAX_NB_LETTERS
)
from tools.basic_tools import (
dichotomy,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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

0 comments on commit 10723a5

Please sign in to comment.