-
Notifications
You must be signed in to change notification settings - Fork 1
/
o.js
364 lines (307 loc) · 9.29 KB
/
o.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
import board from './board.mjs'
import Players from './players.mjs'
var initial_board;
var human;
var ai;
var rdepth;
var maxdepth =-1;
var human_choice;
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]
]
//event handler cells
const cells = document.querySelectorAll('.cell');
//call start game
startGame();
/////timer
const FULL_DASH_ARRAY = 283;
const WARNING_THRESHOLD = 10;
const ALERT_THRESHOLD = 5;
const COLOR_CODES = {
info: {
color: "green"
},
warning: {
color: "orange",
threshold: WARNING_THRESHOLD
},
alert: {
color: "red",
threshold: ALERT_THRESHOLD
}
};
const TIME_LIMIT = 7;
let timePassed = 0;
let timeLeft = TIME_LIMIT;
let timerInterval = null;
let remainingPathColor = COLOR_CODES.info.color;
//event handler :timer
document.getElementById("app").innerHTML = `
<div class="base-timer">
<svg class="base-timer__svg" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<g class="base-timer__circle">
<circle class="base-timer__path-elapsed" cx="50" cy="50" r="45"></circle>
<path
id="base-timer-path-remaining"
stroke-dasharray="283"
class="base-timer__path-remaining ${remainingPathColor}"
d="
M 50, 50
m -45, 0
a 45,45 0 1,0 90,0
a 45,45 0 1,0 -90,0
"
></path>
</g>
</svg>
<span id="base-timer-label" class="base-timer__label">${formatTime(
timeLeft
)}</span>
</div>
`;
function time_up(){
document.getElementById("s_move").style.display="none";
for (var i = 0; i < cells.length; i++) {
cells[i].style.backgroundColor = "green";
cells[i].removeEventListener('click', turnClick, false);
}
document.querySelector(".endgame").style.display = "block";
document.querySelector(".endgame .text").innerText = "GAME OVER";
}
function resetTimer(){
timePassed=0;
timeLeft = TIME_LIMIT;
timerInterval = null;
remainingPathColor = COLOR_CODES.info.color;
}
function stopTimer()
{
clearInterval(timerInterval);
resetTimer();
document.getElementById("app").style.display="none";
}
function onTimesUp() {
clearInterval(timerInterval);
resetTimer();
time_up();
}
function startTimer() {
document.getElementById("s_move").style.display = "none";
document.getElementById("app").style.display="block";
timerInterval = setInterval(() => {
timePassed = timePassed += 1;
timeLeft = TIME_LIMIT - timePassed;
if (timeLeft <= 0) {
onTimesUp();
}
document.getElementById("base-timer-label").innerHTML = formatTime(
timeLeft
);
setCircleDasharray();
setRemainingPathColor(timeLeft);
}, 1000);
}
//second conversion
function formatTime(time) {
const minutes = Math.floor(time / 60);
let seconds = time % 60;
if (seconds < 10) {
seconds = `0${seconds}`;
}
return `${minutes}:${seconds}`;
}
//
function setRemainingPathColor(timeLeft) {
const { alert, warning, info } = COLOR_CODES;
if (timeLeft <= alert.threshold) {
document
.getElementById("base-timer-path-remaining")
.classList.remove(warning.color);
document
.getElementById("base-timer-path-remaining")
.classList.add(alert.color);
} else if (timeLeft <= warning.threshold) {
document
.getElementById("base-timer-path-remaining")
.classList.remove(info.color);
document
.getElementById("base-timer-path-remaining")
.classList.add(warning.color);
}
}
function calculateTimeFraction() {
const rawTimeFraction = timeLeft / TIME_LIMIT;
return rawTimeFraction - (1 / TIME_LIMIT) * (1 - rawTimeFraction);
}
function setCircleDasharray() {
const circleDasharray = `${(
calculateTimeFraction() * FULL_DASH_ARRAY
).toFixed(0)} 283`;
document
.getElementById("base-timer-path-remaining")
.setAttribute("stroke-dasharray", circleDasharray);
}
//MAIN FUNCtion of the game
function startGame() {
document.getElementById("s_move").style.display="none";
document.querySelector('.endgame').style.display = "none";
document.querySelector('.endgame .text').innerText ="";
document.querySelector('.selectSym').style.display = "block";
for (let i = 0; i < cells.length; i++) {
cells[i].innerText = '';
cells[i].style.removeProperty('background-color');
}
}
function gameOver(gameWon) {
document.getElementById("s_move").style.display="none";
clearInterval(timerInterval);
resetTimer();
for (let index of winCombos[gameWon.index]) {
document.getElementById(index).style.backgroundColor =
gameWon.player == human.svar? "blue" : "red";
}
for (var i = 0; i < cells.length; i++) {
cells[i].removeEventListener('click', turnClick, false);
}
declareWinner(gameWon.player == human.svar ? "You win!" : "You lose.");
}
//if user selects O
function symo(){
human=new Players('O');
ai=new Players('X');
let origBoard = Array.from(Array(9).keys());
initial_board=new board(origBoard);
for (let i = 0; i < cells.length; i++) {
cells[i].addEventListener('click', turnClick, false);
}
document.querySelector('.selectSym').style.display = "none";
}
//button event handler
document.getElementById("so").addEventListener('click', symo);
//if user selects X
function symx(){
human=new Players('X');
ai=new Players('O');
let origBoard = Array.from(Array(9).keys());
initial_board=new board(origBoard);
for (let i = 0; i < cells.length; i++) {
cells[i].addEventListener('click', turnClick, false);
}
document.querySelector('.selectSym').style.display = "none";
}
//button event handler
document.getElementById("sx").addEventListener('click', symx);
//replay button event handler
document.getElementById("Replay").addEventListener('click', startGame);
function turnClick(square) {
document.getElementById("s_move").style.display="none";
if (typeof initial_board.state[square.target.id] == 'number') {
stopTimer();
//document.getElementById("s_move").style.display = "none";
turn(square.target.id, human.svar);
if (!initial_board.checkWin(human.svar) &&!checkTie(initial_board)) {
turn(bestSpot(ai.svar), ai.svar);
document.getElementById("s_move").style.display = "block";
document.getElementById("s_move").innerHTML=`<p class="pc" id="oho" >Hooman you better move to <span id="suggest_move_label" class="suggest-move_lable" style="color:blue"> ${human_choice}</span> block if you wanna win </p> `;
human_choice=bestSpot(human.svar);
human_choice=human_choice+1;
document.getElementById("suggest_move_label").innerHTML =human_choice;
//sugest move
}
}
}
//to call the minimax
function bestSpot(player) {
if (player==ai.svar)
{
return minimax(initial_board,ai.svar).index;
}
else if (player==human.svar){
return minimax(initial_board, human.svar).index;
}
}
//MINIMAX WITH ALPHA BETHA PRUNING
function minimax(initial_board, player,depth=0) {
var availSpots = initial_board.emptySquares();
if (initial_board.checkWin( human.svar)||depth==rdepth) {
return {score: -10};
} else if (initial_board.checkWin( ai.svar) || depth==rdepth) {
return {score: 10};
} else if (availSpots.length === 0 ) {
return {score: 0};
}
var moves = [];
for (var i = 0; i < availSpots.length; i++) {
var move = {};
move.index = initial_board.state[availSpots[i]];
initial_board.state[availSpots[i]] = player;
if (player == ai.svar) {
var result = minimax(initial_board, human.svar,depth+1);
move.score = result.score;
} else {
var result = minimax(initial_board, ai.svar,depth+1);
move.score = result.score;
}
initial_board.state[availSpots[i]] = move.index;
moves.push(move);
}
var bestMove;
if(player === ai.svar) {
var bestScore = -Infinity;
for(var i = 0; i < moves.length; i++) {
if (moves[i].score > bestScore) {
bestScore = moves[i].score;
bestMove = i;
}
}
} else {
var bestScore = Infinity;
for(var i = 0; i < moves.length; i++) {
if (moves[i].score < bestScore) {
bestScore = moves[i].score;
bestMove = i;
}
}
}
return moves[bestMove];
}
function turn(squareId, player) {
initial_board.state[squareId] = player;
document.getElementById(squareId).innerText = player;
let gameWon =initial_board.checkWin(player);
//comment
if (gameWon) {document.getElementById("s_move").style.display="none";
gameOver(gameWon)}
else if(player==ai.svar){
startTimer();
}
else if (player==human.svar){
stopTimer();
}
}
function declareWinner(who) {
document.getElementById("s_move").style.display="none";
document.querySelector(".endgame").style.display = "block";
document.querySelector(".endgame .text").innerText = who;
}
function checkTie(board) {
if (board.emptySquares().length == 0) {
document.getElementById("s_move").style.display="none";
clearInterval(timerInterval);
resetTimer();
for (var i = 0; i < cells.length; i++) {
cells[i].style.backgroundColor = "green";
cells[i].removeEventListener('click', turnClick, false);
}
declareWinner("Tie Game!");
return true;
}
return false;
}