-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
182 lines (169 loc) · 4.64 KB
/
app.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
const $ = (s, e = document.body) => e.querySelector(s);
const $$ = (s, e = document.body) => [...e.querySelectorAll(s)];
const wait = (ms) => new Promise((done) => setTimeout(done, ms));
const dom = (tag, attrs, ...children) => {
const el = document.createElement(tag);
if (attrs instanceof HTMLElement) {
children.unshift(attrs);
} else {
Object.entries(attrs).forEach(([key, value]) => {
if (key === "class" && value instanceof Array) {
value = value.join(" ");
}
el.setAttribute(key, value);
});
}
el.append(...children.flat());
return el;
};
const KEYS = ["QWERTYUIOP", "ASDFGHJKL", "+ZXCVBNM-"];
const PRETTY_KEYS = {
"+": "Enter",
"-": "Del",
};
const ROUNDS = 6;
const LENGTH = 5;
const dictionaryRequest = fetch("/dictionary.txt").then((r) => r.text());
const swearRequest = fetch("/words.txt").then((ra) => ra.text());
const board = $(".board");
const keyboard = $(".keyboard");
window.onload = () => init().catch((e) => console.error(e));
async function init() {
const board = generateBoard();
const kb = generateKeyboard();
const words = (await dictionaryRequest).split("\n");
const cusses = (await swearRequest).split("\n");
const word = cusses[(Math.random() * cusses.length) | 0];
await startGame({ word, kb, board, words });
}
async function animate(el, name, ms) {
el.style.animation = `${ms}ms ${name}`;
await wait(ms * 1.2);
el.style.animation = "none";
}
async function startGame({ word, kb, board, words }) {
let guesses = [];
const solution = word.split("");
let round = 0;
for (round = 0; round < ROUNDS; round++) {
const guess = await collectGuess({ kb, board, round, words });
const hints = guess.map((letter, i) => {
let pos = solution.indexOf(letter);
if (solution[i] === letter) {
return "correct";
} else if (pos > -1) {
return "close";
}
return "wrong";
});
board.revealHint(round, hints);
kb.revealHint(guess, hints);
if (guess.join("") === word) {
$(".feedback").innerText = `Correct!`;
return;
}
}
$(".feedback").innerText = `GAME OVER\nCorrect Answer was: ${word}`;
}
function collectGuess({ kb, board, round, words }) {
return new Promise((submit) => {
let letters = [];
async function keyHandler(key) {
if (key === "+") {
if (letters.length === 5) {
const guessIsValid = words.includes(letters.join(""));
if (!guessIsValid) {
$(".feedback").innerText = "Invalid Word";
await animate($$(".round")[round], "shake", 800);
} else {
$(".feedback").innerText = "";
kb.off(keyHandler);
submit(letters);
}
}
} else if (key === "-") {
if (letters.length > 0) {
letters.pop();
}
board.updateGuess(round, letters);
} else {
if (letters.length < 5) {
letters.push(key);
}
board.updateGuess(round, letters);
}
}
kb.on(keyHandler);
});
}
function generateBoard() {
const rows = [];
for (let i = 0; i < ROUNDS; i++) {
const row = dom("div", {
class: "round",
"data-round": i,
});
for (let j = 0; j < LENGTH; j++) {
row.append(
dom("div", {
class: "letter",
"data-pos": j,
})
);
}
board.append(row);
}
return {
updateGuess: (round, letters) => {
const blanks = $$(".letter", $$(".round")[round]);
blanks.forEach((b, i) => (b.innerText = letters[i] || ""));
},
revealHint: (round, hints) => {
const blanks = $$(".letter", $$(".round")[round]);
hints.forEach((hint, i) => {
if (hint) {
blanks[i].classList.add("letter--hint-" + hint);
}
});
},
};
}
function generateKeyboard() {
keyboard.append(
...KEYS.map((row) =>
dom(
"div",
{
class: "keyboard__row",
},
row.split("").map((key) =>
dom(
"button",
{
class: `key${PRETTY_KEYS[key] ? " key--pretty" : ""}`,
"data-key": key,
},
PRETTY_KEYS[key] || key
)
)
)
)
);
const keyListeners = new Set();
keyboard.addEventListener("click", (e) => {
e.preventDefault();
const key = e.target.getAttribute("data-key");
if (key) {
keyListeners.forEach((l) => l(key));
}
});
return {
on: (l) => keyListeners.add(l),
off: (l) => keyListeners.delete(l),
revealHint: (guess, hints) => {
hints.forEach((hint, i) => {
$(`[data-key="${guess[i]}"]`).classList.add("key--hint-" + hint);
});
},
};
}