-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
458 lines (364 loc) · 15.2 KB
/
index.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
const board = document.getElementById("board");
const cells = [];
let currentPlayer = "";
let playerSymbol = "X";
let computerSymbol = "O";
let gameActive = false;
let playerName = "";
function startGame() {
gameActive = true;
if (Math.random() < 0.5) {
currentPlayer = "Your";
} else {
currentPlayer = "Raj's";
setTimeout(computerMove, 500);
}
document.getElementById("turn-info").textContent = `${currentPlayer} turn (you are "${playerSymbol}")`;
cells.forEach((cell) => (cell.textContent = ""));
}
for (let i = 0; i < 9; i++) {
const cell = document.createElement("div");
cell.className = "cell";
cells.push(cell);
board.appendChild(cell);
cell.addEventListener("click", () => {
if (cell.textContent === "" && currentPlayer === "Your" && gameActive) {
cell.textContent = playerSymbol;
if (checkWinner(playerSymbol)) {
alert("Your");
resetBoard();
} else if (cells.every((cell) => cell.textContent !== "")) {
alert("It's a draw!");
resetBoard();
} else {
currentPlayer = "Raj's";
document.getElementById("turn-info").textContent = `${currentPlayer} turn`;
setTimeout(computerMove, 500);
}
}
});
}
function checkWinner(player) {
const winningCombinations = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
return winningCombinations.some((combination) =>
combination.every((index) => cells[index].textContent === player)
);
}
function computerMove() {
if (!gameActive) return;
const emptyCells = cells.filter((cell) => cell.textContent === "");
if (emptyCells.length > 0) {
const bestMove = findBestMove();
cells[bestMove].textContent = computerSymbol;
if (checkWinner(computerSymbol)) {
alert("Raj Won");
resetBoard();
} else if (cells.every((cell) => cell.textContent !== "")) {
alert("It's a draw!");
resetBoard();
}
}
currentPlayer = "Your";
document.getElementById("turn-info").textContent = `${currentPlayer} turn (you are "${playerSymbol}")`;
}
function resetBoard() {
gameActive = false;
cells.forEach((cell) => (cell.textContent = ""));
currentPlayer = "Your";
document.getElementById("turn-info").textContent = `${currentPlayer} turn (you are "${playerSymbol}")`;
}
function minimax(board, depth, isMaximizing) {
const scores = {
X: -1,
O: 1,
draw: 0,
};
const result = checkWin(board);
if (result !== null) {
return scores[result];
}
if (isMaximizing) {
let bestScore = -Infinity;
for (let i = 0; i < 9; i++) {
if (board[i] === "") {
board[i] = computerSymbol;
const score = minimax(board, depth + 1, false);
board[i] = "";
bestScore = Math.max(score, bestScore);
}
}
return bestScore;
} else {
let bestScore = Infinity;
for (let i = 0; i < 9; i++) {
if (board[i] === "") {
board[i] = playerSymbol;
const score = minimax(board, depth + 1, true);
board[i] = "";
bestScore = Math.min(score, bestScore);
}
}
return bestScore;
}
}
function findBestMove() {
let bestScore = -Infinity;
let bestMove = -1;
for (let i = 0; i < 9; i++) {
if (cells[i].textContent === "") {
cells[i].textContent = computerSymbol;
const score = minimax(cells.map((cell) => cell.textContent), 0, false);
cells[i].textContent = "";
if (score > bestScore) {
bestScore = score;
bestMove = i;
}
}
}
return bestMove;
}
function checkWin(board) {
const winningCombinations = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
for (const combination of winningCombinations) {
const [a, b, c] = combination;
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
return board[a];
}
}
if (board.every((cell) => cell !== "")) {
return "draw";
}
return null;
}
// function slider(input) {
// // const item = document.querySelector(`#${input}`);
// // const slideArr = document.querySelectorAll(".slides");
// // const slideArr = [...document.querySelector(".slides").children];
// // slideArr.forEach((element) => element.classList.remove("active"));
// // item.classList.add("active");
// const item = document.querySelector(`#${input}`);
// const slideArr = document.querySelectorAll(".slides");
// if (item && slideArr) {
// slideArr.forEach((element) => element.classList.remove("active"));
// item.classList.add("active");
// } else {
// console.error("Slider elements not found");
// }
// }
// let slideIndex = 1;
// setInterval(() => {
// slideIndex++;
// if (slideIndex === 7)
// slideIndex = 1;
// slider(`slide${slideIndex}`);
// }, 2000);
// document.addEventListener("DOMContentLoaded", function () {
// var container = document.querySelector(".container");
// var scrollThreshold = container.offsetTop;
// window.addEventListener("scroll", function () {
// if (window.pageYOffset > scrollThreshold) {
// container.classList.add("fixed");
// } else {
// container.classList.remove("fixed");
// }
// });
// });
// document.addEventListener("DOMContentLoaded", function () {
// const textElements = [
// "Text for Slide 1",
// "Text for Slide 2",
// "Text for Slide 3",
// "Text for Slide 4",
// "Text for Slide 5",
// "Text for Slide 6",
// ];
// let currentSlideIndex = 0;
// const textContainer = document.getElementById("text-container");
// function typeWriter() {
// if (currentSlideIndex < textElements.length) {
// const currentText = textElements[currentSlideIndex];
// const currentSlide = document.getElementById(`slide${currentSlideIndex + 1}`);
// const overlay = currentSlide.querySelector(".overlay div");
// let charIndex = 0;
// function type() {
// if (charIndex < currentText.length) {
// overlay.textContent += currentText.charAt(charIndex);
// charIndex++;
// setTimeout(type, 50);
// } else {
// setTimeout(erase, 1000);
// }
// }
// function erase() {
// if (charIndex > 0) {
// overlay.textContent = currentText.substring(0, charIndex - 1);
// charIndex--;
// setTimeout(erase, 30);
// } else {
// currentSlideIndex++;
// setTimeout(typeWriter, 500);
// }
// }
// type();
// }
// }
// typeWriter();
// });
document.addEventListener("DOMContentLoaded", function () {
var homeSection = document.getElementById("home");
var observer = new IntersectionObserver(function (entries) {
if (entries[0].isIntersecting && !homeSection.classList.contains("active")) {
homeSection.classList.add("active");
}
}, { threshold: 0 });
observer.observe(homeSection);
});
document.addEventListener('DOMContentLoaded', function () {
// Select the target node
const target = document.querySelector('#projects');
// Set up the Intersection Observer options
const options = {
root: null,
rootMargin: '0px',
threshold: 0.5, // Adjust the threshold as needed
};
// Callback function to handle the intersection changes
const callback = function (entries, observer) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
// If the section is in view, add the "animate" class to each project
document.querySelectorAll('.project').forEach(function (project, index) {
project.classList.add('animate');
});
// Disconnect the observer once animation is triggered
observer.disconnect();
}
});
};
// Create an Intersection Observer
const observer = new IntersectionObserver(callback, options);
// Start observing the target node
observer.observe(target);
});
window.addEventListener('load', function() {
// After the page has fully loaded, show the content
document.body.style.display = 'block';
});
//
const cursor = document.querySelector(".cursor");
const links = document.querySelectorAll("nav ul li a");
const navlinks = document.querySelectorAll("nav ul li");
document.addEventListener("mousemove", (e) => {
let leftPosition = e.pageX + 4;
let topPosition = e.pageY + 4;
cursor.style.left = leftPosition + "px";
cursor.style.top = topPosition + "px";
})
links.forEach(link => {
link.addEventListener("mouseenter", () => {
cursor.classList.add("large");
})
})
links.forEach(link => {
link.addEventListener("mouseleave", () => {
cursor.classList.remove("large");
})
})
// Animation
window.addEventListener('scroll', function() {
var header = document.getElementById('header');
var scrollPosition = window.scrollY;
if (scrollPosition > 0) {
header.classList.add('fixed');
} else {
header.classList.remove('fixed');
}
});
navlinks.forEach((li, i) => {
li.style.animationDelay = 0 + i * 140 + "ms";
})
const elements = document.querySelectorAll('.a1, .a2, .a3, .a4');
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('show');
}
});
});
elements.forEach(element => {
observer.observe(element);
});
(function() {
var follower, init, mouseX, mouseY, positionElement, printout, timer;
follower = document.getElementById('follower');
printout = document.getElementById('printout');
mouseX = (event) => {
return event.clientX;
};
mouseY = (event) => {
return event.clientY;
};
positionElement = (event) => {
var mouse;
mouse = {
x: mouseX(event),
y: mouseY(event)
};
follower.style.top = mouse.y + 'px';
return follower.style.left = mouse.x + 'px';
};
timer = false;
window.onmousemove = init = (event) => {
var _event;
_event = event;
return timer = setTimeout(() => {
return positionElement(_event);
}, 1);
};
}).call(this);
var loadingBar = document.getElementById('loading-bar');
if (loadingBar) {
loadingBar.remove();
}
function isInViewport(element) {
const rect = element.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
// Function to add animation class when section is in viewport
function animateOnScroll() {
const sections = document.querySelectorAll('.animate-on-scroll');
sections.forEach((section) => {
if (isInViewport(section)) {
section.classList.add('animate');
} else {
section.classList.remove('animate');
}
});
}
// Initial check when page loads
window.addEventListener('load', animateOnScroll);
// Check when scrolling
window.addEventListener('scroll', animateOnScroll);