-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle.js
101 lines (86 loc) · 2.95 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
'use strict'
let attempt = 0;
let j = 0;
let word = '';
let input = '';
function type(key) {
if (j > 4) return;
let tgt = document.getElementById(`place-${attempt}-${j++}`);
tgt.innerText = key.dataset.letter;
input += key.dataset.letter;
}
function backspace() {
let tgt = document.getElementById(`place-${attempt}-${--j}`);
tgt.innerText = '';
input = input.slice(0, -1);
}
function enter() {
if (j < 5 || !GUESSES.includes(input)) {
let places = Array.from([0,1,2,3,4].map(index => document.getElementById(`place-${attempt}-${index}`)));
places.forEach(place => {
place.classList.add('shaking');
let shake = place.getAnimations()[0];
shake.onfinish = () => place.classList.remove('shaking');
});
return;
}
let results = rate(input, word);
for (let i = 0; i < j; i++) {
document.getElementById(`place-${attempt}-${i}`).classList.add(results[i]);
}
if (results.every(result => result === 'correct'))
return win();
else
for (let i = 0; i < j; i++) document.getElementById(`key-${input[i]}`).classList.add(results[i]);
attempt++;
j = 0;
input = '';
if (attempt > 5)
lose();
}
function clearAll() {
while (j > 0)
backspace();
}
function rate(input, word) {
const countOf = (char, str) => [...str].reduce((count, letter) => count + (char === letter ? 1 : 0), 0);
input = input.toUpperCase();
word = word.toUpperCase();
return [...input].map((character, index) =>
word[index] === character ?
'correct' :
{
true: 'misplaced',
false: 'wrong'
}[ countOf(character, input.slice(0, index + 1)) <= countOf(character, word) - [...input.slice(index + 1)].filter((char, idx) => char === character && input.slice(index + 1)[idx] === word.slice(index + 1)[idx]).length ]
)
}
function init() {
document.getElementById('resultscreen').classList.add('hidden');
document.getElementById('board').classList.remove('hidden');
document.getElementById(`keyboard`).classList.remove('hidden');
Array.from(document.getElementsByClassName('place')).forEach(element => {
element.innerText = '';
['correct', 'misplaced', 'wrong'].forEach(rating => element.classList.remove(rating));
});
Array.from(document.getElementsByClassName('key')).forEach(element => {
['correct', 'misplaced', 'wrong'].forEach(rating => element.classList.remove(rating));
});
attempt = 0;
j = 0;
input = '';
word = WORDS[Math.floor(Math.random() * WORDS.length)];
}
function win() {
document.getElementById('win').classList.remove('hidden');
document.getElementById('lose').classList.add('hidden');
document.getElementById('resultscreen').classList.remove('hidden');
document.getElementById(`keyboard`).classList.add('hidden');
}
function lose() {
document.getElementById('win').classList.add('hidden');
document.getElementById('lose').classList.remove('hidden');
document.getElementById('solution').innerText = word.toUpperCase();
document.getElementById('resultscreen').classList.remove('hidden');
document.getElementById(`keyboard`).classList.add('hidden');
}