-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
113 lines (90 loc) · 3.59 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
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
document.addEventListener('DOMContentLoaded', function() {
console.log('Bingo games to initialize:', bingoGames.length);
bingoGames.forEach(function(game, index) {
console.log('Initializing game:', index + 1);
initBingoGame(game.id, game.words);
});
});
function initBingoGame(containerId, words) {
const container = document.getElementById(containerId);
if (!container) {
console.error('Container not found:', containerId);
return;
}
const bingoCard = container.querySelector('.bingo-card');
const popup = container.querySelector('.bingo-popup');
const closePopup = container.querySelector('button');
const popupMessage = container.querySelector('h2');
const bingoSound = container.querySelector('audio');
let completedRows = new Set();
let completedColumns = new Set();
console.log('Words for this game:', words.length);
// Ensure we have exactly 16 words
words = words.slice(0, 16);
// Shuffle the words
shuffleArray(words);
// Clear existing content
bingoCard.innerHTML = '';
// Create bingo card
for (let i = 0; i < 16; i++) {
const cell = document.createElement('div');
cell.className = 'bingo-cell';
cell.textContent = words[i] || '';
cell.dataset.row = Math.floor(i / 4);
cell.dataset.col = i % 4;
cell.addEventListener('click', () => toggleCell(cell));
bingoCard.appendChild(cell);
}
console.log('Cells created:', bingoCard.children.length);
// Explicitly set grid layout
bingoCard.style.display = 'grid';
bingoCard.style.gridTemplateColumns = 'repeat(4, 1fr)';
bingoCard.style.gridTemplateRows = 'repeat(4, 1fr)';
function toggleCell(cell) {
cell.classList.toggle('selected');
const row = parseInt(cell.dataset.row);
const col = parseInt(cell.dataset.col);
checkRow(row);
checkColumn(col);
checkAllCompleted();
}
function checkRow(rowIndex) {
if (completedRows.has(rowIndex)) return;
const cells = bingoCard.querySelectorAll(`.bingo-cell[data-row="${rowIndex}"]`);
const isRowComplete = Array.from(cells).every(cell => cell.classList.contains('selected'));
if (isRowComplete) {
completedRows.add(rowIndex);
showPopup(`Reihe ${rowIndex + 1} abgeschlossen!`);
}
}
function checkColumn(colIndex) {
if (completedColumns.has(colIndex)) return;
const cells = bingoCard.querySelectorAll(`.bingo-cell[data-col="${colIndex}"]`);
const isColumnComplete = Array.from(cells).every(cell => cell.classList.contains('selected'));
if (isColumnComplete) {
completedColumns.add(colIndex);
showPopup(`Spalte ${colIndex + 1} abgeschlossen!`);
}
}
function checkAllCompleted() {
if (completedRows.size === 4 && completedColumns.size === 4) {
setTimeout(() => showPopup("Glückwunsch! Alle Reihen und Spalten abgeschlossen!"), 1500);
}
}
function showPopup(message) {
popupMessage.textContent = message;
popup.style.display = 'flex';
// Play sound effect
bingoSound.currentTime = 0;
bingoSound.play().catch(e => console.log("Audio play failed:", e));
}
closePopup.addEventListener('click', () => {
popup.style.display = 'none';
});
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
}