-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
84 lines (74 loc) · 2.66 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
const totalNumeros = 90;
const tamanhoCartela = 25;
let poolNumeros = [];
let numerosSorteados = [];
let numerosCartela = [];
let jogoAtivo = true;
function initGame() {
poolNumeros = [];
numerosSorteados = [];
numerosCartela = [];
jogoAtivo = true;
for (let i = 1; i <= totalNumeros; i++) {
poolNumeros.push(i);
}
const copiaPool = [...poolNumeros];
shuffle(copiaPool);
numerosCartela = copiaPool.slice(0, tamanhoCartela);
renderCartela();
document.getElementById("drawnNumber").innerText = "Número Sorteado: -";
document.getElementById("message").innerText = "";
document.getElementById("drawBtn").disabled = false;
document.getElementById("restartBtn").style.display = "none";
updateCounters();
}
function shuffle(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]];
}
return array;
}
function renderCartela() {
const bingoBoard = document.getElementById("bingoBoard");
bingoBoard.innerHTML = "";
numerosCartela.forEach(num => {
const cell = document.createElement("div");
cell.classList.add("cell");
cell.innerText = num;
cell.dataset.numero = num;
cell.addEventListener("click", () => {
if (!jogoAtivo) return;
if (numerosSorteados.includes(Number(cell.dataset.numero)) && !cell.classList.contains("marked")) {
cell.classList.add("marked");
verificarVitoria();
updateCounters();
}
});
bingoBoard.appendChild(cell);
});
}
function sortearNumero() {
if (!jogoAtivo) return;
const indice = Math.floor(Math.random() * poolNumeros.length);
const numeroSorteado = poolNumeros.splice(indice, 1)[0];
numerosSorteados.push(numeroSorteado);
document.getElementById("drawnNumber").innerText = "Número Sorteado: " + numeroSorteado;
document.querySelectorAll(".cell").forEach(cell => {
if (Number(cell.dataset.numero) === numeroSorteado) {
cell.classList.add("highlight");
setTimeout(() => { cell.classList.remove("highlight"); }, 1000);
}
});
updateCounters();
}
function updateCounters() {
document.getElementById("sorteadosCount").innerText = numerosSorteados.length;
document.getElementById("restantesCount").innerText = totalNumeros - numerosSorteados.length;
const markedCount = document.querySelectorAll(".cell.marked").length;
document.getElementById("acertadosCount").innerText = markedCount;
document.getElementById("aAcertarCount").innerText = tamanhoCartela - markedCount;
}
document.getElementById("drawBtn").addEventListener("click", sortearNumero);
document.getElementById("restartBtn").addEventListener("click", initGame);
initGame();