Use string for encoded word instead of int

This commit is contained in:
Terry Hearst 2024-03-19 01:20:28 -04:00
parent d8931d3baf
commit 8a2a393f4a
2 changed files with 18 additions and 15 deletions

View file

@ -133,8 +133,7 @@ func parse_custom(value: String) -> bool:
custom_word = word custom_word = word
return true return true
if value.is_valid_hex_number(): var decoded := decode_word(value)
var decoded := decode_word(value.hex_to_int())
if not decoded.is_empty(): if not decoded.is_empty():
custom_word = decoded custom_word = decoded
return true return true
@ -177,10 +176,10 @@ func popcnt(num: int) -> int:
return count return count
## Encodes a word using a very basic "encryption" method. Returns -1 if invalid word ## Encodes a word using a very basic "encryption" method. Returns an empty string if invalid word
func encode_word(word: String) -> int: func encode_word(word: String) -> String:
if word.length() != 5: if word.length() != 5:
return -1 return ""
word = word.to_upper() word = word.to_upper()
# Encode word in base 26 # Encode word in base 26
@ -188,7 +187,7 @@ func encode_word(word: String) -> int:
for i in range(word.length()): for i in range(word.length()):
var letter_ind := word[i].unicode_at(0) - 65 # 65 = ascii "A" var letter_ind := word[i].unicode_at(0) - 65 # 65 = ascii "A"
if letter_ind < 0 or letter_ind >= 26: if letter_ind < 0 or letter_ind >= 26:
return -1 return ""
num += letter_ind * (26 ** i) num += letter_ind * (26 ** i)
# Add an offset so that "aaaaa" isn't just zeroes # Add an offset so that "aaaaa" isn't just zeroes
num += 123456 num += 123456
@ -201,13 +200,17 @@ func encode_word(word: String) -> int:
var bits := popcnt(num) var bits := popcnt(num)
num = num | (bits << 24) num = num | (bits << 24)
return num return String.num_int64(num, 16)
## Decodes an encoded word. Returns an empty string if invalid ## Decodes an encoded word. Returns an empty string if invalid
func decode_word(encoded_word: int) -> String: func decode_word(encoded_word: String) -> String:
var bits := encoded_word >> 24 if not encoded_word.is_valid_hex_number():
var num := encoded_word & 0xffffff return ""
var encoded_word_num := encoded_word.hex_to_int()
var bits := encoded_word_num >> 24
var num := encoded_word_num & 0xffffff
# Verify bit count # Verify bit count
if popcnt(num) != bits: if popcnt(num) != bits:

View file

@ -58,7 +58,7 @@ func _ready() -> void:
if Global.custom_date_str: if Global.custom_date_str:
subtitle.text = Global.custom_date_str subtitle.text = Global.custom_date_str
else: else:
subtitle.text = String.num_int64(Global.encode_word(target_word), 16) subtitle.text = Global.encode_word(target_word)
else: else:
var random_seed = null var random_seed = null
if Global.game_mode == Global.GameMode.DAILY: if Global.game_mode == Global.GameMode.DAILY:
@ -73,7 +73,7 @@ func _ready() -> void:
target_word = Global.generate_word(letter_count, random_seed) target_word = Global.generate_word(letter_count, random_seed)
if Global.game_mode != Global.GameMode.DAILY: if Global.game_mode != Global.GameMode.DAILY:
subtitle.text = String.num_int64(Global.encode_word(target_word), 16) subtitle.text = Global.encode_word(target_word)
current_guess = 0 current_guess = 0
ended = false ended = false