-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
76 lines (58 loc) · 1.69 KB
/
app.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
const gameBoard=document.querySelector('#gameBoard');
const gameInfo=document.querySelector('#gameInfo');
const boardArray=[" "," "," "," "," "," "," "," "," "];
let go="circle";
gameInfo.textContent="circle goes First!";
function createBoard(){
boardArray.forEach((_cell,index)=>{
const cell=document.createElement('div');
cell.classList.add('square');
cell.setAttribute('id',index)
gameBoard.append(cell);
cell.addEventListener('click',addGo);
}
)
};
createBoard();
function addGo(e){
// console.log(e.target);
const goDisplay=document.createElement('div');
goDisplay.classList.add(go);
e.target.append(goDisplay);
if(go==="circle")
{go="cross"}
else
{go="circle"}
gameInfo.textContent= go+" Goes Now!";
e.target.removeEventListener('click',addGo);
checkScore();
}
function checkScore() {
const allSqrs = document.querySelectorAll('.square');
winningCombo = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
const circleWins = winningCombo.some(array => {
return array.every(cell => allSqrs[cell].firstChild?.classList.contains('circle'))
})
if (circleWins) {
gameInfo.textContent = "Circle Wins!!"
allSqrs.forEach(square => square.replaceWith(square.cloneNode(true)))
return;
}
const crossWins = winningCombo.some(array => {
return array.every(cell => allSqrs[cell].firstChild?.classList.contains('cross'))
})
if (crossWins) {
gameInfo.textContent = "Cross Wins!!"
allSqrs.forEach(square => square.replaceWith(square.cloneNode(true)))
return;
}
}