-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
75 lines (65 loc) · 2.73 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
// JavaScript logic for Word Guessing Game
const words = ['apple', 'banana', 'orange', 'grape', 'watermelon','mango','kiwi','guava','fig','dragonfruit','strawberry']; // List of words to be guessed
let currentWord; // Variable to store the current word being guessed
let remainingAttempts; // Variable to store the remaining attempts
function initializeGame() {
remainingAttempts = 5; // Set initial attempts
currentWord = words[Math.floor(Math.random() * words.length)]; // Pick a random word from the list
displayWord(); // Display dashes representing the word to be guessed
displayHint(); // Display a hint for the word
displayLetterOptions(); // Display letter options for the player
}
function displayWord() {
const wordDisplay = document.getElementById('word-display');
wordDisplay.innerHTML = currentWord.replace(/./g, '_ '); // Display dashes for each letter in the word
}
function displayHint() {
const hintSpan = document.getElementById('hint-text');
hintSpan.innerText = "It's a fruit."; // Example hint
}
function displayLetterOptions() {
const letterOptionsDiv = document.getElementById('letter-options');
const letters = 'abcdefghijklmnopqrstuvwxyz';
letters.split('').forEach(letter => {
const letterButton = document.createElement('button');
letterButton.innerText = letter;
letterButton.classList.add('letter');
letterButton.addEventListener('click', () => {
guessLetter(letter);
letterButton.disabled = true; // Disable the button after it's clicked
});
letterOptionsDiv.appendChild(letterButton);
});
}
function guessLetter(letter) {
if (currentWord.includes(letter)) {
updateWordDisplay(letter);
} else {
updateRemainingAttempts();
}
}
function updateWordDisplay(letter) {
const wordDisplay = document.getElementById('word-display');
const currentWordArray = wordDisplay.innerText.split(' ');
for (let i = 0; i < currentWord.length; i++) {
if (currentWord[i] === letter) {
currentWordArray[i] = letter;
}
}
wordDisplay.innerText = currentWordArray.join(' ');
if (!currentWordArray.includes('_')) {
// Player has guessed the word correctly
alert('Congratulations! You guessed the word.');
initializeGame();
}
}
function updateRemainingAttempts() {
remainingAttempts--;
if (remainingAttempts === 0) {
// Player has run out of attempts
alert('Game over. You ran out of attempts.');
initializeGame();
}
}
document.getElementById('reset-button').addEventListener('click', initializeGame);
initializeGame(); // Start the game