-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
59 lines (55 loc) · 1.75 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
let gameover = new Audio("victory.mp3");
let turn = 'X';
// FUNCTION TO CHANGE TURN
let changeTurn = () => {
return turn === 'X' ? 'O' : 'X';
}
let isGameOver = false;
let winningPattern = [
[0, 1, 2],
[0, 3, 6],
[0, 4, 8],
[2, 4, 6],
[2, 5, 8],
[6, 7, 8],
[3, 4, 5],
[1, 4, 7]
]
// console.log(document.getElementsByClassName('box-text'))
// FUNCTION TO CHECK VICTORY
let checkVictory = () => {
let boxText = document.getElementsByClassName('box-text');
winningPattern.forEach(e => {
var checkOne = (boxText[e[0]].innerText === boxText[e[1]].innerText);
var checkTwo = (boxText[e[1]].innerText === boxText[e[2]].innerText);
var notEmpty = (boxText[e[0]].innerText !== '');
if (checkOne && checkTwo && notEmpty) {
document.querySelector('.info').innerText = boxText[e[0]].innerText + " won 🥳";
isGameOver = true;
gameover.play();
}
})
}
// game logic
let boxes = Array.from(document.getElementsByClassName('box'))
// console.log(boxes)
boxes.forEach(element => {
let boxText = element.querySelector('.box-text');
element.addEventListener('click', () => {
if (boxText.innerText === '') {
boxText.innerText = turn;
turn = changeTurn();
checkVictory();
if (!isGameOver) {
document.getElementsByClassName('info')[0].innerText = "Turn for " + turn;
}
}
})
});
reset.addEventListener('click', () => {
let boxText = document.getElementsByClassName('box-text');
Array.from(boxText).forEach(x => {
x.innerText = ''
})
document.querySelector('.info').innerText = "Turn for " + turn;
})