-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
169 lines (153 loc) · 4.28 KB
/
script.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
const word = document.getElementById('word');
const incorrect = document.getElementById('incorrect');
const incorrectLettersEl = document.querySelector('#incorrect p');
const backdrop = document.getElementById('backdrop');
const finalMsg = document.getElementById('final-msg');
const msgInfo = document.getElementById('msg-info');
const playBtn = document.getElementById('play');
const indication = document.getElementById('indication');
const bodyParts = document.getElementsByClassName('body-part');
// List of words
const wordList = [
'MARKET',
'KNOCK',
'SMITE',
'WINDY',
'COIN',
'THROW',
'SILENCE',
'BLUFF',
'DOWNFALL',
'CLIMB',
'LYING',
'WEAVER',
'SNOB',
'KICKOFF',
'MATCH',
'COAT',
'ESMERALD',
'COHERENT',
'MULTIPLE',
'SQUARE',
];
// Word that is selected to play
let selectedWord = null;
// Stores the count of no.of incorrectly typed letters
let incorrectCount = 0;
// Correct letters typed by the player
const correctLetters = [];
// Incorrect letters typed by the player
const incorrectLetters = [];
// Select a word randomly from wordList and initialize in the DOM
function initializeWord() {
selectedWord = wordList[Math.floor(Math.random() * wordList.length)];
const noOfLetters = selectedWord.length;
for (let i = 0; i < noOfLetters; i++) {
const listItem = document.createElement('li');
listItem.classList.add('letter');
word.append(listItem);
}
}
// Displays an indication sliding from the bottom
function displayIndication() {
indication.classList.add('visible');
setTimeout(() => {
indication.classList.remove('visible');
}, 2400);
}
// Update the figure when incorrect letters typed
function updateFigure() {
try {
bodyParts[incorrectCount].style.display = 'block';
incorrectCount++;
} catch (error) {}
}
// When player wins
function successState() {
setTimeout(() => {
backdrop.classList.add('visible');
finalMsg.classList.add('visible');
msgInfo.textContent = '¡Ganaste. Felicidades!';
}, 400);
}
// When player looses
function failureState() {
setTimeout(() => {
backdrop.classList.add('visible');
finalMsg.classList.add('visible');
msgInfo.textContent = `¡Fin del juego! "${selectedWord}"`;
}, 400);
}
// Check if typed key is part of the selected word and update in the DOM if required
function check(ev) {
const letterElements = document.querySelectorAll('.word .letter');
const character = ev.key;
// Handle keyboard events
if (
!backdrop.classList.contains('visible') &&
!indication.classList.contains('visible') &&
ev.keyCode >= 65 &&
ev.keyCode <= 90
) {
if (selectedWord.includes(character)) {
if (correctLetters.includes(character)) {
displayIndication();
} else {
correctLetters.push(character);
const indexes = [];
[...selectedWord].forEach((value, index) => {
if (value === character) {
indexes.push(index);
}
});
indexes.forEach((value) => {
letterElements[value].textContent = character;
});
}
} else {
if (incorrectLetters.includes(character)) {
displayIndication();
} else {
incorrectLetters.push(character);
if (!incorrect.classList.contains('visible')) {
incorrect.classList.add('visible');
}
incorrectLettersEl.textContent = `${incorrectLetters.join(', ')}`;
updateFigure();
}
}
}
// Create a word from all letter items
let formedWord = '';
letterElements.forEach((value) => {
formedWord += value.textContent;
});
// Check if created word is correct
if (formedWord === selectedWord) {
successState();
}
// Check if man was hung
if (incorrectCount >= 6) {
failureState();
}
}
// Reset all variables and start a new game
function startNewGame() {
selectedWord = null;
incorrectCount = 0;
correctLetters.splice(0);
incorrectLetters.splice(0);
word.innerHTML = '';
Array.from(bodyParts).forEach((value) => {
value.style.display = 'none';
});
incorrect.classList.remove('visible');
backdrop.classList.remove('visible');
finalMsg.classList.remove('visible');
initializeWord();
}
// Start the game
initializeWord();
// Event Listeners
window.addEventListener('keyup', check);
playBtn.addEventListener('click', startNewGame);