-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
75 lines (67 loc) · 2.2 KB
/
scripts.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
let turn = 'circle'
let numberCheck = 0
for(let i = 1; i <= 3; i++){
for(let j = 1; j <=3; j++){
document.getElementById(`ttt${i}${j}`).addEventListener("click", function(){
var elemt = document.getElementById(`ttt${i}${j}`)
if(!elemt.className.split(' ').includes('active')) {
elemt.classList.add(turn)
elemt.classList.add('active')
turn = turn === 'xis' ? 'circle' : 'xis'
if (numberCheck + 1 >= 5) {
HaveWinner()
}
numberCheck++
}
});
document.getElementById(`ttt${i}${j}`).addEventListener("mouseenter", function(){
var elemt = document.getElementById(`ttt${i}${j}`)
if(!elemt.className.split(' ').includes('active')) {
elemt.classList.add(turn)
}
});
document.getElementById(`ttt${i}${j}`).addEventListener("mouseleave", function(){
var elemt = document.getElementById(`ttt${i}${j}`)
if(!elemt.className.split(' ').includes('active')) {
elemt.classList.remove(turn)
}
});
}
}
function HaveWinner () {
// Verifica as linhas
for(let i = 1; i <= 3; i++){
check(`${i}1`, `${i}2`, `${i}3`)
check(`1${i}`, `2${i}`, `3${i}`)
}
// Verifica as diagonais
check('11', '22', '33')
check('13', '22', '31')
}
function check(first, secont, third) {
var elemt = document.getElementById(`ttt${first}`)
if(elemt.className.split(' ').includes('active')) {
const lineClass = elemt.className.split(' ').includes('xis') ? 'xis' : 'circle'
var parent2 = document.getElementById(`ttt${secont}`)
var parent3 = document.getElementById(`ttt${third}`)
if (parent2.className.split(' ').includes(lineClass)
&& parent3.className.split(' ').includes(lineClass)) {
const button = document.querySelectorAll('button')
for (var i = 0, len = button.length; i<len; i++){
button[i].disabled = true;
}
elemt.classList.add('win')
parent2.classList.add('win')
parent3.classList.add('win')
}
}
}
function reset () {
turn = 'circle'
numberCheck = 0
const button = document.querySelectorAll('button')
for (var i = 0, len = button.length; i<len; i++){
button[i].classList = '';
button[i].disabled = false;
}
}