-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
179 lines (137 loc) · 4.75 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
// Variable to store the player's score
let score = 0;
// Function to update and display the player's score
const updateScore = () => {
const scoreElement = document.querySelector(".score");
scoreElement.innerText = `Score: ${score}`;
};
const keyboard = document.querySelector(".keyboard");
const h4 = document.querySelector("h4");
const wordDisplay = document.querySelector(".word-display");
const chance = document.querySelector(".chance");
const img = document.querySelector(".img");
const gameover = document.querySelector(".GameOver");
const gameoverimg = document.querySelector(".gameoverImg");
const answer = document.querySelector(".answer");
const h3 = document.querySelector("h3");
const h6 = document.querySelector("h6");
let count = 0;
let gameStarted = false; // Flag to check if the game has started
const randomIndex = Math.floor(Math.random() * wordList.length);
const { word, hint } = wordList[randomIndex];
for (var i = 97; i <= 122; i++) {
let button = document.createElement("button");
button.classList.add("btn");
button.innerHTML = String.fromCharCode(i);
keyboard.appendChild(button);
}
const gameOver = (bool) => {
const gameOverAudio = document.getElementById('gameOverAudio');
const victoryAudio = document.getElementById('victoryAudio');
if (bool) {
gameover.classList.add("show");
document.querySelector(".game").style.opacity = 0.8;
answer.innerText = word;
// Deduct points on losing
score = 0;
updateScore();
// Play audio when the player loses the game
gameOverAudio.play();
} else {
gameover.classList.add("show");
document.querySelector(".game").style.opacity = 0.8;
gameoverimg.src = "images/victory2.gif";
h3.innerText="Congrats!"
// Add points on winning
score += 10;
updateScore();
h6.innerText="You Guessed The Correct Answer!"
// Play "Victory" audio when the player wins the game
victoryAudio.play();
}
};
const gameOverwin = () => {
const letterElem = document.querySelectorAll(".letter");
var matchLetter = "";
letterElem.forEach((v) => {
matchLetter += v.innerText.toLowerCase();
});
if (matchLetter === word) {
gameOver(false);
}
};
const matchWord = (val) => {
const isSpace = word.includes(' ') && val === ' ';
if (isSpace) {
return; // Ignore spaces
}
const matches = [];
//console.log(word);
word.split("").forEach((el, index) => {
if (el === val.toLowerCase()) {
matches.push(index);
}
});
if (matches.length === 0) {
count++;
//matches.disabled= true;
chance.innerText = `${count}/6`;
} else {
matches.forEach((v) => {
const letterElem = document.querySelectorAll(".letter");
letterElem[v].innerText = val;
letterElem[v].classList.add("guess");
});
}
};
const loadQuestion = () => {
h4.innerText = `Hint: ${hint}`;
for (let i = 0; i < word.length; i++) {
let liTag = document.createElement("li");
liTag.classList.add("letter");
wordDisplay.appendChild(liTag);
}
const buttonTag = document.querySelectorAll(".btn");
buttonTag.forEach((v) => {
v.addEventListener("click", (e) => {
const clickedButton = e.target;
matchWord(e.target.innerText);
// Add a 'disabled' class to the clicked button
clickedButton.classList.add("disabled");
// Disable the button to prevent further clicks
clickedButton.disabled = true;
// const letterElem = document.querySelectorAll(".letter");
if (count >= 1 && count < 2) {
img.src = "images/hangman-1.svg";
} else if (count >= 2 && count < 3) {
img.src = "images/hangman-2.svg";
} else if (count >= 3 && count < 4) {
img.src = "images/hangman-4.svg";
} else if (count >= 4 && count < 5) {
img.src = "images/hangman-5.svg";
} else if (count >= 6 && count < 7) {
img.src = "images/hangman-6.svg";
setTimeout(()=>{
gameOver(true);
},200)
}
gameOverwin();
});
}
);
};
document.getElementById('startButton').addEventListener('click', startGame);
function startGame() {
if (!gameStarted) {
// Clear any existing content in the word display
wordDisplay.innerHTML = '';
let button = document.createElement("button");
button.classList.add("btn")
button.innerHTML = String.fromCharCode(i);
keyboard.appendChild(button);
loadQuestion();
gameStarted = true; // Set the flag to true to indicate that the game has started
// Hide the start modal
document.getElementById('startModal').style.display = 'none';
}
}