-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordle.js
More file actions
181 lines (147 loc) · 5.17 KB
/
wordle.js
File metadata and controls
181 lines (147 loc) · 5.17 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const tds = document.getElementsByTagName("td");
let coulmn = 0;
let row = 0;
let correctWord = "";
let hint = "";
let dict = "";
let color = "2px solid black";
const modeButton = document.getElementById("dark-btn");
const helpButton = document.getElementById("help-btn");
const helpScreen = document.getElementById("help-screen");
const hintButton = document.getElementById("hint-btn");
const hintScreen = document.getElementById("hint-screen");
const StartOverButton = document.getElementById("startover-btn");
async function getData() {
StartOverButton.disabled = true;
StartOverButton.style.cursor = "not-allowed";
StartOverButton.innerText = "Loading...";
const res = await fetch("https://api.masoudkf.com/v1/wordle", {
headers: {
"x-api-key": "sw0Tr2othT1AyTQtNDUE06LqMckbTiKWaVYhuirv",
},
});
const data = await res.json();
dict = data.dictionary;
StartOverButton.disabled = false;
StartOverButton.style.cursor = "pointer";
StartOverButton.innerText = "Start Over";
return dict;
}
async function main() {
dict = await getData();
startGame(dict);
modeButton.addEventListener("click", () => {
document.body.classList.toggle("dark-mode");
is_dark = document.body.classList.contains("dark-mode");
is_dark ? color = "2px solid green": color = "2px solid black";
tds[coulmn + 4*row].style.border = color;
is_dark ? modeButton.style.color = "white" : modeButton.style.color = "black";
is_dark ? helpButton.style.color = "white" : helpButton.style.color = "black";
is_dark ? hintButton.style.color = "white" : hintButton.style.color = "black";
modeButton.blur();
});
helpButton.addEventListener("click", () => {
helpScreen.classList.toggle("hider");
helpButton.blur();
});
hintButton.addEventListener("click", () => {
hintScreen.classList.toggle("hider");
hintButton.blur();
});
StartOverButton.addEventListener("click", () => {
resetEverything();
StartOverButton.blur();
});
tds[coulmn + 4*row].style.border = color;
document.addEventListener("keyup", (event) => {
if (event.key === "Enter") {
if (coulmn < 4){
alert("first complete the word");
return;
}
check_word();
coulmn = 0;
row += 1;
tds[coulmn + 4*row].style.border = color;
return;
}
if(event.key === "Backspace" && coulmn > 0) {
if (coulmn < 4) tds[coulmn + 4*row].style.border = "1px solid rgb(208, 211, 212)";
coulmn -= 1;
tds[coulmn + 4*row].style.border = color;
tds[coulmn + 4*row].innerText = "";
return;
}
const regex = new RegExp('[a-zA-Z]');
if (coulmn > 3 || !(regex.test(event.key)) || event.key.length > 1) return;
const keyName = event.key.toUpperCase();
tds[coulmn + 4*row].innerText = keyName;
tds[coulmn + 4*row].style.border = "1px solid rgb(208, 211, 212)";
coulmn+= 1;
if (coulmn < 4) tds[coulmn + 4*row].style.border = color;
});
}
function check_word() {
let word = "";
for (let i = 0; i < 4; i++) {
cell = tds[i + 4*row];
letter = cell.innerText;
word += letter;
if (correctWord[i] == letter) {
cell.style.backgroundColor = "green";
cell.style.color = "white";
}
else if (correctWord.includes(letter)) {
cell.style.backgroundColor = "#ffffb3";
cell.style.color = "black";
}
else {
cell.style.backgroundColor = "grey";
cell.style.color = "white";
}
}
if (word == correctWord) {
toggleWinScreen();
} else if(row == 3) {
toggleLoseScreen();
}
}
function toggleLoseScreen(){
document.getElementById("lose-screen").classList.toggle("hider");
}
function toggleWinScreen(){
document.getElementById("table").classList.toggle("hider");
document.getElementById("Win-gif").classList.toggle("hider");
document.getElementById("win-screen").classList.toggle("hider");
}
function togglHintScreen(){
document.getElementById("hint-screen").classList.toggle("hider");
}
function resetEverything(){
document.getElementById("lose-screen").classList.add("hider");
document.getElementById("win-screen").classList.add("hider");
document.getElementById("table").classList.remove("hider");
document.getElementById("Win-gif").classList.add("hider");
for (let i = 0; i < 4; i++) {
for(let j = 0; j < 4; j++){
cell = tds[j + 4*i];
cell.style.backgroundColor = "transparent";
cell.style.border = "1px solid rgb(208, 211, 212)";
cell.style.color = "inherit";
cell.innerText = "";
}
}
coulmn = 0;
row = 0;
tds[coulmn + 4*row].style.border = color;
startGame(dict);
}
function startGame(dictionary){
random = Number.parseInt(Math.random() * dictionary.length);
correctWord = dictionary[random].word.toUpperCase();
hint = dictionary[random].hint;
document.getElementById("lost").innerText = "HAHAHA, You Lost!, The word was: " + correctWord;
document.getElementById("hint").innerText = "Hint: " + hint;
document.getElementById("win").innerText = "Congratulations, You guessed " + correctWord + " right!";
}
main();