-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
116 lines (89 loc) · 2.41 KB
/
game.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
111
112
113
114
115
116
// accessing HTML elements
let boxes=document.querySelectorAll(".box");
let reset=document.querySelector("#reset");
let msg=document.querySelector("#msg");
let msgContainer=document.querySelector(".msg-container");
let newGameBtn=document.querySelector("#newgame");
let count=0;
let turnO=true; //x or o
// defining winnig pattern
const winning_pattern=[
[0,1,2],
[0,3,6],
[0,4,8],
[3,4,5],
[6,7,8],
[1,4,7],
[2,5,8],
[2,4,6]
];
// adding event for each box to have X or O on clicking
boxes.forEach((box) =>{
box.addEventListener("click",() =>{
if (turnO){ //player O
box.innerText="0";
turnO=false;
}else{
box.innerText="X"; //player x
turnO=true;;
}
box.disabled=true;
count++;
let isWinner=checkWinner();
if(count===9 && !isWinner){
gameDraw();
}
});
});
//function for disabling button
disableAllBtn=()=>{
for (let box of boxes){
box.disabled=true;
}
};
//function for enable button
const enableBoxes = () => {
for (let box of boxes) {
box.disabled = false;
box.innerText = "";
}
};
//showing winner on screen
const showWinner=(winner) => {
msg.innerText =`Congratulations,Winner is ${winner}`;
msg.style.fontSize='50px';
msgContainer.classList.remove("hide");
disableAllBtn();
};
//checking who is winner
const checkWinner = () =>{
for (let pattern of winning_pattern){
let val1=boxes[pattern[0]].innerText;
let val2=boxes[pattern[1]].innerText;
let val3=boxes[pattern[2]].innerText;
if (val1!="" && val2!="" && val3!=""){
if (val1===val2 && val2===val3){
// won=true;
showWinner(val1);
// disapleBtn()
return true;
}
}
console.log(count);
}
};
//draw case
const gameDraw = () => {
msg.innerText = `Game was a Draw.`;
msgContainer.classList.remove("hide");
disableBoxes();
};
//function for reset game and new game
const resetGame = () => {
turnO = true;
count = 0;
enableBoxes();
msgContainer.classList.add("hide");
};
newgame.addEventListener("click", resetGame);
reset.addEventListener("click", resetGame);