-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
110 lines (91 loc) · 2.54 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
let btnGame= document.querySelectorAll(".btn");
let popupGame=document.querySelector(".popup");
let newbtn=document.getElementById("new-game");
let restart=document.getElementById("again");
let msg=document.getElementById("message");
let winning = [[0,1,2] ,[0,3,6],[2,5,8] ,[6,7,8],[3,4,5],[1,4,7] ,[0,4,8] ,[2,4,6],];
//player "X" plays first
let xTurn= true;
let count =0;
//Disable All buttons
const disableButtons = ()=>{
btnGame.forEach((element) =>(element.disabled = true));
//enable popup
popupGame.classList.remove("hide");
};
//Enable all buttons (For New Game and Restart)
const enableButtons= () =>{
btnGame.forEach((element) => {
element.innerText="";
element.disabled=false;
});
//disable popup
popupGame.classList.add("hide");
};
//this function executed when players wins
const winFunction = (letter) => {
disableButtons();
if(letter == "X"){
msg.innerHTML="X Wins";
}
else {
msg.innerHTML="O Wins";
}
};
//Function draw
const drawFunction =() =>{
disableButtons();
msg.innerHTML=" I'ts a Draw";
};
//new game
newbtn.addEventListener("click", () =>{
count =0;
enableButtons();
});
restart.addEventListener("click", () => {
count =0;
enableButtons();
});
//win logic
const winChecker = () => {
//loopthrough all win patterns
for(let i of winning)
{
let [element1 ,element2,element3] =[
btnGame[i[0]].innerText,
btnGame[i[1]].innerText,
btnGame[i[2]].innerText,
];
//check if elements are filled
//3 empty elements are same and would givewin as would
if(element1 != "" && (element2 != "") && (element3 !="")){
if(element1 == element2 && element2 == element3){
// if all 3 buttons have values then pass the value to winFunction
winFunction(element1);
}
}
}
};
// Display X/0 click
btnGame.forEach((element) => {
element.addEventListener("click",() => {
if(xTurn)
{
xTurn=false;
element.innerText = "X";
element.disabled =true;
}
else{
xTurn =true;
element.innerText="O"
element.disabled =false;
}
count += 1;
if(count == 9){
drawFunction();
}
winChecker();
});
});
//Enable Button and popUp on pageLoad
window.onload = enableButtons;