-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript1.js
228 lines (211 loc) · 6 KB
/
script1.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
const e0=document.getElementById("e0");
const e1=document.getElementById("e1");
const e2=document.getElementById("e2");
const e3=document.getElementById("e3");
const e4=document.getElementById("e4");
const e5=document.getElementById("e5");
const e6=document.getElementById("e6");
const e7=document.getElementById("e7");
const e8=document.getElementById("e8");
let gameboard=["","","","","","","","",""];
let count=0;
e0.addEventListener("click",()=>{
if (gameboard[0]===""){
gameboard[0]=switchplayer();
showboard(gameboard);
checkwinner(gameboard);
setTimeout(() => {
botplayer(gameboard)
}, 2000);
}
});
e1.addEventListener("click",()=>{
if (gameboard[1]===""){
gameboard[1]=switchplayer();
showboard(gameboard);
checkwinner(gameboard);
setTimeout(() => {
botplayer(gameboard)
}, 2000);
}
});
e2.addEventListener("click",()=>{
if (gameboard[2]===""){
gameboard[2]=switchplayer();
showboard(gameboard);
checkwinner(gameboard);
setTimeout(() => {
botplayer(gameboard)
}, 2000);
}
});
e3.addEventListener("click",()=>{
if (gameboard[3]===""){
gameboard[3]=switchplayer();
showboard(gameboard);
checkwinner(gameboard);
setTimeout(() => {
botplayer(gameboard)
}, 2000);
}
});
e4.addEventListener("click",()=>{
if (gameboard[4]===""){
gameboard[4]=switchplayer();
showboard(gameboard);
checkwinner(gameboard);
setTimeout(() => {
botplayer(gameboard)
}, 2000);
}
});
e5.addEventListener("click",()=>{
if (gameboard[5]===""){
gameboard[5]=switchplayer();
showboard(gameboard);
checkwinner(gameboard);
setTimeout(() => {
botplayer(gameboard)
}, 2000);
}
});
e6.addEventListener("click",()=>{
if (gameboard[6]===""){
gameboard[6]=switchplayer();
showboard(gameboard);
checkwinner(gameboard);
setTimeout(() => {
botplayer(gameboard)
}, 2000);
}
});
e7.addEventListener("click",()=>{
if (gameboard[7]===""){
gameboard[7]=switchplayer();
showboard(gameboard);
checkwinner(gameboard);
setTimeout(() => {
botplayer(gameboard)
}, 2000);
}
});
e8.addEventListener("click",()=>{
if (gameboard[8]===""){
gameboard[8]=switchplayer();
showboard(gameboard);
checkwinner(gameboard);
setTimeout(() => {
botplayer(gameboard)
}, 2000);
}
});
function showboard(gameboard){
e0.innerText=gameboard[0];
e1.innerText=gameboard[1];
e2.innerText=gameboard[2];
e3.innerText=gameboard[3];
e4.innerText=gameboard[4];
e5.innerText=gameboard[5];
e6.innerText=gameboard[6];
e7.innerText=gameboard[7];
e8.innerText=gameboard[8];
// console.log(gameboard);
}
function switchplayer(){
count++;
if (count>=5){
gamereset();
document.getElementById("win").innerText=gameboard[2]+" Game Draw";
}
else{
document.getElementById("player").innerText="Player : "+"0";
return "X";
}
}
function botplayer(gameboard){
const ran=Math.floor(Math.random() * 8);
if(gameboard[ran]==""){
document.getElementById("player").innerText="Player : "+"X";
gameboard[ran]="0";
showboard(gameboard);
checkwinner(gameboard);
}
else{
botplayer(gameboard);
}
}
function checkwinner(gameboard){
if (gameboard[0]==gameboard[1] && gameboard[0]==gameboard[2]){
if(gameboard[0]!=""){
document.getElementById("win").innerText=gameboard[0]+" Player Won";
setTimeout(() => {
gamereset();
}, 2000);
}
}
else if (gameboard[3]==gameboard[4] && gameboard[3]==gameboard[5]){
if(gameboard[3]!=""){
document.getElementById("win").innerText=gameboard[3]+" Player Won";
setTimeout(() => {
gamereset();
}, 2000);
}
}
else if (gameboard[6]==gameboard[7] && gameboard[6]==gameboard[8]){
if(gameboard[6]!=""){
document.getElementById("win").innerText=gameboard[6]+" Player Won";
setTimeout(() => {
gamereset();
}, 2000);
}
}
else if (gameboard[0]==gameboard[3] && gameboard[0]==gameboard[6]){
if(gameboard[0]!=""){
document.getElementById("win").innerText=gameboard[0]+" Player Won";
setTimeout(() => {
gamereset();
}, 2000);
}
}
else if (gameboard[1]==gameboard[4] && gameboard[1]==gameboard[7]){
if(gameboard[1]!=""){
document.getElementById("win").innerText=gameboard[1]+" Player Won";
setTimeout(() => {
gamereset();
}, 2000);
}
}
else if (gameboard[2]==gameboard[5] && gameboard[2]==gameboard[8]){
if(gameboard[2]!=""){
document.getElementById("win").innerText=gameboard[2]+" Player Won";
setTimeout(() => {
gamereset();
}, 2000);
}
}
else if (gameboard[0]==gameboard[4] && gameboard[0]==gameboard[8]){
if(gameboard[0]!=""){
document.getElementById("win").innerText=gameboard[0]+" Player Won";
setTimeout(() => {
gamereset();
}, 2000);
}
}
else if (gameboard[2]==gameboard[4] && gameboard[2]==gameboard[6]){
if(gameboard[2]!=""){
document.getElementById("win").innerText=gameboard[2]+" Player Won";
setTimeout(() => {
gamereset();
}, 2000);
}
}
}
function gamereset(){
gameboard=["","","","","","","","",""];
count=0;
setTimeout(() => {
showboard(gameboard);
document.getElementById("win").innerText="";
document.getElementById("player").innerText="Player : X";
}, 2000);
}