-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.js
310 lines (290 loc) · 8.19 KB
/
hangman.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
const wordList = [
"parakeet",
"grateful",
"performer",
"algorithm",
"honeydew",
"twins",
"anchovy",
"mountain",
"spruce",
"waltz",
"trust",
"gardenia",
"blossom",
"silver",
"fudge",
"style",
"shorthair",
"garden",
"spanish",
"course",
"harmony",
"butterfly",
"sapphire",
"rifle",
"electricity",
"steel",
"plankton",
"future",
"delaware"
];
const wordCategories = [
"birds",
"happiness",
"circus",
"programming",
"fruit",
"family",
"fish",
"camping",
"tree",
"dance",
"feelings",
"flowers",
"plants",
"colors",
"desserts",
"art",
"cats",
"house",
"languages",
"restaurant",
"music theory",
"insect",
"minerals",
"weapons",
"science",
"metals",
"ocean",
"time",
"US States"
];
const wordHints = [
"also known as 'budgie'",
"feeling of appreciation",
"a person who entertains an audience",
"solves logical or mathematical problems",
"a green melon",
"two children born at the same birth",
"can be eaten on pizza",
"a very large, steep hill",
"second darkest wood in minecraft",
"a dance in triple time performed by a couple",
"faith in someone",
"a genus of flowering plants in the coffee family, Rubiaceae",
"what flowers do when they're ready",
"a metal used in jewelry",
"involves chocolate",
"design or make in a particular form",
"they don't have long hair",
"mainly where veggies are grown",
"one of the top 5 most spoken languages in the world",
"may include multiple dishes or only one",
"chords/three or more notes played at the same time",
"related to moths",
"precious blue gemstone",
"man-portable, long-barrelled firearm designed for accurate shooting",
"form of energy used to power machines and electrical devices",
"it is a major component used in buildings, infrastructure, tools, ships, trains, automobiles, machines, appliances, and weapons",
"also featured in spongebob",
"opposite of past",
"starts with a D"
];
var wordIndex = Math.floor(Math.random() * 29);
var word = wordList[wordIndex];
var wordLength = wordList[wordIndex].length;
var category = wordCategories[wordIndex];
var hint = wordHints[wordIndex];
var lives = 10;
var docCategory = document.getElementById("category");
var docLetterCount = document.getElementById("lettercount");
var docLetterSpace = document.getElementById("letterspace");
var docLives = document.getElementById("lives");
var docHint = document.getElementById("hint");
var docAlphabetLetters = document
.getElementById("alphabet")
.getElementsByTagName("button");
var docModal = document.querySelector(".modal");
var docModalWinLose = document.getElementById("modal-text");
var docModalHint = document.getElementById("modal-hint");
var docCloseButton = document.querySelector(".close-button");
function startGame() {
updateCategory();
updateLetterCount();
updateLives();
updateLetterSpace();
}
function newGame() {
refreshWordInfo();
resetAlphabetLetters();
updateCategory();
updateLetterCount();
updateLives();
deleteLetterSpaceText();
updateLetterSpace();
resetStickman();
}
function checkLetter(letter) {
disableButton(letter);
if (word.includes(letter)) {
updateLetterSpace(letter);
if (docLetterSpace.textContent.includes(word)) {
disableAlphabetLetters();
modalPopup("win");
}
} else {
--lives;
updateLives();
if (lives == 0) {
disableAlphabetLetters();
modalPopup("lose");
}
updateStickman();
}
}
function updateLetterSpace(letter = null) {
if (letter != null) {
replaceLetterSpace(letter);
} else {
for (var i = 0; i < wordLength; ++i) {
var underscore = document.createTextNode("_");
docLetterSpace.appendChild(underscore);
}
}
}
function replaceLetterSpace(letter) {
var newStr = docLetterSpace.textContent;
var indices = findAllIndices(letter);
for (var i = 0; i < indices.length; ++i) {
newStr = replaceAt(newStr, indices[i], letter);
}
docLetterSpace.textContent = newStr;
}
function findAllIndices(letter) {
var indices = [];
for (var i = 0; i < wordLength; ++i) {
if (letter == word[i]) {
indices.push(i);
}
}
return indices;
}
function replaceAt(string, index, replace) {
return string.substring(0, index) + replace + string.substring(index + 1);
}
function disableAlphabetLetters() {
for (var i = 0; i < docAlphabetLetters.length; ++i) {
var alphabetLetter = docAlphabetLetters[i];
alphabetLetter.disabled = true;
}
}
function resetAlphabetLetters() {
for (var i = 0; i < docAlphabetLetters.length; ++i) {
var alphabetLetter = docAlphabetLetters[i];
alphabetLetter.disabled = false;
alphabetLetter.className = "alphabet-style";
}
}
function refreshWordInfo() {
wordIndex = Math.floor(Math.random() * 29);
word = wordList[wordIndex];
wordLength = wordList[wordIndex].length;
category = wordCategories[wordIndex];
hint = wordHints[wordIndex];
docHint.disabled = false;
docHint.className = "hint-style";
lives = 10;
}
function disableButton(letter) {
document.getElementById(letter).disabled = true;
if (word.includes(letter)) {
document.getElementById(letter).className = "disabledRight";
} else {
document.getElementById(letter).className = "disabledWrong";
}
}
function updateStickman() {
switch(lives) {
case 9:
document.getElementById("base").classList.toggle("hidden");
break;
case 8:
document.getElementById("stem").classList.toggle("hidden");
break;
case 7:
document.getElementById("top1").classList.toggle("hidden");
break;
case 6:
document.getElementById("top2").classList.toggle("hidden");
break;
case 5:
document.getElementById("head").classList.toggle("hidden");
break;
case 4:
document.getElementById("body").classList.toggle("hidden");
break;
case 3:
document.getElementById("right-arm").classList.toggle("hidden");
break;
case 2:
document.getElementById("left-arm").classList.toggle("hidden");
break;
case 1:
document.getElementById("right-leg").classList.toggle("hidden");
break;
case 0:
document.getElementById("left-leg").classList.toggle("hidden");
}
}
function resetStickman() {
var stickmanpieces = document.getElementById("hangman-square").children;
for (var i=0; i < stickmanpieces.length; ++i) {
if (!hasClass(stickmanpieces[i], "hidden")) {
stickmanpieces[i].classList.toggle("hidden");
}
}
}
function hasClass(target, className) {
return new RegExp('(\\s|^)' + className + '(\\s|$)').test(target.className);
}
function deleteLetterSpaceText() {
docLetterSpace.innerHTML = "";
}
function showHint() {
docHint.disabled = true;
docHint.className = "disabledHint";
modalPopup("hint");
}
function updateCategory() {
docCategory.textContent = "category: " + category;
}
function updateLetterCount() {
docLetterCount.textContent = "number of letters: " + wordLength;
}
function updateLives() {
docLives.textContent = "lives remaining: " + lives;
}
function toggleModal() {
docModal.classList.toggle("show-modal");
}
function windowOnClick(event) {
if (event.target === docModal) {
toggleModal();
}
}
function modalPopup(msg) {
docModalHint.textContent = "";
docModalWinLose.textContent = "";
if (msg == "win") {
docModalWinLose.textContent = "Congratulations, you won :D";
} else if (msg == "lose") {
docModalWinLose.textContent = "Sorry, you're out of tries :(";
} else if (msg == "hint") {
docModalHint.textContent = hint;
}
toggleModal();
}
window.addEventListener("click", windowOnClick);
docCloseButton.addEventListener("click", toggleModal);
startGame();