forked from Abhilash-0322/Tic_Tac_Toe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic.js
218 lines (215 loc) · 4.76 KB
/
tic.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
let boxes = document.querySelectorAll(".box");
let Reset = document.querySelector("#resetbutton");
let turn0 = true;
let Body = document.querySelector("body");
let newgame = document.querySelector("#newgame");
let windialogue = "<h1>CONGRATULATIONS \nWinner Is</h1>";
let Div = document.createElement("div");
let counter = 0;
let won = false;
let color_val =10; Math.floor(Math.random() * 63);
let colors_l = [
"crimson",
"blue",
"lime",
"red",
"green",
"orange",
"yellow",
"cyan",
"magenta",
"purple",
"pink",
"teal",
"gold",
"silver",
"brown",
"black",
"white",
"gray",
"darkblue",
"darkgreen",
"darkred",
"darkorange",
"darkcyan",
"darkmagenta",
"darkpurple",
"darkpink",
"darkteal",
"darkgoldenrod",
"darkslategray",
"indigo",
"darkolivegreen",
"darkslateblue",
"darkseagreen",
"darkturquoise",
"darkviolet",
"deeppink",
"mediumblue",
"mediumseagreen",
"mediumslateblue",
"mediumspringgreen",
"mediumturquoise",
"mediumvioletred",
"navy",
"olive",
"orangered",
"rosybrown",
"royalblue",
"saddlebrown",
"salmon",
"sandybrown",
"seagreen",
"sienna",
"skyblue",
"slateblue",
"springgreen",
"steelblue",
"tan",
"thistle",
"tomato",
"turquoise",
"violet",
"wheat",
"yellowgreen",
];
let color_r;
// let Toggle=document.getElementById("toggle");
const aiPlayer = "X";
let AI = true;
// Toggle.addEventListener("click",()=>{
// if(AI){
// AI=false;
// }
// else{
// AI=true;
// }
// selectmode();
// })
// Function to make the AI move
const makeAIMove = () => {
// For simplicity, let's make a random move in an empty spot
// Filter out the empty spots
const emptySpots = Array.from(boxes).filter((box) => box.innerText === "");
// Choose a random empty spot
const randomIndex = Math.floor(Math.random() * emptySpots.length);
const randomEmptyBox = emptySpots[randomIndex];
// Make the AI move
randomEmptyBox.innerText = aiPlayer;
randomEmptyBox.disabled = true;
turn0 = true;
// Check for a winner after the AI move
checkwinner();
};
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],
];
function modeAI() {
// makeAIMove();
boxes.forEach((box) => {
box.addEventListener("click", () => {
console.log("box was clicked");
if (turn0) {
box.innerHTML = "0";
turn0 = false;
}
box.disabled = true;
// AI move after player's move
checkwinner();
if (!won) {
makeAIMove();
}
});
});
}
// function modeMutiplayer(){
// boxes.forEach((box)=>{
// box.addEventListener("click",()=>{
// console.log("box was clicked");
// if(turn0){
// box.innerHTML="0";
// turn0=false;
// }
// else{
// box.innerHTML="X";
// turn0=true;
// }
// box.disabled=true;
// checkwinner();
// })
// })
// }
const Newgame = () => {
won = false;
counter = 0;
newgame.style.display = "none";
Div.style.display = "none";
boxes.forEach((box) => {
box.disabled = false;
color_val=Math.floor(Math.random() * 63);
// color_r=colors_l[color_val];
// box.style.backgroundColor = color_r;
box.innerText = "";
});
makeAIMove();
};
newgame.addEventListener("click", () => {
Newgame();
});
Reset.addEventListener("click", () => {
Newgame();
});
const winEvents = () => {
console.log("We GOT THE WINNER");
};
const checkwinner = () => {
counter++;
for (let pattern of winPatterns) {
console.log(pattern[0], pattern[1], pattern[2]);
console.log(
boxes[pattern[0]].innerText,
boxes[pattern[1]].innerText,
boxes[pattern[2]].innerText
);
if (counter === 9 && !won) {
Body.appendChild(Div).innerHTML = "<h1>GAME HAS BEEN DRAWN</h1>";
Div.style.display = "unset";
newgame.style.display = "unset";
console.log("Drawn");
}
console.log(boxes[pattern[0]], boxes[pattern[1]], boxes[pattern[2]]);
let posval1 = boxes[pattern[0]].innerText;
let posval2 = boxes[pattern[1]].innerText;
let posval3 = boxes[pattern[2]].innerText;
if (posval1 !== "" && posval2 !== "" && posval3 !== "") {
if (posval1 == posval2 && posval2 == posval3) {
winEvents();
Body.appendChild(
Div
).innerHTML = `<h1>CONGRATULATIONS \n</h1> <h2> ${posval1} Is Winner</h2>`;
newgame.style.display = "unset";
Div.style.display = "unset";
won = true;
boxes.forEach((box) => {
box.disabled = true;
});
}
}
}
};
modeAI();
// function selectmode(){
// if(AI){
// modeAI();
// }
// else{
// modeMutiplayer();
// }
// }