forked from Kolatzek/WPdoodlez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangapp.js
409 lines (359 loc) · 12.1 KB
/
hangapp.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
;(
function(window, document) {
'use strict';
const availableChars = [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
],
maxGuesses = 10,
answer = getAnswer(),
answerChars = getAnswerChars(),
placeholderChars = answer.toUpperCase().split(''),
availableCharsSelector = document.getElementById(
'hangman-available-characters-list'),
answerPlaceholdersSelector = document.getElementById(
'hangman-answer-placeholders'),
noticesSelector = document.getElementById('hangman-notices'),
canvasSelector = document.getElementById('hangman-canvas'),
canvasContext = canvasSelector.getContext('2d'),
stickmanCoordinates = [
{ // Base.
'lineStartX': 20,
'lineStartY': 300,
'lineEndX': 280,
'lineEndY': 300,
},
{ // Post.
'lineStartX': 40,
'lineStartY': 20,
'lineEndX': 40,
'lineEndY': 300,
},
{ // Boom.
'lineStartX': 40,
'lineStartY': 20,
'lineEndX': 150,
'lineEndY': 20,
},
{ // Rope.
'lineStartX': 150,
'lineStartY': 20,
'lineEndX': 150,
'lineEndY': 60,
},
{ // Head.
'arcCenterX': 150,
'arcCenterY': 80,
'radius': 20,
},
{ // Torso.
'lineStartX': 150,
'lineStartY': 100,
'lineEndX': 150,
'lineEndY': 220,
},
{ // Left leg.
'lineStartX': 150,
'lineStartY': 220,
'lineEndX': 75,
'lineEndY': 280,
},
{ // Right leg.
'lineStartX': 150,
'lineStartY': 220,
'lineEndX': 225,
'lineEndY': 280,
},
{ // Left arm.
'lineStartX': 150,
'lineStartY': 150,
'lineEndX': 80,
'lineEndY': 125,
},
{ // Right arm.
'lineStartX': 150,
'lineStartY': 150,
'lineEndX': 220,
'lineEndY': 125,
},
];
var guessedChars = [],
misses = 0,
hits = 0,
characterPlaceholderElements = [];
/***************
* Helpers
***************/
/**
* Returns the base64 decoded answer, or an empty string on failure.
* @returns {string}
*/
function getAnswer() {
if (typeof hangman_app_script_data.answer === 'undefined') {
return '';
}
else {
try {
return window.atob(hangman_app_script_data.answer);
}
catch (e) {
console.log('ERROR: Hangman answer not formatted correctly!');
return '';
}
}
}
/**
* Returns a sorted array of unique characters that exits within the
* answer.
* @returns {Array}
*/
function getAnswerChars() {
var answerArray = answer.toUpperCase().split('');
return answerArray.sort().filter(function(char, index, inputArray) {
return isValidChar(char) && char !== inputArray[index - 1];
});
}
/**
* Checks if a character exists in the available characters.
* @param {string} char Character to check.
* @returns {boolean}
*/
function isValidChar(char) {
return availableChars.includes(char.toUpperCase());
}
/**
* Checks if the player lost.
* @returns {boolean}
*/
function playerWon() {
var correctGuesses = guessedChars.sort().filter(function(char) {
return answerChars.includes(char);
});
return correctGuesses.length === answerChars.length &&
correctGuesses.every(function(element, index) {
return element === answerChars[index];
});
}
/**
* Checks if the player lost.
* @returns {boolean}
*/
function playerLost() {
return maxGuesses <= misses;
}
/***************
* Game Setup
***************/
/**
* Renders the available characters.
*/
function renderAvailableChars() {
var html = '';
for (var $i = 0; $i < availableChars.length; $i++) {
html += '<li class="hangman-available-character" data-key-code="' +
availableChars[$i].charCodeAt(0) + '" data-char="' +
availableChars[$i] + '">' + availableChars[$i] +
'</li>';
}
availableCharsSelector.innerHTML += html;
}
/**
* Render the HTML to show empty placeholders for each of the characters
* that comprise the answer. Really this could be done with PHP, but hey,
* why not do it with JS. We will interact with these placeholders quite
* a bit, so let's give each one an id as well as a class.
*/
function renderEmptyPlaceholders() {
if (!answer) {
answerPlaceholdersSelector.innerHTML += '<strong>Fehler: Antwort konnte nicht übergeben werden</strong>';
return;
}
var html = '<ul id="hangman-placeholders"><li class="word-placeholder"><ul>';
for (var $i = 0; $i < placeholderChars.length; $i++) {
if (' ' === placeholderChars[$i]) {
html += '<li class="character-placeholder space"></ul></li><li class="word-placeholder"><ul>';
}
else if (!isValidChar(placeholderChars[$i])) {
html += '<li class="character-placeholder given">' +
placeholderChars[$i] + '</li>';
}
else {
html += '<li class="character-placeholder guess">_</li>';
}
}
html += '</ul></li></ul>';
answerPlaceholdersSelector.innerHTML += html;
}
/**
* Retrieves the character placeholder elements.
*/
function getCharacterPlaceholderElements() {
characterPlaceholderElements = answerPlaceholdersSelector.querySelectorAll(
'.character-placeholder');
}
/**
* Retrieves the current guess and passes it to the validateCurrentGuess()
* function.
*/
function addGuessListener() {
document.onkeydown = function(event) {
validateCurrentGuess(event.key.toUpperCase());
};
availableCharsSelector.addEventListener('click', function(event) {
if (event.target.matches('li')) {
validateCurrentGuess(event.target.textContent);
}
});
}
/**
* Resets the game when the appropriate button is clicked.
*/
function addResetListener() {
noticesSelector.addEventListener('click', function(event) {
if (event.target.matches('button#hangman-reset-game')) {
resetGame();
}
});
}
/**
* Sets up the HTML canvas.
*/
function setupCanvas() {
canvasSelector.width = 300;
canvasSelector.height = 320;
}
/***********************
* Player interaction
***********************/
/**
* Checks if the current guess matches a character in the answer and takes
* the appropriate next step for the game.
* @param {string|number} currentGuess Current guess submitted by the
* player.
*/
function validateCurrentGuess(currentGuess) {
if (playerWon() || playerLost()) {
return;
}
if (!isValidChar(currentGuess) || guessedChars.includes(currentGuess)) {
console.log('Falsche Antwort');
return;
}
guessedChars.push(currentGuess);
// Is the current guess correct?
hits++;
if (answerChars.includes(currentGuess)) {
noticesSelector.innerHTML = "<span class='newlabel white'>Versuche: " + hits + "</span> <span class='newlabel yellow'>Falsch: " + misses + " von 10</span>";
printCorrectGuess(currentGuess);
if (playerWon()) {
doGameEnd('won');
}
}
else {
misses++;
noticesSelector.innerHTML = "<span class='newlabel white'>Versuche: " + hits + "</span> <span class='newlabel yellow'>Falsch: " + misses + " von 10</span>";
drawHangman(misses - 1);
if (playerLost()) {
doGameEnd('lost');
}
}
// Disable the guessed character.
disableCharacter(currentGuess);
}
/**
* Fills in the placeholders with correct guesses.
* @param {string} guess Player's guess.
*/
function printCorrectGuess(guess) {
for (var $i = 0; $i < placeholderChars.length; $i++) {
if (placeholderChars[$i] === guess) {
characterPlaceholderElements[$i].innerHTML = guess;
}
}
}
/**
* Disables characters from begin selected.
* @param {string|number} character Character to be disabled.
*/
function disableCharacter(character) {
var charSelector = availableCharsSelector.querySelector(
'[data-char="' + character + '"]');
if (!charSelector.classList.contains('disabled')) {
charSelector.className += ' disabled';
}
}
/**
* Draws the hangman figure.
* @param {number} misses Stickman coordinates index
*/
function drawHangman(misses) {
canvasContext.beginPath();
var path = stickmanCoordinates[misses];
// Check if we should draw a circle or line.
if (path.hasOwnProperty('radius')) {
canvasContext.arc(path.arcCenterX, path.arcCenterY, path.radius, 0,
2 * Math.PI);
}
else {
canvasContext.moveTo(
path.lineStartX,
path.lineStartY
);
canvasContext.lineTo(
path.lineEndX,
path.lineEndY
);
}
canvasContext.stroke();
}
/*****************
* Game Teardown
*****************/
/**
* Ends the game.
* @param {String} outcome Whether the player 'won' or not.
*/
function doGameEnd(outcome) {
var html;
if ('won' === outcome) {
html = '<span class="newlabel white">Sie haben den Betriff ' + answer + ' nach ' + hits + ' Versuchen und ' + misses + ' Fehlern erraten. Herzlichen Glückwunsch.</span>';
}
else {
html = '<span class="newlabel yellow">Sie haben den Begriff ' + answer + ' nach ' + hits + ' Versuchen und ' + misses + ' Fehlern leider nicht erraten!</span>';
// <button id="hangman-reset-game">Neu versuchen</button>
}
noticesSelector.innerHTML = html;
}
/**
* Resets the game so player can start over.
*/
function resetGame() {
var disabledChars = availableCharsSelector.querySelectorAll(
'li.hangman-available-character.disabled'),
blankPlaceholders = answerPlaceholdersSelector.querySelectorAll(
'li.guess')
;
disabledChars.forEach(function(element) {
element.className = 'hangman-available-character';
});
blankPlaceholders.forEach(function(element) {
element.innerHTML = '_';
});
noticesSelector.innerHTML = '';
canvasContext.clearRect(0, 0, canvasSelector.width,
canvasSelector.height);
guessedChars = [];
misses = 0;
}
function init() {
renderAvailableChars();
renderEmptyPlaceholders();
getCharacterPlaceholderElements();
addGuessListener();
addResetListener();
setupCanvas();
}
init();
}
)(window, document);