-
Notifications
You must be signed in to change notification settings - Fork 0
/
ind.html
451 lines (383 loc) · 16.1 KB
/
ind.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bingo Game</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="confetti">
</div>
<div class="bingo-container">
<h1>Bingo Game</h1>
<div class="score_card">
<button onclick="decScore()">-</button>
<div class="score-container">
<p>Score: <span id="score">0</span></p>
</div>
<button onclick="incScore()">+</button>
</div>
<table id="bingo-board"></table>
<!-- <div id="custom-input"> -->
<button onclick="enableCustom()">Custom</button>
<button onclick="saveCustom()" disabled>Save</button>
<!-- </div> -->
<button onclick="undo()">Undo</button>
<button onclick="refresh()">New Game</button>
<!-- <button onclick="removeTheme()">Theme</button> -->
</div>
<script>
document.addEventListener("DOMContentLoaded", function () {
// Load default numbers from local storage
const defaultNumbers = JSON.parse(localStorage.getItem("createCustomBoard")) || [];
if (defaultNumbers.length === 25) {
createBoard(defaultNumbers);
} else {
// Initialize the default board
createCustomBoard(); // This will be an empty board
}
updateScore();
});
// Keep track of cell changes for undo feature
const cellChanges = [];
function enableCustom() {
createCustomBoard();
document.querySelector("button[onclick='saveCustom()']").disabled = false;
}
function saveCustom() {
const customNumbers = [];
const inputs = document.querySelectorAll("#bingo-board input");
// Check if the user entered exactly 25 numbers
if (inputs.length !== 25) {
alert("Please enter exactly 25 numbers.");
return;
}
// Check for repeated numbers and numbers out of the range 1-25
const enteredNumbers = new Set();
for (const input of inputs) {
const number = parseInt(input.value, 10);
// Check if the input is a number between 1 and 25
if (isNaN(number) || number < 1 || number > 25) {
alert("Please enter numbers between 1 and 25.");
return;
}
// Check for repeated numbers
if (enteredNumbers.has(number)) {
alert("Please enter unique numbers.");
return;
}
enteredNumbers.add(number);
customNumbers.push(number);
}
// Save custom numbers to local storage
localStorage.setItem("createCustomBoard", JSON.stringify(customNumbers));
createBoard(customNumbers);
document.querySelector("button[onclick='saveCustom()']").disabled = true;
}
function createCustomBoard() {
const board = document.getElementById("bingo-board");
board.innerHTML = "";
const headerRow = document.createElement("tr");
const headerLetters = ["B", "I", "N", "G", "O"];
for (let k = 0; k < 5; k++) {
const headerCell = document.createElement("th");
headerCell.setAttribute("class", "th-cell");
headerCell.textContent = headerLetters[k];
headerRow.appendChild(headerCell);
}
board.appendChild(headerRow);
for (let i = 0; i < 5; i++) {
const row = document.createElement("tr");
for (let j = 0; j < 5; j++) {
const cell = document.createElement("td");
const cellId = `cell-${i}-${j}`;
cell.setAttribute("id", cellId);
cell.setAttribute("class", "cell");
const input = document.createElement("input");
input.setAttribute("type", "number");
input.setAttribute("min", "1");
input.setAttribute("max", "25");
input.value = ''; // Empty value for custom board
cell.appendChild(input);
row.appendChild(cell);
}
board.appendChild(row);
}
}
function createBoard(numbers) {
const board = document.getElementById("bingo-board");
board.innerHTML = "";
const headerRow = document.createElement("tr");
const headerLetters = ["B", "I", "N", "G", "O"];
for (let k = 0; k < 5; k++) {
const headerCell = document.createElement("th");
headerCell.setAttribute("class", "th-cell");
headerCell.textContent = headerLetters[k];
headerRow.appendChild(headerCell);
}
board.appendChild(headerRow);
for (let i = 0; i < 5; i++) {
const row = document.createElement("tr");
for (let j = 0; j < 5; j++) {
const cell = document.createElement("td");
const cellId = `cell-${i}-${j}`;
cell.setAttribute("id", cellId);
cell.setAttribute("class", "cell");
cell.textContent = numbers[i * 5 + j];
// Add click event listener to each cell
cell.addEventListener("click", function () {
markCell(cellId);
checkBingo();
});
row.appendChild(cell);
}
board.appendChild(row);
}
}
function markCell(cellId) {
const cell = document.getElementById(cellId);
const number = cell.textContent;
// Keep track of cell changes for undo
if (cell.textContent !== "X") {
cellChanges.push({ cellId, number });
cell.textContent = "X";
cell.style.backgroundColor = "rgb(255 15 15 / 20%)";
}
}
function refresh() {
location.reload();
}
function undo() {
if (cellChanges.length > 0) {
const lastChange = cellChanges.pop();
const cell = document.getElementById(lastChange.cellId);
cell.textContent = lastChange.number;
cell.style.backgroundColor = "transparent";
}
}
let theme = 0;
let themeBody = document.querySelector('.bingo-container'); // Corrected the class name
function removeTheme() {
if (theme === 0) {
themeBody.style.display = 'none';
theme = 1;
} else {
themeBody.style.display = 'block';
theme = 0;
}
}
function checkBingo() {
const horizontalLines = [];
const verticalLines = [];
const diagonalLines = [[], []];
for (let i = 0; i < 5; i++) {
// Check horizontal lines
horizontalLines.push([
`cell-${i}-0`,
`cell-${i}-1`,
`cell-${i}-2`,
`cell-${i}-3`,
`cell-${i}-4`,
]);
// Check vertical lines
verticalLines.push([
`cell-0-${i}`,
`cell-1-${i}`,
`cell-2-${i}`,
`cell-3-${i}`,
`cell-4-${i}`,
]);
// Check diagonals
diagonalLines[0].push(`cell-${i}-${i}`);
diagonalLines[1].push(`cell-${i}-${4 - i}`);
}
// Function to check if a line is complete
function isLineComplete(line) {
return line.every(
(cellId) => document.getElementById(cellId).textContent === "X"
);
}
const headerLetters = ['B', 'I', 'N', 'G', 'O'];
const strikeThrough = (index) => {
const headerCell = document.querySelector(`#bingo-board tr:first-child th:nth-child(${index + 1})`);
headerCell.style.textDecoration = "line-through";
};
let completedRows = 0;
// Check for completed rows
for (let i = 0; i < 5; i++) {
if (isLineComplete(horizontalLines[i])) {
completedRows++;
}
if (isLineComplete(verticalLines[i])) {
completedRows++;
}
}
// Check for completed diagonals
if (isLineComplete(diagonalLines[0])) {
completedRows++;
}
if (isLineComplete(diagonalLines[1])) {
completedRows++;
}
// Apply line-through based on the number of completed lines
switch (completedRows) {
case 1:
strikeThrough(0); // 'B' for a single completed line
break;
case 2:
strikeThrough(0); // 'B' for two completed lines
strikeThrough(1); // 'I' for two completed lines
break;
case 3:
strikeThrough(0); // 'B' for three completed lines
strikeThrough(1); // 'I' for three completed lines
strikeThrough(2); // 'N' for three completed lines
break;
case 4:
strikeThrough(0); // 'B' for four completed lines
strikeThrough(1); // 'I' for four completed lines
strikeThrough(2); // 'N' for four completed lines
strikeThrough(3); // 'G' for four completed lines
break;
case 5:
strikeThrough(0); // 'B' for five completed lines
strikeThrough(1); // 'I' for five completed lines
strikeThrough(2); // 'N' for five completed lines
strikeThrough(3); // 'G' for five completed lines
strikeThrough(4); // 'O' for five completed lines
break;
default:
break;
}
// Check for Bingo
if (
// Total of 5 horizontal rows
horizontalLines.filter((line) => isLineComplete(line)).length === 5 ||
// Total of 5 vertical columns
verticalLines.filter((line) => isLineComplete(line)).length === 5 ||
// Total of 3 horizontal rows and 2 diagonal lines
(horizontalLines.filter((line) => isLineComplete(line)).length === 3 &&
diagonalLines.filter((line) => isLineComplete(line)).length === 2) ||
// Total of 3 vertical columns and 2 diagonal lines
(verticalLines.filter((line) => isLineComplete(line)).length === 3 &&
diagonalLines.filter((line) => isLineComplete(line)).length === 2) ||
// Total of 4 horizontal rows and 1 diagonal line
(horizontalLines.filter((line) => isLineComplete(line)).length === 4 &&
diagonalLines.filter((line) => isLineComplete(line)).length === 1) ||
// Total of 4 vertical columns and 1 diagonal line
(verticalLines.filter((line) => isLineComplete(line)).length === 4 &&
diagonalLines.filter((line) => isLineComplete(line)).length === 1) ||
// Total of 4 horizontal rows and 2 diagonal lines
(horizontalLines.filter((line) => isLineComplete(line)).length === 4 &&
diagonalLines.filter((line) => isLineComplete(line)).length === 2) ||
// Total of 4 vertical lines and 2 diagonal lines
(verticalLines.filter((line) => isLineComplete(line)).length === 4 &&
diagonalLines.filter((line) => isLineComplete(line)).length === 2) ||
// Total of 4 horizontal rows and 1 vertical line
(horizontalLines.filter((line) => isLineComplete(line)).length === 4 &&
verticalLines.filter((line) => isLineComplete(line)).length === 1) ||
// Total of 3 horizontal rows and 2 vertical lines
(horizontalLines.filter((line) => isLineComplete(line)).length === 3 &&
verticalLines.filter((line) => isLineComplete(line)).length === 2) ||
// Total of 2 horizontal rows and 3 vertical lines
(horizontalLines.filter((line) => isLineComplete(line)).length === 2 &&
verticalLines.filter((line) => isLineComplete(line)).length === 3) ||
// Total of 1 horizontal row and 4 vertical lines
(horizontalLines.filter((line) => isLineComplete(line)).length === 1 &&
verticalLines.filter((line) => isLineComplete(line)).length === 4) ||
// Total of 2 diagonal lines, 2 vertical lines, and 1 horizontal line
(diagonalLines.filter((line) => isLineComplete(line)).length === 2 &&
verticalLines.filter((line) => isLineComplete(line)).length === 2 &&
horizontalLines.filter((line) => isLineComplete(line)).length === 1) ||
// Total of 2 diagonal lines, 1 vertical line, and 2 horizontal lines
(diagonalLines.filter((line) => isLineComplete(line)).length === 2 &&
verticalLines.filter((line) => isLineComplete(line)).length === 1 &&
horizontalLines.filter((line) => isLineComplete(line)).length === 2) ||
// Total of 1 diagonal line, 3 vertical lines, and 1 horizontal line
(diagonalLines.filter((line) => isLineComplete(line)).length === 1 &&
verticalLines.filter((line) => isLineComplete(line)).length === 3 &&
horizontalLines.filter((line) => isLineComplete(line)).length === 1) ||
// Total of 1 diagonal line, 1 vertical line, and 3 horizontal lines
(diagonalLines.filter((line) => isLineComplete(line)).length === 1 &&
verticalLines.filter((line) => isLineComplete(line)).length === 1 &&
horizontalLines.filter((line) => isLineComplete(line)).length === 3) ||
// Total of 1 diagonal line, 2 vertical line, and 2 horizontal lines
(diagonalLines.filter((line) => isLineComplete(line)).length === 1 &&
verticalLines.filter((line) => isLineComplete(line)).length === 2 &&
horizontalLines.filter((line) => isLineComplete(line)).length === 2) ||
// Total of 2 diagonal lines, 2 vertical lines, and 2 horizontal lines
(diagonalLines.filter((line) => isLineComplete(line)).length === 2 &&
verticalLines.filter((line) => isLineComplete(line)).length === 2 &&
horizontalLines.filter((line) => isLineComplete(line)).length === 2) ||
// Total of 2 diagonal lines, 1 vertical line, and 3 horizontal lines
(diagonalLines.filter((line) => isLineComplete(line)).length === 2 &&
verticalLines.filter((line) => isLineComplete(line)).length === 1 &&
horizontalLines.filter((line) => isLineComplete(line)).length === 3) ||
// Total of 1 diagonal line, 3 vertical lines, and 2 horizontal lines
(diagonalLines.filter((line) => isLineComplete(line)).length === 1 &&
verticalLines.filter((line) => isLineComplete(line)).length === 3 &&
horizontalLines.filter((line) => isLineComplete(line)).length === 2) ||
// Total of 1 diagonal line, 1 vertical line, and 4 horizontal lines
(diagonalLines.filter((line) => isLineComplete(line)).length === 1 &&
verticalLines.filter((line) => isLineComplete(line)).length === 1 &&
horizontalLines.filter((line) => isLineComplete(line)).length === 4) ||
// Total of 1 diagonal line, 2 vertical lines, and 3 horizontal lines
(diagonalLines.filter((line) => isLineComplete(line)).length === 1 &&
verticalLines.filter((line) => isLineComplete(line)).length === 2 &&
horizontalLines.filter((line) => isLineComplete(line)).length === 3) ||
// Total of 3 diagonal lines, 1 vertical line, and 1 horizontal line
(diagonalLines.filter((line) => isLineComplete(line)).length === 3 &&
verticalLines.filter((line) => isLineComplete(line)).length === 1 &&
horizontalLines.filter((line) => isLineComplete(line)).length === 1)
) {
confettiContainer.style.display = "block";
addConfettiPieces(confettiContainer, 13);
alert("Bingooooooo!");
return;
}
}
function resetBoard() {
confettiContainer.style.display = "none";
createBoard();
cellChanges.length = 0;
}
// winning moment
function createConfettiPiece() {
const confettiPiece = document.createElement("div");
confettiPiece.className = "confetti-piece";
return confettiPiece;
}
function addConfettiPieces(container, count) {
for (let i = 0; i < count; i++) {
const confettiPiece = createConfettiPiece();
container.appendChild(confettiPiece);
}
}
const confettiContainer = document.querySelector(".confetti");
// updating the score
var score = localStorage.getItem("score") || 0;
function updateScore() {
document.getElementById("score").innerText = score;
}
function incScore() {
score++;
updateScore();
localStorage.setItem("score", score);
}
function decScore() {
if (score > 0) {
score--;
updateScore();
localStorage.setItem("score", score);
}
}
var lastResetDate = localStorage.getItem("lastResetDate");
if (!lastResetDate || new Date().toLocaleDateString() !== lastResetDate) {
score = 0;
localStorage.setItem("score", score);
localStorage.setItem("lastResetDate", new Date().toLocaleDateString());
}
</script>
</body>
</html>