-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordle.js
229 lines (193 loc) · 6.33 KB
/
wordle.js
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//Store the data in a matrix, separate the game data from the ui
const dictionary = ['earth','plane','crane','house'];
// DECLARE STATE CLASS
const state = {
secret:dictionary[Math.floor(Math.random()*dictionary.length)],
grid:Array(6).fill().map(() => Array(5).fill('')),
currentRow:0,
currentCol:0,
};
//Display the game state in the actual grid
//Get the box from the dom and set the value of the text in the box
function updateGrid() {
for (let i = 0; i < state.grid.length; i++) {
for (let j = 0; j < state.grid[i].length; j++) {
const box = document.getElementById(`box${i}${j}`);
box.textContent = state.grid[i][j];
}
}
}
// DEFINE A LETTER BOX
function drawBox(container, row, col, letter = '') {
const box = document.createElement('div');
box.className = 'box';
box.textContent = letter;
box.id = `box${row}${col}`;//Don't understand here
container.appendChild(box);
return box;
}
// DRAW GAME GRID
function drawGrid(container) {
const grid = document.createElement('div');
grid.className = 'grid';
for (let i = 0; i < 6; i++) {
for (let j = 0; j < 5; j++) {
drawBox(grid, i, j);
}
}
container.appendChild(grid);
}
async function registerKeyboardEvents() {
document.body.onkeydown = async (e) => {
const key = e.key;
if (key === 'Enter') {
if (state.currentCol === 5) {
var flag;
// Check if input 5 letters
const word = getCurrentWord();
flag = await checkWordValidity(word);
// if(await checkWordValidity(word))
// {
console.log(flag);
// updateGrid();
// }
if(flag){
revealWord(word);//Tell the player if the position right or wrong
state.currentRow++;
state.currentCol = 0;
}
else{
removeRow();
}
}
}
if (key === 'Backspace') {
removeLetter();
}
if (isLetter(key)) {
addLetter(key);
}
updateGrid();
}
}
async function checkWordValidity(word){
const fetchUrl = 'https://api.dictionaryapi.dev/api/v2/entries/en/'+word;
try{
const response = await fetch(fetchUrl);
const data = await response.json();
console.log(data);
if(data.title === 'No Definitions Found'){
alert('Not a valid word');
return false;
}
else return true;
}
catch(error){
console.error(error);
console.log('false 1');
return false;
}
}
function getCurrentWord() {
return state.grid[state.currentRow].reduce((prev, curr) => prev + curr);
}//what's prev and curr?
function isWordValid(word) {
return dictionary.incluereredes(word);
}
// function getNumOfOccurrencesInWord(word, letter) {
// let result = 0;
// for (let i = 0; i < word.length; i++) {
// if (word[i] === letter) {
// result++;
// }
// }
// return result;
// }
// function getPositionOfOccurrence(word, letter, position) {
// let result = 0;
// for (let i = 0; i <= position; i++) {
// if (word[i] === letter) {
// result++;
// }
// }
// return result;
// }
function revealWord(guess) {
const row = state.currentRow;
//WORDLE ANNIMATION BOX HEIGHT SHRIEKING TO ZERO THEN BACK TO NORMAL
const animation_duration = 500; // ms
for (let i = 0; i < 5; i++) {
const box = document.getElementById(`box${row}${i}`);
const letter = box.textContent;
// const numOfOccurrencesSecret = getNumOfOccurrencesInWord(
// state.secret,
// letter
// );
// const numOfOccurrencesGuess = getNumOfOccurrencesInWord(guess, letter);
// const letterPosition = getPositionOfOccurrence(guess, letter, i);
//TIMEOUT DEPENDENT ON THE INDEX OF THE CURRENT LETTER
setTimeout(() => {
// if (
// numOfOccurrencesGuess > numOfOccurrencesSecret &&
// letterPosition > numOfOccurrencesSecret
// ) {
// box.classList.add('empty');
// } else {
if (letter === state.secret[i]) {
box.classList.add('right');
} else if (state.secret.includes(letter)) {
box.classList.add('wrong');
} else {
box.classList.add('empty');
}
// }
}, ((i + 1) * animation_duration) / 2);
//ADD ANIMATED CLASS TO ANY BOX
box.classList.add('animated');
//ANIMATION DELAY DEPEND ON THE INDEX OR THE POSITON OF THE LETTER
box.style.animationDelay = `${(i * animation_duration) / 2}ms`;
}
// Check if win or lose
const isWinner = state.secret === guess;
const isGameOver = state.currentRow === 5;
//SCHEDULE THE FUNCTION TO BE EXECUTED AFTER A SPECIFIC OF TIME
setTimeout(() => {
if (isWinner) {
alert('Congratulations!');
location.reload();
//update grid here!
} else if (isGameOver) {
alert(`Better luck next time! The word was ${state.secret}.`);
}
}, 3 * animation_duration);
}
//CHECK IF INPUT LETTER (REGULAR EXPRESSION)
function isLetter(key) {
return key.length === 1 && key.match(/[a-z]/i);
}
//ADD THE LETTER TO THE GRID
function addLetter(letter) {
if (state.currentCol === 5) return;
state.grid[state.currentRow][state.currentCol] = letter;
state.currentCol++;
}
//REMOVE THE LETTER FROM THE GRID
function removeLetter() {
if (state.currentCol === 0) return;
state.grid[state.currentRow][state.currentCol - 1] = '';
state.currentCol--;
}
function removeRow(){
for(let i=state.currentCol;i>0;i--){
removeLetter();
}
}
//DRWA GAME GRID
function startup() {
const game = document.getElementById('game');
drawGrid(game); //DRWA GAME GRID
registerKeyboardEvents();//LISTEN TO THE KEY DOWN EVENT
console.log(state.secret);
}
//CALL GAME STARTUP FUNCTION
startup();