-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
608 lines (539 loc) · 19.2 KB
/
script.js
File metadata and controls
608 lines (539 loc) · 19.2 KB
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
const cells = document.querySelectorAll('[data-cell]');
const board = document.getElementById('game-board');
const gameOverMessage = document.getElementById('game-over-message');
const restartButton = document.getElementById('restart-button');
const undoButton = document.getElementById('undo-button');
const playerScoreDisplay = document.getElementById('player-score');
const computerScoreDisplay = document.getElementById('computer-score');
const swipeHint = document.getElementById('swipe-hint');
const HUMAN_PLAYER = 'x';
const COMPUTER_PLAYER = 'o';
let currentPlayer;
let gameActive = true;
let boardState = ['', '', '', '', '', '', '', '', ''];
let playerScore = 0;
let computerScore = 0;
let playerStarts = true;
let moveHistory = []; // For undo functionality
let touchStartX = 0;
let touchStartY = 0;
let minSwipeDistance = 50; // Minimum distance for swipe gesture
// Initialize AudioContext - will be created after user interaction
let audioContext = null;
// Function to initialize/resume audio context
const initAudioContext = () => {
if (!audioContext) {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
}
if (audioContext.state === 'suspended') {
audioContext.resume();
}
return audioContext;
};
// Add multiple event listeners to resume audio context (required by browsers)
document.addEventListener('click', initAudioContext, { once: false });
document.addEventListener('touchstart', initAudioContext, { once: false });
document.addEventListener('keydown', initAudioContext, { once: false });
// Haptic feedback function
const hapticFeedback = (type = 'light') => {
if ('vibrate' in navigator) {
const patterns = {
light: 10,
medium: 20,
heavy: 30,
double: [10, 50, 10],
success: [10, 50, 10, 50, 20],
error: [20, 50, 20]
};
navigator.vibrate(patterns[type] || patterns.light);
}
};
// Touch gesture handlers for swipe to undo
let isSwipeGesture = false;
const handleTouchStart = (e) => {
// Track touch start for swipe detection
if (gameActive && currentPlayer === HUMAN_PLAYER && moveHistory.length > 0) {
const touch = e.touches[0];
touchStartX = touch.clientX;
touchStartY = touch.clientY;
isSwipeGesture = false;
}
};
const handleTouchMove = (e) => {
// Detect if user is swiping (moving finger)
if (touchStartX !== 0 && touchStartY !== 0) {
const touch = e.touches[0];
const deltaX = Math.abs(touch.clientX - touchStartX);
const deltaY = Math.abs(touch.clientY - touchStartY);
// If moved significantly, it's a swipe gesture
if (deltaX > 10 || deltaY > 10) {
isSwipeGesture = true;
}
}
};
const handleTouchEnd = (e) => {
if (gameActive && currentPlayer === HUMAN_PLAYER && moveHistory.length > 0 && isSwipeGesture) {
const touch = e.changedTouches[0];
const touchEndX = touch.clientX;
const touchEndY = touch.clientY;
const deltaX = touchEndX - touchStartX;
const deltaY = touchEndY - touchStartY;
const absDeltaX = Math.abs(deltaX);
const absDeltaY = Math.abs(deltaY);
// Check for horizontal swipe (left or right)
if (absDeltaX > minSwipeDistance && absDeltaX > absDeltaY) {
// Swipe left or right to undo
e.preventDefault();
hapticFeedback('medium');
undoLastMove();
}
}
// Reset touch tracking
touchStartX = 0;
touchStartY = 0;
isSwipeGesture = false;
};
// Undo last move function
const undoLastMove = () => {
if (moveHistory.length === 0 || !gameActive || currentPlayer === COMPUTER_PLAYER) {
return;
}
// Only allow undo for player's last move if it's their turn
const lastMove = moveHistory[moveHistory.length - 1];
if (lastMove.player === HUMAN_PLAYER && currentPlayer === HUMAN_PLAYER) {
// Remove the last move
boardState[lastMove.index] = '';
cells[lastMove.index].classList.remove(HUMAN_PLAYER);
moveHistory.pop();
// If there was a computer move before that, undo it too
if (moveHistory.length > 0 && moveHistory[moveHistory.length - 1].player === COMPUTER_PLAYER) {
const computerMoveObj = moveHistory.pop();
boardState[computerMoveObj.index] = '';
cells[computerMoveObj.index].classList.remove(COMPUTER_PLAYER);
currentPlayer = HUMAN_PLAYER; // Switch back to player
}
hapticFeedback('success');
playSound('restart');
updateUndoButton();
}
};
// Add touch event listeners to the game board for swipe gestures
const gameBoard = document.getElementById('game-board');
gameBoard.addEventListener('touchstart', handleTouchStart, { passive: true });
gameBoard.addEventListener('touchmove', handleTouchMove, { passive: true });
gameBoard.addEventListener('touchend', handleTouchEnd, { passive: false });
// Create longer, more arcade-style sound effects
const playSound = (type) => {
// Initialize audio context if needed
const ctx = initAudioContext();
if (!ctx || ctx.state === 'suspended') {
// Try to resume if suspended
if (ctx && ctx.state === 'suspended') {
ctx.resume().catch(() => {
// If resume fails, audio won't play - that's okay
return;
});
}
return;
}
const now = ctx.currentTime;
const duration = type === 'win' ? 1.2 : type === 'lose' ? 1.0 : type === 'draw' ? 0.8 : 0.3;
switch (type) {
case 'move':
// Bouncy arcade beep for moves
const osc1 = ctx.createOscillator();
const gain1 = ctx.createGain();
osc1.type = 'square';
osc1.frequency.setValueAtTime(400, now);
osc1.frequency.exponentialRampToValueAtTime(600, now + 0.15);
osc1.frequency.exponentialRampToValueAtTime(300, now + 0.3);
gain1.gain.setValueAtTime(0.3, now);
gain1.gain.exponentialRampToValueAtTime(0.01, now + duration);
osc1.connect(gain1);
gain1.connect(ctx.destination);
osc1.start(now);
osc1.stop(now + duration);
break;
case 'win':
// Victory fanfare - ascending chord sequence
const notes = [523.25, 659.25, 783.99, 1046.50]; // C5, E5, G5, C6
notes.forEach((freq, i) => {
const osc = ctx.createOscillator();
const gain = ctx.createGain();
osc.type = 'sine';
osc.frequency.setValueAtTime(freq, now);
gain.gain.setValueAtTime(0, now + i * 0.15);
gain.gain.linearRampToValueAtTime(0.4, now + i * 0.15 + 0.05);
gain.gain.exponentialRampToValueAtTime(0.01, now + duration - i * 0.1);
osc.connect(gain);
gain.connect(ctx.destination);
osc.start(now + i * 0.15);
osc.stop(now + duration);
});
break;
case 'lose':
// Defeat sound - descending notes
const loseNotes = [523.25, 440, 349.23, 261.63]; // C5, A4, F4, C4
loseNotes.forEach((freq, i) => {
const osc = ctx.createOscillator();
const gain = ctx.createGain();
osc.type = 'sawtooth';
osc.frequency.setValueAtTime(freq, now);
gain.gain.setValueAtTime(0, now + i * 0.2);
gain.gain.linearRampToValueAtTime(0.35, now + i * 0.2 + 0.05);
gain.gain.exponentialRampToValueAtTime(0.01, now + i * 0.2 + 0.25);
osc.connect(gain);
gain.connect(ctx.destination);
osc.start(now + i * 0.2);
osc.stop(now + i * 0.2 + 0.3);
});
break;
case 'draw':
// Neutral draw sound - two tones
[440, 523.25].forEach((freq, i) => {
const osc = ctx.createOscillator();
const gain = ctx.createGain();
osc.type = 'triangle';
osc.frequency.setValueAtTime(freq, now + i * 0.2);
gain.gain.setValueAtTime(0, now + i * 0.2);
gain.gain.linearRampToValueAtTime(0.3, now + i * 0.2 + 0.05);
gain.gain.exponentialRampToValueAtTime(0.01, now + i * 0.2 + 0.35);
osc.connect(gain);
gain.connect(ctx.destination);
osc.start(now + i * 0.2);
osc.stop(now + i * 0.2 + 0.4);
});
break;
case 'restart':
// Quick restart beep
const osc2 = ctx.createOscillator();
const gain2 = ctx.createGain();
osc2.type = 'square';
osc2.frequency.setValueAtTime(600, now);
osc2.frequency.exponentialRampToValueAtTime(800, now + 0.15);
gain2.gain.setValueAtTime(0.25, now);
gain2.gain.exponentialRampToValueAtTime(0.01, now + 0.25);
osc2.connect(gain2);
gain2.connect(audioContext.destination);
osc2.start(now);
osc2.stop(now + 0.25);
break;
}
};
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]
];
const startGame = (isRestart = false) => {
if (isRestart) {
playSound('restart');
hapticFeedback('double');
}
gameActive = true;
boardState = ['', '', '', '', '', '', '', '', ''];
moveHistory = []; // Reset move history
currentPlayer = playerStarts ? HUMAN_PLAYER : COMPUTER_PLAYER;
gameOverMessage.innerText = '';
cells.forEach(cell => {
cell.classList.remove(HUMAN_PLAYER);
cell.classList.remove(COMPUTER_PLAYER);
cell.removeEventListener('click', handleCellClick);
cell.addEventListener('click', handleCellClick, { once: true });
// Add touch events for better mobile support
cell.addEventListener('touchstart', (e) => {
e.preventDefault(); // Prevent double-tap zoom
handleCellClick(e);
}, { passive: false });
});
updateUndoButton();
if (!playerStarts) {
setTimeout(() => {
computerMove();
updateUndoButton();
}, 500);
}
};
const handleCellClick = (e) => {
const cell = e.target.closest('[data-cell]') || e.target;
const index = Array.from(cells).indexOf(cell);
if (boardState[index] !== '' || !gameActive || currentPlayer === COMPUTER_PLAYER) {
return;
}
playSound('move');
hapticFeedback('light');
placeMark(index, HUMAN_PLAYER);
moveHistory.push({ index, player: HUMAN_PLAYER });
updateUndoButton();
if (checkWin(HUMAN_PLAYER)) {
endGame(false);
} else if (isDraw()) {
endGame(true);
} else {
swapTurns();
setTimeout(() => {
computerMove();
updateUndoButton();
}, 500);
}
};
const placeMark = (index, player) => {
boardState[index] = player;
cells[index].classList.add(player);
if (player === COMPUTER_PLAYER) {
moveHistory.push({ index, player: COMPUTER_PLAYER });
}
playSound('move');
};
const swapTurns = () => {
currentPlayer = currentPlayer === HUMAN_PLAYER ? COMPUTER_PLAYER : HUMAN_PLAYER;
};
const checkWin = (player) => {
return winningCombinations.some(combination => {
return combination.every(index => {
return boardState[index] === player;
});
});
};
const isDraw = (board = boardState) => {
return board.every(cell => {
return cell !== '';
});
};
const endGame = (draw) => {
if (draw) {
gameOverMessage.innerText = "It's a Draw!";
playSound('draw');
hapticFeedback('double');
} else {
if (currentPlayer === HUMAN_PLAYER) {
gameOverMessage.innerText = "You Win!";
playerScore++;
playerScoreDisplay.innerText = `Player: ${playerScore}`;
playSound('win');
hapticFeedback('success');
} else {
gameOverMessage.innerText = "Computer Wins!";
computerScore++;
computerScoreDisplay.innerText = `Computer: ${computerScore}`;
playSound('lose');
hapticFeedback('error');
}
}
gameActive = false;
updateUndoButton();
};
const computerMove = () => {
if (!gameActive) return;
let bestScore = -Infinity;
let move = -1;
const availableMoves = [];
// First, collect all available moves
for (let i = 0; i < boardState.length; i++) {
if (boardState[i] === '') {
availableMoves.push(i);
}
}
// If no moves available, return
if (availableMoves.length === 0) return;
// Evaluate each move using minimax
for (let i of availableMoves) {
const newBoard = [...boardState];
newBoard[i] = COMPUTER_PLAYER;
let score = minimax(newBoard, 0, false);
if (score > bestScore) {
bestScore = score;
move = i;
}
}
// Fallback: if move is still -1, use first available move (shouldn't happen with proper minimax)
if (move === -1) {
move = availableMoves[0];
}
placeMark(move, COMPUTER_PLAYER);
if (checkWin(COMPUTER_PLAYER)) {
endGame(false);
} else if (isDraw()) {
endGame(true);
} else {
swapTurns();
}
};
const scores = {
[COMPUTER_PLAYER]: 1,
[HUMAN_PLAYER]: -1,
'tie': 0
};
const minimax = (board, depth, isMaximizing) => {
let result = checkWinner(board);
if (result !== null) {
return scores[result];
}
if (isMaximizing) {
let bestScore = -Infinity;
for (let i = 0; i < board.length; i++) {
if (board[i] === '') {
// Create a new copy of the board for this move
const newBoard = [...board];
newBoard[i] = COMPUTER_PLAYER;
let score = minimax(newBoard, depth + 1, false);
bestScore = Math.max(score, bestScore);
}
}
return bestScore;
} else {
let bestScore = Infinity;
for (let i = 0; i < board.length; i++) {
if (board[i] === '') {
// Create a new copy of the board for this move
const newBoard = [...board];
newBoard[i] = HUMAN_PLAYER;
let score = minimax(newBoard, depth + 1, true);
bestScore = Math.min(score, bestScore);
}
}
return bestScore;
}
};
const checkWinner = (board = boardState) => {
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 (isDraw(board)) {
return 'tie';
}
return null;
}
restartButton.addEventListener('click', () => {
playerStarts = !playerStarts;
startGame(true);
});
// Undo button functionality
undoButton.addEventListener('click', () => {
undoLastMove();
updateUndoButton();
});
// Update undo button state
const updateUndoButton = () => {
if (moveHistory.length > 0 && gameActive && currentPlayer === HUMAN_PLAYER) {
undoButton.disabled = false;
} else {
undoButton.disabled = true;
}
};
// PWA Install Prompt
let deferredPrompt;
const installButton = document.createElement('button');
installButton.id = 'install-button';
installButton.textContent = 'Install App';
installButton.style.cssText = `
margin-top: 10px;
padding: 10px 20px;
font-size: clamp(0.9em, 3vw, 1em);
cursor: pointer;
background-color: #2ed573;
border: none;
color: #282c34;
border-radius: 5px;
transition: background-color 0.3s;
display: none;
`;
installButton.addEventListener('click', async () => {
if (deferredPrompt) {
deferredPrompt.prompt();
const { outcome } = await deferredPrompt.userChoice;
console.log(`User response to install prompt: ${outcome}`);
deferredPrompt = null;
installButton.style.display = 'none';
}
});
document.body.appendChild(installButton);
// Service Worker Registration
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/service-worker.js')
.then((registration) => {
console.log('ServiceWorker registered:', registration);
})
.catch((error) => {
console.log('ServiceWorker registration failed:', error);
});
});
}
// Before Install Prompt event
window.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault();
deferredPrompt = e;
installButton.style.display = 'block';
});
// App Installed event
window.addEventListener('appinstalled', () => {
console.log('PWA was installed');
deferredPrompt = null;
installButton.style.display = 'none';
});
// Push Notification Registration
const registerPushNotifications = async () => {
if ('serviceWorker' in navigator && 'PushManager' in window) {
try {
const registration = await navigator.serviceWorker.ready;
const subscription = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: null // Would need a VAPID key for production
});
console.log('Push subscription:', subscription);
} catch (error) {
console.log('Push registration failed:', error);
}
}
};
// Request notification permission only after user interaction (not automatically)
// This prevents browser dialogs from interfering with the user experience
const requestNotificationPermission = () => {
if ('Notification' in window && Notification.permission === 'default') {
Notification.requestPermission().then((permission) => {
if (permission === 'granted') {
registerPushNotifications();
}
});
}
};
// Only request after first user interaction (click anywhere on the page)
let notificationPermissionRequested = false;
document.addEventListener('click', () => {
if (!notificationPermissionRequested && 'Notification' in window && Notification.permission === 'default') {
notificationPermissionRequested = true;
// Delay the request slightly to avoid interfering with other click handlers
setTimeout(requestNotificationPermission, 1000);
}
}, { once: false });
// Online/Offline event handlers
window.addEventListener('online', () => {
console.log('App is online');
if (gameOverMessage) {
const originalText = gameOverMessage.innerText;
gameOverMessage.innerText = 'Back online!';
setTimeout(() => {
gameOverMessage.innerText = originalText;
}, 2000);
}
});
window.addEventListener('offline', () => {
console.log('App is offline');
if (gameOverMessage) {
const originalText = gameOverMessage.innerText;
gameOverMessage.innerText = 'Playing offline...';
setTimeout(() => {
gameOverMessage.innerText = originalText;
}, 2000);
}
});
startGame();