Spaces:
Running
Running
Update index.html
Browse files- index.html +93 -17
index.html
CHANGED
@@ -1,19 +1,95 @@
|
|
1 |
<!DOCTYPE html>
|
2 |
<html>
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
<
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
<!DOCTYPE html>
|
2 |
<html>
|
3 |
+
<head>
|
4 |
+
<title>Wordle Game</title>
|
5 |
+
</head>
|
6 |
+
<body>
|
7 |
+
<h1>Welcome to Wordle!</h1>
|
8 |
+
<p>Try to guess the hidden word by selecting the correct letters.</p>
|
9 |
+
<p id="word"></p>
|
10 |
+
<p>Guess a letter:</p>
|
11 |
+
<input type="text" id="guess" maxlength="1">
|
12 |
+
<button onclick="guessLetter()">Submit</button>
|
13 |
+
<p id="feedback"></p>
|
14 |
+
<p id="guesses"></p>
|
15 |
+
|
16 |
+
<script>
|
17 |
+
// List of words to choose from
|
18 |
+
var words = ["apple", "banana", "cherry", "date", "elderberry", "fig", "grape"];
|
19 |
+
// Maximum number of guesses allowed
|
20 |
+
var maxGuesses = 6;
|
21 |
+
// Current state of the game
|
22 |
+
var word;
|
23 |
+
var guessedLetters = new Set();
|
24 |
+
var guessesLeft;
|
25 |
+
var wordDisplay = "";
|
26 |
+
|
27 |
+
// Function to start a new game
|
28 |
+
function newGame() {
|
29 |
+
// Select a random word from the list
|
30 |
+
word = words[Math.floor(Math.random() * words.length)];
|
31 |
+
guessedLetters.clear();
|
32 |
+
guessesLeft = maxGuesses;
|
33 |
+
|
34 |
+
// Display the initial state of the game
|
35 |
+
wordDisplay = "";
|
36 |
+
for (var i = 0; i < word.length; i++) {
|
37 |
+
wordDisplay += "_ ";
|
38 |
+
}
|
39 |
+
document.getElementById("word").innerHTML = wordDisplay;
|
40 |
+
document.getElementById("guesses").innerHTML = "Guesses left: " + guessesLeft;
|
41 |
+
}
|
42 |
+
|
43 |
+
// Function to update the display when a letter is guessed
|
44 |
+
function updateDisplay() {
|
45 |
+
wordDisplay = "";
|
46 |
+
for (var i = 0; i < word.length; i++) {
|
47 |
+
if (guessedLetters.has(word[i])) {
|
48 |
+
wordDisplay += word[i] + " ";
|
49 |
+
} else {
|
50 |
+
wordDisplay += "_ ";
|
51 |
+
}
|
52 |
+
}
|
53 |
+
document.getElementById("word").innerHTML = wordDisplay;
|
54 |
+
document.getElementById("guesses").innerHTML = "Guesses left: " + guessesLeft;
|
55 |
+
}
|
56 |
+
|
57 |
+
// Function to check if the game is over
|
58 |
+
function checkGameOver() {
|
59 |
+
if (wordDisplay.indexOf("_") == -1) {
|
60 |
+
alert("Congratulations, you win!");
|
61 |
+
newGame();
|
62 |
+
} else if (guessesLeft == 0) {
|
63 |
+
alert("Sorry, you lose! The word was: " + word);
|
64 |
+
newGame();
|
65 |
+
}
|
66 |
+
}
|
67 |
+
|
68 |
+
// Function to handle a letter guess
|
69 |
+
function guessLetter() {
|
70 |
+
var guess = document.getElementById("guess").value.toLowerCase();
|
71 |
+
if (guess.length == 1 && /[a-z]/.test(guess)) {
|
72 |
+
if (guessedLetters.has(guess)) {
|
73 |
+
document.getElementById("feedback").innerHTML = "You already guessed that letter.";
|
74 |
+
} else if (word.indexOf(guess) == -1) {
|
75 |
+
guessesLeft--;
|
76 |
+
document.getElementById("feedback").innerHTML = "Incorrect guess.";
|
77 |
+
updateDisplay();
|
78 |
+
checkGameOver();
|
79 |
+
} else {
|
80 |
+
guessedLetters.add(guess);
|
81 |
+
document.getElementById("feedback").innerHTML = "Correct guess!";
|
82 |
+
updateDisplay();
|
83 |
+
checkGameOver();
|
84 |
+
}
|
85 |
+
} else {
|
86 |
+
document.getElementById("feedback").innerHTML = "Please enter a valid letter.";
|
87 |
+
}
|
88 |
+
document.getElementById("guess").value = "";
|
89 |
+
document.getElementById("guess").focus();
|
90 |
+
}
|
91 |
+
|
92 |
+
// Start a new game when the page loads
|
93 |
+
newGame();
|
94 |
+
</script>
|
95 |
+
</body
|