-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
88 lines (63 loc) · 2.64 KB
/
Copy pathscript.js
File metadata and controls
88 lines (63 loc) · 2.64 KB
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
var ultimoValor = "O";
var todosInputs = []
function obterProximo(x){
if(x.currentTarget.innerHTML) return;
x.currentTarget.innerHTML = obterProximaJogada();
verificarGanhador();
}
function obterProximaJogada(){
ultimoValor = ultimoValor == 'X'
? ultimoValor = 'O'
: ultimoValor = 'X';
return ultimoValor;
}
function verificarGanhador(){
var elementos = ['a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3'];
elementos.forEach(function(id) {
var elemento = document.getElementById(id);
todosInputs.push(elemento);
});
if(todosInputs.every(x => !!x.innerHTML)) finalizarPartida();
var [a1, a2, a3, b1, b2, b3, c1, c2, c3] = todosInputs.map(elemento => elemento.innerHTML);
let l1hx = [a1, a2, a3].every(valor => valor === 'X');
let l2hx = [b1, b2, b3].every(valor => valor === 'X');
let l3hx = [c1, c2, c3].every(valor => valor === 'X');
let l1vx = [a1, b1, c1].every(valor => valor === 'X');
let l2vx = [a2, b2, c2].every(valor => valor === 'X');
let l3vx = [a3, b3, c3].every(valor => valor === 'X');
let v1x = [a3, b2, c1].every(valor => valor === 'X');
let v2x = [a1, b2, c3].every(valor => valor === 'X');
if(l1hx || l2hx || l3hx || l1vx || l2vx || l3vx || v1x || v2x){ abrirModal(); }
let l1ho = [a1, a2, a3].every(valor => valor === 'O');
let l2ho = [b1, b2, b3].every(valor => valor === 'O');
let l3ho = [c1, c2, c3].every(valor => valor === 'O');
let l1vo = [a1, b1, c1].every(valor => valor === 'O');
let l2vo = [a2, b2, c2].every(valor => valor === 'O');
let l3vo = [a3, b3, c3].every(valor => valor === 'O');
let v1o = [a3, b2, c1].every(valor => valor === 'O');
let v2o = [a1, b2, c3].every(valor => valor === 'O');
if(l1ho || l2ho || l3ho || l1vo || l2vo || l3vo || v1o || v2o){ abrirModal(); }
}
var modal = document.getElementById("myModal");
var btn = document.getElementById("myBtn");
var span = document.getElementsByClassName("close")[0];
function abrirModal() {
let mensagem = document.getElementById('mensagemGanhador');
mensagem.innerHTML = `O Jogador '${ultimoValor}' ganhou a partida`
modal.style.display = "block";
}
function finalizarPartida() {
let mensagem = document.getElementById('mensagemGanhador');
mensagem.innerHTML = "Deu velha, começe uma nova partida!"
modal.style.display = "block";
}
span.onclick = function() {
modal.style.display = "none";
todosInputs.map(x => x.innerHTML = '')
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
todosInputs.map(x => x.innerHTML = '')
}
}