-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscript.js
421 lines (403 loc) · 14.8 KB
/
script.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
const gameCells = Array.from(document.getElementsByClassName('cell'));
const startPage = document.getElementById('start-page');
const difficulty = document.getElementById('difficulty');
const container = document.getElementById('container');
const gamecontainer = document.getElementById('game-container');
const result = document.getElementById("result").innerHTML;
const Player1 = 'X';
const Player2 = 'O';
let gameOn = true;
let Draw = false;
const moves = [0, 1, 2, 3, 4, 5, 6, 7, 8];
let currentPlayer = Player1;
let typeOfgame;
let gameLevel;
let winnerLine;
let movesLeft = [0, 1, 2, 3, 4, 5, 6, 7, 8];
//function for storing the type of game in a variable typeOfgame
function gametype(id) {
if (id == "btn1") {
typeOfgame = 1;
startPage.style.display = "none";
difficulty.style.display = "block";
}
else {
typeOfgame = 2;
container.style.display = "none";
gamecontainer.style.display = "block";
}
}
//function for storing the game difficulty in a variable gameLevel
function gamedifficulty(id) {
if (id == "btn3")
gameLevel = "easy";
else
gameLevel = "hard";
container.style.display = "none";
gamecontainer.style.display = "block";
}
//Event Handlers
// This function is called when any cell is clicked
const cellClicked = (e) => {
const id = e.target.id;
if (!gameOn)
return;
if (!isNaN(moves[id])) {
moves[id] = currentPlayer;
let pos = movesLeft.indexOf(Number(id));
movesLeft.splice(pos, 1);
e.target.innerText = currentPlayer;
if (currentPlayer == Player1) {
e.target.style.color = "#F83157";
e.target.style.textShadow = "3px 3px #CE2948";
if (isPlayerWon(currentPlayer)) {
setTimeout(()=>displayResult(Player1, "won"),1000);
document.getElementById("status").style.visibility = "hidden";
gameOn = false;
return;
}
if (checkDraw()) {
setTimeout(()=>displayResult(player, "draw"),1000);
document.getElementById("status").style.visibility = "hidden";
gameOn = false;
return;
}
document.getElementById("player").innerHTML = Player2;
document.getElementById("player").style.textShadow = "1.5px 1.5px #318DB5";
document.getElementById("player").style.color = "#3BA8D7";
currentPlayer = Player2;
}
else {
e.target.style.color = "#3BA8D7";
e.target.style.textShadow = "3px 3px #318DB5";
if (isPlayerWon(currentPlayer)) {
setTimeout(()=>displayResult(Player2, "won"),1000);
document.getElementById("status").style.visibility = "hidden";
gameOn = false;
return;
}
if (checkDraw()) {
setTimeout(()=>displayResult(player, "draw"),1000);
document.getElementById("status").style.visibility = "hidden";
gameOn = false;
return;
}
document.getElementById("player").innerHTML = Player1;
document.getElementById("player").style.textShadow = "1.5px 1.5px #CE2948";
document.getElementById("player").style.color = "#F83157";
currentPlayer = Player1;
}
if (typeOfgame == 1 && gameLevel == 'easy') {
setTimeout(() => computerTurn(), 500);
}
if (typeOfgame == 1 && gameLevel == 'hard') {
setTimeout(() => aiTurn(), 500);
}
}
}
//Event listener
gameCells.forEach((cell, index) => {
cell.addEventListener("click", cellClicked);
})
//This fuction checks whether the current player is winner or not
let x=window.matchMedia("(max-width: 670px)")
function isPlayerWon (currentPlayer){
//Horizontal
if (moves[0] == moves[1] && moves[1] == moves[2] && moves[2] == currentPlayer) {
if(!(x.matches))
document.getElementById("l012").style.visibility = 'visible';
winnerLine = 'l012';
return true;
}
else if (moves[3] == moves[4] && moves[4] == moves[5] && moves[5] == currentPlayer) {
if(!(x.matches))
document.getElementById("l345").style.visibility = 'visible';
winnerLine = 'l345';
return true;
}
else if (moves[6] == moves[7] && moves[7] == moves[8] && moves[8] == currentPlayer) {
if(!(x.matches))
document.getElementById("l678").style.visibility = 'visible';
winnerLine = 'l678';
return true;
}
//Vertical
else if (moves[0] == moves[3] && moves[3] == moves[6] && moves[6] == currentPlayer) {
if(!(x.matches))
document.getElementById("l036").style.visibility = 'visible';
winnerLine = 'l036';
return true;
}
else if (moves[1] == moves[4] && moves[4] == moves[7] && moves[7] == currentPlayer) {
if(!(x.matches))
document.getElementById("l147").style.visibility = 'visible';
winnerLine = 'l147';
return true;
}
else if (moves[2] == moves[5] && moves[5] == moves[8] && moves[8] == currentPlayer) {
if(!(x.matches))
document.getElementById("l258").style.visibility = 'visible';
winnerLine = 'l258';
return true;
}
//Diagonals
else if (moves[0] == moves[4] && moves[4] == moves[8] && moves[8] == currentPlayer) {
if(!(x.matches))
document.getElementById("l048").style.visibility = 'visible';
winnerLine = 'l048';
return true;
}
else if (moves[2] == moves[4] && moves[4] == moves[6] && moves[6] == currentPlayer) {
if(!(x.matches))
document.getElementById("l246").style.visibility = 'visible';
winnerLine = 'l246';
return true;
}
else
return false;
}
let i, draw;
//This function is used to check if the game is draw
function checkDraw() {
if(movesLeft.length==0)
{
Draw=true;
return true;
}
else
return false;
}
// this function displays the result
function displayResult(player, status) {
document.getElementById("displayresult").style.display = "flex";
gamecontainer.style.opacity = '0.5';
if (status == "won") {
document.getElementById("winner").innerText = player;
if (player == 'X') {
if(typeOfgame==1)
document.getElementById("result").innerText = "You Won the Game!";
else{
document.getElementById("winner").style.textShadow = "1.5px 1.5px #CE2948";
document.getElementById("winner").style.color = "#F83157";
}
}
else {
if(typeOfgame==1)
document.getElementById("result").innerText = "You Lost the Game!";
else{
document.getElementById("winner").style.textShadow = "1.5px 1.5px #318DB5";
document.getElementById("winner").style.color = "#3BA8D7";
}
}
}
else {
document.getElementById("result").innerText = "Game Draw!";
}
}
function finalResult(id) {
if (id == "btn4") {
currentPlayer = Player1;
container.style.display = "flex";
gamecontainer.style.display = "none";
gamecontainer.style.opacity = '1';
if (typeOfgame == 1) {
difficulty.style.display = "none";
startPage.style.display = 'block';
}
document.getElementById("displayresult").style.display = "none";
if (Draw == false)
document.getElementById(winnerLine).style.visibility = 'hidden';
document.getElementById("result").innerHTML = result;
document.getElementById("status").style.visibility = "visible";
document.getElementById("player").style.color = "#F83157";
document.getElementById("player").style.textShadow = "1.5px 1.5px #CE2948";
document.getElementById("player").innerHTML = Player1;
gameOn = true;
Draw = false;
for (i = 0; i < 9; i++) {
moves[i] = i;
}
for (i = 0; i < 9; i++) {
document.getElementById(i).innerText = "";
}
for (i = 0; i < 9; i++) {
movesLeft[i] = i;
}
}
else {
currentPlayer = Player1;
gamecontainer.style.opacity = '1';
document.getElementById("displayresult").style.display = "none";
document.getElementById("status").style.visibility = "visible";
document.getElementById("player").style.color = "#F83157";
document.getElementById("player").style.textShadow = "1.5px 1.5px #CE2948";
document.getElementById("player").innerHTML = Player1;
if (Draw == false)
document.getElementById(winnerLine).style.visibility = 'hidden';
document.getElementById("result").innerHTML = result;
gameOn = true;
Draw = false;
for (i = 0; i < 9; i++) {
moves[i] = i;
}
for (i = 0; i < 9; i++) {
document.getElementById(i).innerText = "";
}
for (i = 0; i < 9; i++) {
movesLeft[i] = i;
}
}
}
//code for easy AI , It choses random position from the available position, if there are any available position by which AI can win then
//the ai choses that position
let turn;
let position;
function canWin(movesLeft){
let board=[...moves];
for(position=0;position<movesLeft.length;position++)
{
turn=movesLeft[position];
board[turn]=Player2;
if(winning(board,Player2))
return true;
else
{
board=[...moves];
}
}
}
function computerTurn() {
let pos;
let cell;
if(canWin(movesLeft))
{
pos=turn;
cell = document.getElementById(moves[pos]);
moves[pos] = currentPlayer;
movesLeft.splice(position, 1);
}
else
{
pos = Math.floor(Math.random() * movesLeft.length);
cell = document.getElementById(movesLeft[pos]);
moves[movesLeft[pos]] = currentPlayer;
movesLeft.splice(pos, 1);
}
cell.innerText = currentPlayer;
cell.style.color = "#3BA8D7";
cell.style.textShadow = "3px 3px #318DB5";
if (isPlayerWon(currentPlayer)) {
setTimeout(()=>displayResult(Player2, "won"),1000);
document.getElementById("status").style.visibility = "hidden";
gameOn = false;
return;
}
document.getElementById("player").innerHTML = Player1;
document.getElementById("player").style.textShadow = "1.5px 1.5px #CE2948";
document.getElementById("player").style.color = "#F83157";
currentPlayer = Player1;
}
// code for Hard Level AI , this is an advance AI that searches for all the position and then chooses a best move
let a=0;
function aiTurn(){
let bestMove=minimax(moves,Player2);
pos=bestMove.index;
moves[pos]= currentPlayer;
let pos1;
pos1=movesLeft.indexOf(pos)
movesLeft.splice(pos1, 1);
let cell = document.getElementById(pos);
cell.innerText = currentPlayer;
cell.style.color = "#3BA8D7";
cell.style.textShadow = "3px 3px #318DB5";
if (isPlayerWon(currentPlayer)) {
setTimeout(()=>displayResult(Player2, "won"),1000);
document.getElementById("status").style.visibility = "hidden";
gameOn = false;
return;
}
document.getElementById("player").innerHTML = Player1;
document.getElementById("player").style.textShadow = "1.5px 1.5px #CE2948";
document.getElementById("player").style.color = "#F83157";
currentPlayer = Player1;
}
function minimax(newBoard, player){
//available spots
var availSpots = emptyIndexies(newBoard);
// checks for the terminal states such as win, lose, and tie and returning a value accordingly
if (winning(newBoard, Player1)){
return {score:-10};
}
else if (winning(newBoard, Player2)){
return {score:10};
}
else if (availSpots.length === 0){
return {score:0};
}
// an array to collect all the objects
var gamemoves = [];
// loop through available spots
for (var i = 0; i < availSpots.length; i++){
//create an object for each and store the index of that spot that was stored as a number in the object's index key
var move = {};
move.index = newBoard[availSpots[i]];
// set the empty spot to the current player
newBoard[availSpots[i]] = player;
//if collect the score resulted from calling minimax on the opponent of the current player
if (player == Player2){
var result = minimax(newBoard, Player1);
move.score = result.score;
}
else{
var result = minimax(newBoard, Player2);
move.score = result.score;
}
//reset the spot to empty
newBoard[availSpots[i]] = move.index;
// push the object to the array
gamemoves.push(move);
}
// if it is the computer's turn loop over the gamemoves and choose the move with the highest score
var bestMove;
if(player === Player2){
var bestScore = -10000;
for(var i = 0; i < gamemoves.length; i++){
if(gamemoves[i].score > bestScore){
bestScore = gamemoves[i].score;
bestMove = i;
}
}
}else{
// else loop over the gamemoves and choose the move with the lowest score
var bestScore = 10000;
for(var i = 0; i < gamemoves.length; i++){
if(gamemoves[i].score < bestScore){
bestScore = gamemoves[i].score;
bestMove = i;
}
}
}
// return the chosen move (object) from the array to the higher depth
return gamemoves[bestMove];
}
// returns the available spots on the board
function emptyIndexies(board){
return board.filter(s => s != "O" && s != "X");
}
// winning combinations using the board indexies for instace the first win could be 3 xes in a row
function winning(board, player){
if (
(board[0] == player && board[1] == player && board[2] == player) ||
(board[3] == player && board[4] == player && board[5] == player) ||
(board[6] == player && board[7] == player && board[8] == player) ||
(board[0] == player && board[3] == player && board[6] == player) ||
(board[1] == player && board[4] == player && board[7] == player) ||
(board[2] == player && board[5] == player && board[8] == player) ||
(board[0] == player && board[4] == player && board[8] == player) ||
(board[2] == player && board[4] == player && board[6] == player)
) {
return true;
} else {
return false;
}
}