-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameAlgoTTT.js
executable file
·321 lines (272 loc) · 10.4 KB
/
GameAlgoTTT.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*variables stored for games data*/
var plot;
var bigsquareConcluded;
var numOfBSConcluded;
var previousBSID;
var movesPlayed;
var Human = "X";
var Bot = "O";
var Player = "X";
const winCombos = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[6, 4, 2]
]
/*Selecting all tiles elements in a array */
startGame();
/*this variables are elements in array form for tiles and bigsquares. tiles being 0 to 80 indexed and bigsquares being 0 to 8 indexed*/
var tiles;
var BIGsquares;
function startGame() {
/*giving values to some variables such as Player and putting tiles and bigsquares elements to further refrence them. */
Player = "X";
BIGsquares = document.querySelectorAll('.MS');
tiles = document.querySelectorAll('.Tile');
/*variable to store no. of moves so that check function only checks after certain move */
movesPlayed = 0;
/*clears the previous game conlusion display or hides it*/
document.querySelectorAll(".Conclusion")[0].style.display = "none";
/*giving plot 81 values to store*/
plot = [];
for(var i=0; i<81; ++i) plot.push(null);
/*the bigsqaures which are concluded like x wins or o wins or tie is stored in this 9 size array*/
bigsquareConcluded = [];
for(var i=0; i<9; ++i) {
bigsquareConcluded.push(null);
BIGsquares[i].style.backgroundColor = "grey";
}
/*preparing the tiles for the game */
for (var j = 0; j < tiles.length; j++) {
/*Clearing all x and o inside p element inside tiles 1 by 1 through looping */
tiles[j].children[0].innerText = '';
/*adding click event to all tiles in start of game */
tiles[j].addEventListener('click', clickEvent, false);
tiles[j].addEventListener('mouseover', mouseEvent1, false);
tiles[j].addEventListener('mouseout',mouseEvent2,false);
tiles[j].style.cursor = "pointer";
tiles[j].style.backgroundColor = "#ffd304";
}
}
/*everytime a tile is clicked this function gets executed*/
function clickEvent(tile) {
/*updates the moves played each time tile is clicked */
movesPlayed = movesPlayed + 1;
/*gets the first class name of the tile clicked for example S1 */
var tileID = tile.target.classList.item(0);
/* separates tileID string s so that only index is left*/
var extracted_tileID = tileID.replace("S","");
/*saving the move played in the plot variable for further functional operations*/
plot[extracted_tileID] = Human;
console.log(extracted_tileID+"Human")
/*selects the tile clicked and pastes x or o depending on the turn in the corresponding tile clicked*/
var x = document.querySelectorAll('.'+tileID)[0];
x.children[0].innerText = Human;
x.style.backgroundColor = (Human == "X")? "#913ac5": "#48845e";
/*Check Functions for tiles wins or tie...first checks for win if not then checks for tie and stores the return values in temp1*/
var temp1 = (checkWinTiles(Human,extracted_tileID,plot)[0]!=null)? checkWinTiles(Human,extracted_tileID,plot): checkTieTiles(Human,extracted_tileID,plot);
/*if temp1 has value that is not null means its a win or tie*/
if (temp1[1] != null) {
/*stores the conclusion in this variable for further functional operation */
bigsquareConcluded[temp1[1]] = temp1[0];
/*highlights the BS with the corresponding color for x or o or t means tie*/
BIGsquares[temp1[1]].style.backgroundColor = (temp1[0] == "X")? "#0000d7" : (temp1[0] == "O")? "#4ad522" : "#ff0080";
}
/*TO CHECK FOR PLOT WIN temp1 is used again to store the data temporaily*/
temp1 = (checkWinBS(Human)[0]!=null)? checkWinBS(Human): checkTieBS(Human);
if (temp1[1] != null) {
/* breaks the script and exits out after game over*/
gameOver(temp1);
deactivateAll();
return 1;
}
deactivateAll();
/*puts x or o inside p element inside tile based on player turn */
var botID = botMove(extracted_tileID);
console.log(botID);
var x = document.querySelectorAll('.S'+botID)[0];
x.children[0].innerText = Bot;
x.style.backgroundColor = (Bot == "X")? "#913ac5": "#48845e";
var temp2 = (checkWinTiles(Bot,botID,plot)[0]!=null)? checkWinTiles(Bot,botID,plot): checkTieTiles(Bot,botID,plot);
/*if temp1 has value that is not null means its a win or tie*/
if (temp2[1] != null) {
/*stores the conclusion in this variable for further functional operation */
bigsquareConcluded[temp2[1]] = temp2[0];
/*highlights the BS with the corresponding color for x or o or t means tie*/
BIGsquares[temp2[1]].style.backgroundColor = (temp2[0] == "X")? "#0000d7" : (temp2[0] == "O")? "#4ad522" : "#ff0080";
}
/*TO CHECK FOR PLOT WIN temp1 is used again to store the data temporaily*/
temp2 = (checkWinBS(Bot)[0]!=null)? checkWinBS(Bot): checkTieBS(Bot);
if (temp2[1] != null) {
/* breaks the script and exits out after game over*/
gameOver(temp2);
deactivateAll();
return 1;
}
activate(botID);
/*variable to show whose it is...once bot is added it will get deleted*/
}
/*deactivates all the tiles once called or invoked*/
function deactivateAll() {
for (var j = 0; j < tiles.length; j++) {
tiles[j].removeEventListener('click', clickEvent);
tiles[j].removeEventListener('mouseover', mouseEvent1);
tiles[j].removeEventListener('mouseout', mouseEvent2);
tiles[j].style.cursor = "default";
if(plot[j] == null){
tiles[j].style.backgroundColor = "White";
}
}
}
/*activates the tiles based on the move played if the BS is concluded it calls activateALL function*/
function activate(tileID) {
var BigsquareID = tileID%9;
var BSindexID = BigsquareID*9;
var Activate_BS = document.querySelectorAll('#M'+BigsquareID)[0];
if (bigsquareConcluded[BigsquareID] == null) {
for (j = 0; j < 9; j++) {
if (plot[BSindexID+j] == null) {
Activate_BS.children[j].addEventListener ('click', clickEvent, false);
Activate_BS.children[j].style.cursor = "pointer";
Activate_BS.children[j].style.backgroundColor = "#ffd304";
Activate_BS.children[j].addEventListener('mouseover', mouseEvent1,false);
Activate_BS.children[j].addEventListener('mouseout', mouseEvent2,false);
}
}
}
else if (bigsquareConcluded[BigsquareID] != null) {
activateALL(BSindexID);
}
}
/* checks the tiles won using the wincombos and plot variables looping through each BS*/
function checkWinTiles (player,tileID,newPlot) {
if (movesPlayed >= 5){
var instancesOfPlayer = 0;
for (j = 0;j < 9;j++){
if(newPlot[parseInt(tileID/9)*9+j] == player){
instancesOfPlayer++;
}
}
if (instancesOfPlayer>=3) {
for (i = 0; i < 8;i++) {
if (newPlot[parseInt(tileID/9)*9+winCombos[i][0]] == player
&& newPlot[parseInt(tileID/9)*9+winCombos[i][1]] == player
&& newPlot[parseInt(tileID/9)*9+winCombos[i][2]] == player) {
console.log (player+" wins in "+parseInt(tileID/9));
numOfBSConcluded++;
return [player, parseInt(tileID/9)];
}
}
}
}
return [null,null];
}
/*check for ties if any tile is left empty then it returns null otherwise returns 2 size array to temp1 */
function checkTieTiles (player,tileID,newPlot) {
if (bigsquareConcluded[parseInt(tileID/9)] == null){
for (i = 0; i < 9; i++) {
if (newPlot[parseInt(tileID/9)*9+i] == null){
return[null,null];
}
}
return ["T",parseInt(tileID/9)];
}
}
/* is called when the BS to activate is already concluded so activates the remaining tiles that unfilled*/
function activateALL(tileID) {
for (j = 0; j < 9;j++){
if (bigsquareConcluded[j] == null){
for (i = 0; i < 9;i++){
if (plot[j*9+i] == null){
tiles[j*9+i].addEventListener('click', clickEvent, false);
tiles[j*9+i].style.backgroundColor = "#ffd304";
tiles[j*9+i].style.cursor = "pointer";
tiles[j*9+i].addEventListener('mouseover', mouseEvent1,false);
tiles[j*9+i].addEventListener('mouseout', mouseEvent2,false);
}
}
}
}
}
/*checks wins using wincombo and bigsquareconcluded variable through looping technique*/
function checkWinBS (player) {
for (i = 0; i < 8; i++) {
if (bigsquareConcluded[winCombos[i][0]] == player
&& bigsquareConcluded[winCombos[i][1]] == player
&& bigsquareConcluded[winCombos[i][2]] == player) {
console.log("game over "+player+" wins");
return [player,player];
}
}
return [null,null];
}
/* checks whether bigsquareconcluded is filled if yes returns tie*/
function checkTieBS(player) {
for (i = 0; i < 8; i++) {
if (bigsquareConcluded[i] == null) {
return[null,null];
}
}
return["T",player];
}
/* after conclusion in BS check functions this function is called for end game prompts and displays*/
function gameOver(conclusion) {
if(conclusion[0] == "T"){
document.querySelectorAll(".Conclusion")[0].style.display = "block";
document.querySelectorAll(".Conclusion")[0].children[0].innerText = "Its A Tie";
}
else{
document.querySelectorAll(".Conclusion")[0].style.display = "block";
document.querySelectorAll(".Conclusion")[0].children[0].innerText = conclusion[0] + " Wins";
document.querySelectorAll(".Conclusion")[0].children[0].style.color = (conclusion[0]=="X")? "Cyan" :"#4ad522";
}
}
/*functions for hover state of tiles*/
function mouseEvent1(tile) {
document.querySelectorAll('.'+tile.target.classList.item(0))[0].style.backgroundColor = "#e1ba00";
}
function mouseEvent2(tile) {
document.querySelectorAll('.'+tile.target.classList.item(0))[0].style.backgroundColor = "#ffd304";
}
/*bot functions go here */
function botMove(tileID) {
if (movesPlayed >= 100){
return minimax(plot,Bot,movesPlayed);
}
else {
movesPlayed = movesPlayed + 1;
return randomPlay(tileID);
}
}
function minimax(plot,player,depth){
}
function randomPlay(tileID) {
if (bigsquareConcluded[tileID%9] == null){
var Valid_tiles = [];
for (j = 0; j < 9;j++) {
if (plot[9*(tileID%9)+j] == null) {
Valid_tiles.push(9*(tileID%9)+j);
}
}
randomMove = Valid_tiles[Math.floor(Math.random()*(Valid_tiles.length-1))];
plot[randomMove] = "O";
tiles[randomMove].children[0].innerText = "O";
return randomMove;
}
else {
var Valid_tiles = [];
for (j = 0; j < 81;j++) {
if (plot[j] == null && bigsquareConcluded[parseInt(j/9)] == null) {
Valid_tiles.push(j);
}
}
randomMove = Valid_tiles[Math.floor(Math.random()*(Valid_tiles.length-1))];
plot[randomMove] = "O";
tiles[randomMove].children[0].innerText = "O";
return randomMove;
}
}