-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
165 lines (151 loc) · 4.09 KB
/
main.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const cl1 = document.getElementById("cl1");
const cl2 = document.getElementById("cl2");
const cl3 = document.getElementById("cl3");
const cl4 = document.getElementById("cl4");
const cl5 = document.getElementById("cl5");
const cl6 = document.getElementById("cl6");
const cl7 = document.getElementById("cl7");
const cl8 = document.getElementById("cl8");
const cl9 = document.getElementById("cl9");
const o = document.getElementById("o");
const x = document.getElementById("x");
const result = document.getElementById("resultdiv");
const restartbtn = document.getElementById("restart");
let isXorO = false;
let gameOver = false;
let isfil = [0, 0, 0, 0, 0, 0, 0, 0, 0];
let xscore = 0;
let oscore = 0;
cl1.onclick = function () { boxClicked(1); }
cl2.onclick = function () { boxClicked(2); }
cl3.onclick = function () { boxClicked(3); }
cl4.onclick = function () { boxClicked(4); }
cl5.onclick = function () { boxClicked(5); }
cl6.onclick = function () { boxClicked(6); }
cl7.onclick = function () { boxClicked(7); }
cl8.onclick = function () { boxClicked(8); }
cl9.onclick = function () { boxClicked(9); }
restartbtn.onclick = function () { restart(); }
const winningCombinations = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
function boxClicked(boxn) {
if (gameOver) {
return;
}
// alert("this");
if (isfil[boxn - 1] == 0) {
isfil[boxn - 1] = 1;
} else {
return;
}
if (isXorO) {
text = 'X';
isXorO = false;
} else {
text = 'O';
isXorO = true;
}
switch (boxn) {
case 1:
cl1.innerText = text;
break;
case 2:
cl2.innerText = text;
break;
case 3:
cl3.innerText = text;
break;
case 4:
cl4.innerText = text;
break;
case 5:
cl5.innerText = text;
break;
case 6:
cl6.innerText = text;
break;
case 7:
cl7.innerText = text;
break;
case 8:
cl8.innerText = text;
break;
case 9:
cl9.innerText = text;
break;
default:
alert("Error Occured...");
console.log("Error...");
}
gameOver = checkForWinner();
if (gameOver) {
if (isXorO) {
document.getElementById("winner").innerText = "O won the Game";
oscore = oscore + 1;
o.innerText = oscore;
} else {
document.getElementById("winner").innerText = "X won the Game";
xscore = xscore + 1;
x.innerText = xscore;
}
result.style.visibility = "visible";
gameOver = true;
} else if (isFullBoard()) {
gameOver = true;
document.getElementById("winner").innerText = "It's a draw!";
result.style.visibility = "visible";
}
}
function checkForWinner() {
let squares = [
cl1.innerText,
cl2.innerText,
cl3.innerText,
cl4.innerText,
cl5.innerText,
cl6.innerText,
cl7.innerText,
cl8.innerText,
cl9.innerText
];
for (let combination of winningCombinations) {
let [a, b, c] = combination;
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
return true;
}
}
return false;
}
function isFullBoard() {
let squares = [
cl1.innerText,
cl2.innerText,
cl3.innerText,
cl4.innerText,
cl5.innerText,
cl6.innerText,
cl7.innerText,
cl8.innerText,
cl9.innerText
];
return squares.every(square => square);
}
function restart() {
for(let i = 0; i < 9; i++){
isfil[i] = 0;
}
cl1.innerText = cl2.innerText = cl3.innerText = cl4.innerText = cl5.innerText = cl6.innerText = cl7.innerText = cl8.innerText = cl9.innerText = "";
isXorO = false;
gameOver = false;
document.getElementById("winner").innerText = "";
result.style.visibility = "hidden";
return;
}