-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
181 lines (159 loc) · 4.78 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
170
171
172
173
174
175
176
177
178
179
180
181
// array of words
const words = [
{
word: "apple",
hint: "Common fruit, often red or green."
},
{
word: "table",
hint: "Furniture with flat top and legs."
},
{
word: "music",
hint: "Art of arranging sounds in time."
},
{
word: "happy",
hint: "State of well-being and joy."
},
{
word: "ocean",
hint: "Vast saltwater body on Earth."
},
{
word: "light",
hint: "Agent that stimulates sight."
},
{
word: "house",
hint: "Building for human habitation."
},
{
word: "money",
hint: "Medium of exchange, coins, banknotes."
},
{
word: "world",
hint: "Planet Earth and its inhabitants."
},
{
word: "chair",
hint: "Furniture for sitting, with back."
},
{
word: "night",
hint: "Period of darkness, sunset to sunrise."
},
{
word: "water",
hint: "Colorless, odorless liquid, essential for life."
},
{
word: "green",
hint: "Color of foliage and grass."
},
{
word: "smile",
hint: "Facial expression of happiness."
},
{
word: "dance",
hint: "Rhythmic body movements to music."
},
{
word: "tiger",
hint: "Large cat, orange coat, black stripes."
},
{
word: "earth",
hint: "Third planet from the sun, supports life."
},
{
word: "lemon",
hint: "Bright yellow citrus fruit, sour taste."
},
{
word: "piano",
hint: "Musical instrument with keyboard."
},
{
word: "beach",
hint: "Sandy shore by ocean, for recreation."
}
];
let currentWord, wrongLetterCount = 0, correctLetterCount = 0;
const maxWrong = 7;
const fails = [".stang4", ".head", ".body", ".arm", ".arm2", ".leg", ".leg2"];
// all letters for gaming keyboard
const letters = "abcdefghijklmnopqrstuvwxyz";
window.addEventListener("DOMContentLoaded", () => {
gE(".game-end").style.display = "flex";
gE(".game-end h1").innerHTML = "Hangman Game!";
gE(".game-end button").innerHTML = "Play Game";
gE(".game-end button").addEventListener("click", newGame);
});
// get HTML elements
const gE = (sel) => document.querySelector(sel);
// create keyboard
const createKeyboard = () => {
// get html keyboard element
const keyboard = gE(".keyboard");
keyboard.innerHTML = "";
for (let i = 0; i < letters.length; i++) {
let button = document.createElement("button");
button.innerText = letters[i];
keyboard.appendChild(button);
button.addEventListener("click", e => init(e.target, letters[i]))
}
}
// get random word from words list
const getWord = () => {
const { word, hint } = words[Math.floor(Math.random() * words.length)];
currentWord = word;
gE(".hint-text b").innerText = hint;
gE(".word-display").innerHTML = word.split("").map(() => `<li class="letter"></li>`).join("");
}
const init = (button, clickedLetter) => {
button.disabled = true;
if (currentWord.includes(clickedLetter)) {
[...currentWord].forEach((letter, index) => {
if (letter === clickedLetter) {
gE(`.word-display :nth-child(${index + 1})`).innerText = letter;
button.style.background = "green";
correctLetterCount++;
}
})
} else {
gE(fails[wrongLetterCount]).style.display = "block";
button.style.color = "#999";
wrongLetterCount++;
}
gE(".guess b").innerText = `${wrongLetterCount} / ${maxWrong}`;
if (correctLetterCount == currentWord.length) return gameEnd(true);
if (wrongLetterCount == maxWrong) return gameEnd(false);
}
const gameEnd = (value) => {
gE(".game-end").style.display = "flex";
if (value) {
gE(".game-end h1").innerText = "You Win!";
gE(".game-end span").innerHTML = "God Job</br>";
gE(".game-end p").innerHTML = `The word was: <b>${currentWord}</b>`;
gE(".game-end button").innerHTML = "Play Again";
} else {
gE(".game-end h1").innerText = "Game Over!";
gE(".game-end span").innerHTML = "Better luck next time";
gE(".game-end p").innerHTML = `The word was: <b>${currentWord}</b>`;
gE(".game-end button").innerHTML = "Try Again";
}
gE(".game-end button").addEventListener("click", newGame);
}
const newGame = () => {
currentWord = "", wrongLetterCount = 0, correctLetterCount = 0;
gE(".game-end").style.display = "none";
// update wrong letter count
gE(".guess b").innerText = `${wrongLetterCount} / ${maxWrong}`;
// clear draw box
fails.forEach(sel => gE(fails[fails.indexOf(sel)]).style.display = "none");
createKeyboard();
getWord();
}