WordHunt/WordContainer.gd

52 lines
1.6 KiB
GDScript

extends HBoxContainer
const WORD_LENGTH := 5
const THEME_EMPTY := preload('res://letter_empty.tres')
const THEME_ABSENT := preload('res://letter_absent.tres')
const THEME_PRESENT := preload('res://letter_present.tres')
const THEME_CORRECT := preload('res://letter_correct.tres')
var word := ''
var solution := ''
var guess := ''
onready var letter_btns := get_children()
func set_word(word: String):
self.word = word
for i in WORD_LENGTH:
letter_btns[i].text = word[i] if len(word) > i else ''
func grade_word(solution: String):
assert(self.word.length() == WORD_LENGTH)
if solution.length() < WORD_LENGTH:
self.solution = solution + ' '.repeat(WORD_LENGTH - len(solution))
elif solution.length() == WORD_LENGTH:
self.solution = solution
else:
self.solution = solution.left(WORD_LENGTH)
for i in WORD_LENGTH:
if self.word[i] == self.solution[i]:
self.letter_btns[i].theme = THEME_CORRECT
elif self.word[i] in self.solution:
self.letter_btns[i].theme = THEME_PRESENT
else:
self.letter_btns[i].theme = THEME_ABSENT
func grade_solution(guess: String):
if guess.length() < WORD_LENGTH:
self.guess = guess + ' '.repeat(WORD_LENGTH - len(guess))
elif guess.length() == WORD_LENGTH:
self.guess = guess
else:
self.guess = guess.left(WORD_LENGTH)
for i in WORD_LENGTH:
if len(self.word) > i:
if self.word[i] == self.guess[i]:
self.letter_btns[i].theme = THEME_CORRECT
elif self.word[i] in self.guess:
self.letter_btns[i].theme = THEME_PRESENT
else:
self.letter_btns[i].theme = THEME_EMPTY
else:
self.letter_btns[i].theme = THEME_EMPTY