-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGame.gd
114 lines (95 loc) · 3.42 KB
/
Game.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
extends Node
var word_to_guess := ""
var word_attempt := ""
var current_attempt := 1
var current_letter := 1
@onready var word_rows: Array[HBoxContainer] = [
%WordRow1 as HBoxContainer,
%WordRow2 as HBoxContainer,
%WordRow3 as HBoxContainer,
%WordRow4 as HBoxContainer,
%WordRow5 as HBoxContainer,
%WordRow6 as HBoxContainer,
]
@onready var message := %Message as Label
@onready var keyboard := %Keyboard as Keyboard
func _ready():
word_to_guess = WordList.get_todays_word()
func _input(event: InputEvent):
var key_event := event as InputEventKey
if key_event and key_event.pressed:
if key_event.unicode != 0:
var letter := char(key_event.unicode).to_upper()
if current_letter <= 5:
word_attempt += letter
update_letter_panel(letter, current_attempt, current_letter)
current_letter += 1
elif key_event.keycode == KEY_BACKSPACE:
if current_letter > 1:
current_letter -= 1
word_attempt = word_attempt.left(current_letter - 1)
update_letter_panel("", current_attempt, current_letter)
elif key_event.keycode == KEY_ENTER:
if word_attempt.length() < 5:
message.text = "Type 5 letters"
return
word_attempt = word_attempt.to_lower()
var attempt_result := check_word(word_attempt, word_to_guess)
if attempt_result[0] == Globals.CheckLetter.NOT_CHECKED:
message.text = "Not in dictionary"
return
for i in range(5):
update_color_panel(attempt_result[i], current_attempt, i + 1)
keyboard.change_letter_key_color(word_attempt[i], attempt_result[i])
if word_attempt == word_to_guess:
message.text = "You Win!"
set_process_input(false)
return
current_attempt += 1
if current_attempt > 6:
message.text = "The word was: " + word_to_guess
set_process_input(false)
return
current_letter = 1
word_attempt = ""
message.text = "Godot Wordle"
func check_word(word: String, correct_word: String) -> Array[Globals.CheckLetter]:
var result: Array[Globals.CheckLetter] = [
Globals.CheckLetter.NOT_CHECKED,
Globals.CheckLetter.NOT_CHECKED,
Globals.CheckLetter.NOT_CHECKED,
Globals.CheckLetter.NOT_CHECKED,
Globals.CheckLetter.NOT_CHECKED,
]
var correct_letter_count := {}
if not (word in WordList.DICTIONARY) and not (word in WordList.WORDS):
return result
for letter in correct_word:
correct_letter_count[letter] = correct_letter_count.get(letter, 0) + 1
for i in range(5):
if word[i] == correct_word[i]:
result[i] = Globals.CheckLetter.CORRECT
correct_letter_count[word[i]] -= 1
for i in range(5):
if result[i] == Globals.CheckLetter.CORRECT:
continue
elif word[i] in correct_word and correct_letter_count.get(word[i], 0) > 0:
result[i] = Globals.CheckLetter.WRONG_PLACE
correct_letter_count[word[i]] -= 1
else:
result[i] = Globals.CheckLetter.NOT_IN_WORD
return result
func update_letter_panel(letter: String, attempt_number: int, letter_number: int) -> void:
var label := word_rows[attempt_number - 1].get_node("Letter" + str(letter_number) + "/Letter") as Label
assert(label)
label.text = letter
func update_color_panel(check_letter: int, attempt_number: int, letter_number: int) -> void:
var panel := word_rows[attempt_number - 1].get_node("Letter" + str(letter_number)) as ColorRect
assert(panel)
match check_letter:
Globals.CheckLetter.NOT_IN_WORD:
panel.color = Color.INDIAN_RED
Globals.CheckLetter.WRONG_PLACE:
panel.color = Color.YELLOW
Globals.CheckLetter.CORRECT:
panel.color = Color.YELLOW_GREEN