forked from surbhibansal/hello-worldle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
285 lines (252 loc) · 8.29 KB
/
index.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
const DIMENSION = 5;
const GUESSES = 6;
const guessedWords = new Array(DIMENSION);
let currRow = 0;
let currCol = 0;
const INCORRECT_LETTER_COLOR = 'rgb(107 104 104)';
const INCORRECT_POSITION_LETTER_COLOR = 'rgb(194 170 12)';
const CORRECT_POSITION_LETTER_COLOR = '#359c35';
const LETTER_TYPE = {
INCORRECT_LETTER: 'INCORRECT_LETTER',
INCORRECT_POSITION: 'INCORRECT_POSITION',
CORRECT_POSITION: 'CORRECT_POSITION'
};
function initGrid() {
for (let i = 0; i < GUESSES; ++i) {
guessedWords[i] = new Array(DIMENSION);
for (let j = 0; j < DIMENSION; ++j) {
guessedWords[i][j] = '';
}
}
}
const KEYPAD_MAPPING = [
['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'],
['A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ''],
['GO', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', 'DEL', '']
];
const usedLetters = new Set();
let solution = [];
function mapSolution() {
const map = new Map();
for (const letter of solution) {
if (map.has(letter)) {
map.set(letter, map.get(letter) + 1)
} else {
map.set(letter, 1);
}
}
return map;
}
function guess(word) {
if (word === solution.join('')) {
updateInfo(`Congrats! You guessed it in ${currRow + 1} ${currRow === 0 ? 'attempt' : 'attempts'}.
Happy new year 2024!`, 3000);
return true;
}
return false;
}
function updateInfo(message, timeout = 1000) {
const infoEl = document.querySelector('.hello_worldle__info');
infoEl.textContent = message;
infoEl.style.display = 'block';
setTimeout(() => {
infoEl.style.display = 'none';
}, timeout)
}
function selectCurrentRowCells() {
return new Array(DIMENSION).fill(null).map(((_, i) =>
document.querySelector(`div[id='${currRow}_${i}']`)));
}
document.addEventListener('keyup', ev => {
if (ev.code === 'Enter') {
onEnter();
} else if (ev.code === 'Backspace') {
onDelete()
}
});
async function isValidWord(word) {
const url = `https://api.dictionaryapi.dev/api/v2/entries/en/${word}`
const res = await fetch(url);
console.log(res.status);
if (res.status === 404) {
return false;
}
return true;
}
function applyShakeAnimation(gridItems) {
gridItems.forEach(gridItem => {
gridItem.classList.add('shake-animation');
setTimeout(() => {
gridItem.classList.remove('shake-animation');
}, 1000)
});
}
function applyLetterAnimation(cell, type) {
switch(type) {
case LETTER_TYPE.INCORRECT_LETTER: {
cell.classList.add('incorrect-letter-animation');
break;
}
case LETTER_TYPE.INCORRECT_POSITION: {
cell.classList.add('incorrect-position-animation');
break;
}
case LETTER_TYPE.CORRECT_POSITION: {
cell.classList.add('correct-position-animation');
break;
}
default: {
throw new Error('Invalid letter animation type');
}
}
}
async function onEnter() {
const guessedWord = guessedWords[currRow].join('');
const gridItems = selectCurrentRowCells();
if (guessedWord.length < 5) {
applyShakeAnimation(gridItems);
updateInfo('Not enough letters');
return;
}
const isValid = await isValidWord(guessedWord);
if (!isValid) {
applyShakeAnimation(gridItems);
updateInfo('Not in dictionary');
return;
}
const correct = guess(guessedWord);
if (correct) {
processCurrentRow(guessedWord)
return;
}
if (currRow === DIMENSION) {
updateInfo(`Sorry you ran out of all attempts. The word is ${solution.join('')}.`, 5000);
return;
}
processCurrentRow(guessedWord)
currRow++;
currCol = 0;
}
function onDelete() {
if (currCol > 0) {
currCol--;
const gridEl = document.querySelector(`div[id='${currRow}_${currCol}']`);
gridEl.innerText = '';
guessedWords[currRow][currCol] = '';
}
}
function processCurrentRow(guessedWord) {
const gridItems = selectCurrentRowCells();
const solutionMap = mapSolution();
for (let i = 0; i < DIMENSION; ++i) {
const gridItem = gridItems[i]
if (gridItem) {
const guessedLetter = guessedWord[i];
const keypadCell = document.querySelector(`div[id='${guessedLetter}']`);
// 1. Correct position
if (guessedLetter === solution[i]) {
applyLetterAnimation(gridItem, LETTER_TYPE.CORRECT_POSITION);
applyLetterAnimation(keypadCell, LETTER_TYPE.CORRECT_POSITION);
solutionMap.set(guessedLetter, solutionMap.get(guessedLetter) - 1);
// 2. Incorrect position
} else if (solution.includes(guessedLetter)) {
const letterCount = solutionMap.get(guessedLetter);
if (letterCount > 0) {
applyLetterAnimation(gridItem, LETTER_TYPE.INCORRECT_POSITION);
applyLetterAnimation(keypadCell, LETTER_TYPE.INCORRECT_POSITION);
solutionMap.set(guessedLetter, letterCount - 1);
} else {
applyLetterAnimation(gridItem, LETTER_TYPE.INCORRECT_LETTER);
applyLetterAnimation(keypadCell, LETTER_TYPE.INCORRECT_LETTER);
}
} else {
applyLetterAnimation(gridItem, LETTER_TYPE.INCORRECT_LETTER);
applyLetterAnimation(keypadCell, LETTER_TYPE.INCORRECT_LETTER);
usedLetters.add(guessedLetter);
}
}
}
}
function setupCells() {
const grid = document.querySelector('.hello_worldle__grid');
const fragment = document.createDocumentFragment();
for (let i = 0; i < GUESSES; ++i) {
for (let j = 0; j < DIMENSION; ++j) {
const cell = document.createElement('div');
cell.id = `${i}_${j}`;
cell.classList.add('hello_worldle__grid__item');
cell.innerText = '';
fragment.appendChild(cell)
}
}
grid.appendChild(fragment);
}
function onKeyPress(ev) {
const val = ev.target.innerText;
if (val === '') {
return;
}
if (val === 'GO') {
onEnter();
return;
}
if (val === 'DEL') {
onDelete();
return;
}
if (guessedWords[currRow].join('').length >= DIMENSION) {
return;
}
const cell = document.querySelector(`div[id='${currRow}_${currCol}']`);
cell.innerText = val;
guessedWords[currRow][currCol] = val;
currCol++;
}
function setupKeypad() {
const keypadGrid = document.querySelector('.hello_worldle__keypad');
const fragment = document.createDocumentFragment();
for (let i = 0; i < 3; ++i) {
let colStart = 0;
for (let j = 0; j < 10; ++j) {
const key = document.createElement('div');
const keyName = KEYPAD_MAPPING[i][j];
key.id = keyName;
if (keyName !== '') {
key.classList.add('hello_worldle__keypad__key')
}
key.innerText = keyName;
key.style.gridRowStart = i + 1;
key.style.gridRowEnd = i + 2;
if (i === 2 && (j === 0 || j === 8)) {
key.style.gridColumnStart = colStart + 1;
key.style.gridColumnEnd = colStart + 4;
colStart += 3;
} else if (i === 0 || i === 2) {
key.style.gridColumnStart = colStart + 1;
key.style.gridColumnEnd = colStart + 3;
colStart += 2;
} else if (i === 1) {
key.style.gridColumnStart = colStart + 2;
key.style.gridColumnEnd = colStart + 4;
colStart += 2;
}
key.addEventListener('click', onKeyPress);
fragment.appendChild(key);
}
}
keypadGrid.appendChild(fragment);
}
async function readWordFromJson() {
const res = await fetch('words.json');
const json = await res.json();
const date = new Date();
const today = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
return atob(json[today]);
};
window.onload = async (_) => {
const word = await readWordFromJson();
solution = word.split('').map(l => l.toUpperCase());
initGrid()
setupCells()
setupKeypad()
}