-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst.js
87 lines (78 loc) · 1.92 KB
/
first.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
75
76
77
78
79
80
81
82
83
84
85
86
87
let boxes = document.querySelectorAll(".box");
let curr = true; //true O, false X
let msg = document.querySelector("#msg");
let msgcontent = document.querySelector(".msgcontent");
let reset = document.querySelector("#reset");
let restart = document.querySelector("#restart");
reset.addEventListener("click",()=>{
for(box of boxes){
box.innerText = "";
box.disabled = false;
}
curr = true;
msgcontent.classList.add("hide");
});
restart.addEventListener("click",()=>{
for(box of boxes){
box.innerText = "";
box.disabled = false;
}
curr = true;
msgcontent.classList.add("hide");
});
boxes.forEach((box)=>{
box.addEventListener("click",()=>{
if(curr){
box.innerText="O";
curr = false;
}else{
box.innerText="X";
curr = true;
}
box.disabled = true;
checkWinner();
checkTie();
});
});
const checkTie = () =>{
let full=true;
for(box of boxes){
if(box.innerText===""){
full=false;
}
}
if(full===true){
msg.innerText = "Game Draw";
msgcontent.classList.remove("hide");
checkWinner();
}
}
const showWinner = (winner) =>{
msg.innerText = `Congratulations, Winner is ${winner}`;
msgcontent.classList.remove("hide");
}
const winPatterns = [
[0,1,2],
[0,3,6],
[0,4,8],
[1,4,7],
[2,5,8],
[2,4,6],
[3,4,5],
[6,7,8]
];
const checkWinner = () =>{
for(pattern of winPatterns){
pos1=boxes[pattern[0]].innerText;
pos2=boxes[pattern[1]].innerText;
pos3=boxes[pattern[2]].innerText;
if(pos1!=="" && pos2!=="" && pos3!==""){
if(pos1===pos2 && pos2===pos3){
for(box of boxes){
box.disabled=true;
}
showWinner(pos1);
}
}
}
}