-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
67 lines (60 loc) · 1.88 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
60
61
62
63
64
65
66
67
let boxes = document.querySelectorAll(".box");
let resetbtn = document.querySelector("#reset-btn");
let player1 = document.querySelector("#player1");
let player2 = document.querySelector("#player2");
let turnO = true; //player 1 -> O
document.querySelector("#player1").style.boxShadow = "0 0 1rem rgba(255,0,0,1)";
let ans = document.querySelector("#answer");
const patterns = [
[0,1,2],
[3,4,5],
[6,7,8],
[0,3,6],
[1,4,7],
[2,5,8],
[0,4,8],
[2,4,6]
];
resetbtn.addEventListener("click",()=>{
boxes.forEach((box)=>{
box.innerText="";
box.disabled=false;
})
ans.innerText="";
ans.disabled=false;
document.querySelector("#player1").style.boxShadow="0 0 1rem rgba(255,0,0,1)";
document.querySelector("#player2").style.boxShadow="";
});
boxes.forEach((box)=>{
box.addEventListener("click",()=>{
if(turnO==true){ //player O
box.innerText = "X";
turnO = false;
document.querySelector("#player1").style.boxShadow = "";
document.querySelector("#player2").style.boxShadow = "0 0 1rem rgba(255,0,0,1)";
}
else{ // player X
box.innerText = "O";
turnO = true;
document.querySelector("#player2").style.boxShadow = "";
document.querySelector("#player1").style.boxShadow = "0 0 1rem rgba(255,0,0,1)";
}
box.disabled = true;
checkWinner();
})
});
const checkWinner = () =>{
for(let i=0;i<patterns.length;i++){
let t1 = boxes[patterns[i][0]].innerText;
let t2 = boxes[patterns[i][1]].innerText;
let t3 = boxes[patterns[i][2]].innerText;
if(t1!=="" && t1===t2 && t2===t3){
if(turnO){
ans.innerText = "Winner is Player 2";
}
else{
ans.innerText = "Winner is Player 1";
}
}
}
};