-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
201 lines (174 loc) · 6.21 KB
/
index.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Crossword Puzzle Game</title>
<style>
/* Add your CSS styles here */
#puzzle {
border-collapse: collapse;
margin: 20px auto;
}
#puzzle td {
width: 40px;
height: 40px;
border: 1px solid black;
text-align: center;
vertical-align: middle;
cursor: pointer; /* Add cursor pointer for better user experience */
}
.selected {
background-color: lightgreen;
}
.wrong {
background-color: #ffcccc;
}
.correct {
background-color: lightgreen;
}
#controls {
margin-top: 20px;
}
#controls button {
margin: 0 10px;
font-size: 16px;
padding: 5px 10px;
}
#timer-container {
display: inline-block;
vertical-align: middle;
}
#timer {
font-size: 24px;
font-weight: bold;
margin-bottom: 10px;
display: inline-block;
margin-right: 10px;
}
#result-label {
font-size: 24px;
font-weight: bold;
margin-top: 20px;
text-align: center;
}
</style>
</head>
<body>
<div id="game-container">
<table id="puzzle"></table>
<div id="controls">
<button id="ok-btn">OK</button>
<button id="refresh-btn">🔁</button>
<div id="timer-container">
<img src="timer_icon.png" alt="⌛" height="24px" width="24px">
<div id="timer">45</div>
</div>
<div id="result-label"></div>
</div>
</div>
<script>
// List of words for the puzzle
const words = ["example", "word", "puzzle", "crossword", "game"];
// Variable to store the selected word
let selectedLetters = "";
// Function to generate random letters for the puzzle
function generateRandomLetters() {
let randomLetters = [];
for (let i = 0; i < 25; i++) {
let randomWord = words[Math.floor(Math.random() * words.length)];
let randomLetter = randomWord.charAt(Math.floor(Math.random() * randomWord.length));
randomLetters.push(randomLetter);
}
return randomLetters;
}
// Function to populate the puzzle table with random letters
function populatePuzzle() {
let puzzleTable = document.getElementById("puzzle");
let letters = generateRandomLetters();
let cellIndex = 0;
for (let i = 0; i < 5; i++) {
let row = puzzleTable.insertRow(i);
for (let j = 0; j < 5; j++) {
let cell = row.insertCell(j);
cell.textContent = letters[cellIndex++];
cell.addEventListener("click", function() {
toggleCell(cell);
});
}
}
}
// Function to toggle cell selection
function toggleCell(cell) {
if (cell.classList.contains("selected")) {
cell.classList.remove("selected");
selectedLetters = selectedLetters.replace(cell.textContent, "");
} else {
// Check if cell is already selected
if (!cell.classList.contains("selected")) {
cell.classList.add("selected");
selectedLetters += cell.textContent;
}
}
}
// Function to validate the selected word
function validateWord() {
let selectedCells = document.querySelectorAll(".selected");
if (selectedCells.length === 0) return;
if (words.includes(selectedLetters)) {
selectedCells.forEach(cell => cell.classList.add("correct"));
document.getElementById("result-label").textContent = "Correct!";
setTimeout(refreshTable, 1000); // Shuffle the table after 1 second
} else {
selectedCells.forEach(cell => cell.classList.add("wrong"));
setTimeout(resetSelection, 1000); // Reset selection after 1 second
}
}
// Function to reset cell selection and clear wrong indication
function resetSelection() {
let selectedCells = document.querySelectorAll(".selected");
selectedCells.forEach(cell => {
cell.classList.remove("selected");
cell.classList.remove("wrong");
});
selectedLetters = "";
document.getElementById("result-label").textContent = ""; // Clear result label
}
// Function to refresh the puzzle table
function refreshTable() {
let puzzleTable = document.getElementById("puzzle");
puzzleTable.innerHTML = "";
populatePuzzle();
selectedLetters = ""; // Reset selected letters
document.getElementById("result-label").textContent = ""; // Clear result label
}
// Event listener for OK button
document.getElementById("ok-btn").addEventListener("click", function() {
validateWord();
});
// Event listener for Refresh button
document.getElementById("refresh-btn").addEventListener("click", function() {
refreshTable();
});
// Populate the puzzle table and start the timer when the page loads
populatePuzzle();
// Function to start the timer
function startTimer() {
let timeLeft = 45;
let timerElement = document.getElementById("timer");
let timerInterval = setInterval(function() {
timeLeft--;
timerElement.textContent = timeLeft;
if (timeLeft <= 10) {
timerElement.style.color = "red";
}
if (timeLeft === 0) {
clearInterval(timerInterval);
alert("Time's up!");
}
}, 1000);
}
startTimer(); // Start the timer when the page loads
</script>
</body>
</html>