-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.html
131 lines (116 loc) · 4.2 KB
/
game.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Guess the Word</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
background-color: #f0f0f0;
position: relative;
}
input {
padding: 10px;
margin: 10px;
width: 50px;
text-align: center;
font-size: 1em;
}
button {
padding: 10px 20px;
cursor: pointer;
font-size: 1em;
}
#message {
margin-top: 20px;
font-size: 1.2em;
color: green;
}
#wordDisplay {
font-size: 24px;
margin: 20px 0;
}
footer {
position: fixed;
bottom: 10px;
right: 10px;
font-size: 0.9em;
color: #555;
}
</style>
</head>
<body>
<h1>Guess the Word Game</h1>
<div id="wordDisplay">_ _ _ _ _</div>
<input type="text" id="letterInput" maxlength="1" placeholder="A-Z">
<button id="guessButton">Guess</button>
<div id="message"></div>
<footer>
© 2024 Jaxon Lowery
</footer>
<script>
const words = ["kind", "jungle", "apple", "good", "open", "door", "mean", "jingle", "bob", "jibby"];
let selectedWord = words[Math.floor(Math.random() * words.length)];
let displayedWord = "_".repeat(selectedWord.length).split("");
let attempts = 0;
const wordDisplay = document.getElementById('wordDisplay');
const messageDiv = document.getElementById('message');
const guessButton = document.getElementById('guessButton');
const letterInput = document.getElementById('letterInput');
wordDisplay.textContent = displayedWord.join(" ");
guessButton.addEventListener('click', function() {
const guessedLetter = letterInput.value.toLowerCase();
letterInput.value = ''; // Clear input
if (!/^[a-z]$/.test(guessedLetter)) {
messageDiv.textContent = "Please enter a valid single letter (A-Z).";
messageDiv.style.color = "red";
return;
}
attempts++;
let correctGuess = false;
// Check if guessed letter is in the word
for (let i = 0; i < selectedWord.length; i++) {
if (selectedWord[i] === guessedLetter) {
displayedWord[i] = guessedLetter;
correctGuess = true;
}
}
wordDisplay.textContent = displayedWord.join(" ");
if (correctGuess) {
messageDiv.textContent = `Good job! You've guessed a letter. Attempts: ${attempts}`;
messageDiv.style.color = "green";
} else {
messageDiv.textContent = `Oops! "${guessedLetter}" is not in the word. Attempts: ${attempts}`;
messageDiv.style.color = "red";
}
// Check if the word is completely guessed
if (!displayedWord.includes("_")) {
messageDiv.textContent = `🎉 Congratulations! You've guessed the word "${selectedWord}" in ${attempts} attempts! 🎉`;
messageDiv.style.color = "blue";
// Reset game after a short delay
setTimeout(resetGame, 3000);
}
});
function resetGame() {
selectedWord = words[Math.floor(Math.random() * words.length)];
displayedWord = "_".repeat(selectedWord.length).split("");
attempts = 0;
wordDisplay.textContent = displayedWord.join(" ");
messageDiv.textContent = '';
}
// Allow pressing "Enter" to submit the guess
letterInput.addEventListener('keypress', function(event) {
if (event.key === 'Enter') {
guessButton.click();
}
});
</script>
</body>
</html>