-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
65 lines (57 loc) · 2.02 KB
/
main.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
const area = document.getElementById('area');
const contentWrapper = document.getElementById('content');
const modalResult = document.getElementById('modal-result-wrapper');
const overlay = document.getElementById('overlay');
const btnClose = document.getElementById('btn-close');
let move = 0;
let result;
area.addEventListener('click', e => {
if (e.target.className = 'box') {
move % 2 === 0 ? e.target.innerHTML = 'X' : e.target.innerHTML = 'O';
move++;
check();
}
})
const check = () => {
const boxes = document.getElementsByClassName('box');
const arr = [
[0,1,2],
[0,4,8],
[0,3,6],
[3,4,5],
[6,7,8],
[1,4,7],
[2,5,8],
[2,4,6],
];
for(let i = 0; i < arr.length; i++) {
if(
boxes[arr[i][0]].innerHTML == 'X' && boxes[arr[i][1]].innerHTML == 'X' && boxes[arr[i][2]].innerHTML == 'X'
) {
result = 'крестики';
prepareResult(result);
console.log(result);
} else if (
boxes[arr[i][0]].innerHTML == 'O' && boxes[arr[i][1]].innerHTML == 'O' && boxes[arr[i][2]].innerHTML == 'O'
) {
result ='нолики';
prepareResult(result);
console.log(result);
} else if (move==9){
counter ='..Что? У нас ничья!';
prepareResult(counter);
console.log(result);
console.log(counter);
}
}
}
const prepareResult = winner => {
contentWrapper.innerHTML = `Победили ${winner} !`;
modalResult.style.display = 'block';
}
const closeModal = () => {
modalResult.style.display = 'none';
location.reload();
}
overlay.addEventListener('click', closeModal);
btnClose.addEventListener('click', closeModal);