-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
306 lines (252 loc) · 8.67 KB
/
Copy pathscript.js
File metadata and controls
306 lines (252 loc) · 8.67 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
// Game State
let gridSize = 3;
let winCondition = 3;
let board = [];
let currentPlayer = 1;
let gameActive = false;
let scores = { player1: 0, player2: 0 };
let playerNames = { player1: 'Player 1', player2: 'Player 2' };
let winningCells = [];
// DOM Elements
const landingPage = document.getElementById('landing-page');
const gamePage = document.getElementById('game-page');
const rulesModal = document.getElementById('rules-modal');
const finishModal = document.getElementById('finish-modal');
const gameoverModal = document.getElementById('gameover-modal');
const gameGrid = document.getElementById('game-grid');
// Initialize Event Listeners
document.addEventListener('DOMContentLoaded', () => {
initGridSelection();
initButtons();
});
// Grid Selection
function initGridSelection() {
const gridOptions = document.querySelectorAll('.grid-option');
gridOptions.forEach(option => {
option.addEventListener('click', () => {
gridOptions.forEach(opt => opt.classList.remove('selected'));
option.classList.add('selected');
gridSize = parseInt(option.dataset.size);
winCondition = gridSize === 3 ? 3 : gridSize === 5 ? 4 : 5;
});
});
}
// Button Event Listeners
function initButtons() {
// Start Game
document.getElementById('start-btn').addEventListener('click', startGame);
// Rules
document.getElementById('rules-btn').addEventListener('click', () => {
rulesModal.classList.add('active');
});
document.getElementById('close-rules').addEventListener('click', () => {
rulesModal.classList.remove('active');
});
// Finish Game
document.getElementById('finish-btn').addEventListener('click', () => {
showFinishModal();
});
// Finish Modal Buttons
document.getElementById('resume-btn').addEventListener('click', () => {
finishModal.classList.remove('active');
});
document.getElementById('new-game-btn').addEventListener('click', () => {
finishModal.classList.remove('active');
goToLanding();
});
document.getElementById('quit-btn').addEventListener('click', quitGame);
// Game Over Modal Buttons
document.getElementById('play-again-btn').addEventListener('click', () => {
gameoverModal.classList.remove('active');
resetBoard();
});
document.getElementById('gameover-new-btn').addEventListener('click', () => {
gameoverModal.classList.remove('active');
goToLanding();
});
document.getElementById('gameover-quit-btn').addEventListener('click', quitGame);
// 🔙 BACK BUTTON (NEW)
document.getElementById('back-btn').addEventListener('click', () => {
if (confirm("Go back? Current game will be lost.")) {
goToLanding();
}
});
// Close modals on outside click
[rulesModal, finishModal, gameoverModal].forEach(modal => {
modal.addEventListener('click', (e) => {
if (e.target === modal) {
modal.classList.remove('active');
}
});
});
}
// Start Game
function startGame() {
const p1Input = document.getElementById('player1-name').value.trim();
const p2Input = document.getElementById('player2-name').value.trim();
playerNames.player1 = p1Input || 'Player 1';
playerNames.player2 = p2Input || 'Player 2';
document.getElementById('p1-name-display').textContent = playerNames.player1;
document.getElementById('p2-name-display').textContent = playerNames.player2;
scores = { player1: 0, player2: 0 };
updateScoreDisplay();
landingPage.classList.remove('active');
gamePage.classList.add('active');
initBoard();
}
// Initialize Board
function initBoard() {
board = [];
currentPlayer = 1;
gameActive = true;
winningCells = [];
for (let i = 0; i < gridSize; i++) {
board[i] = [];
for (let j = 0; j < gridSize; j++) {
board[i][j] = '';
}
}
renderGrid();
updateTurnIndicator();
}
// Render Grid
function renderGrid() {
gameGrid.innerHTML = '';
gameGrid.className = 'game-grid size-' + gridSize;
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
const cell = document.createElement('div');
cell.className = 'cell';
cell.dataset.row = i;
cell.dataset.col = j;
cell.addEventListener('click', () => handleCellClick(i, j, cell));
gameGrid.appendChild(cell);
}
}
}
// Handle Cell Click
function handleCellClick(row, col, cell) {
if (!gameActive || board[row][col] !== '') return;
board[row][col] = currentPlayer === 1 ? 'X' : 'O';
cell.textContent = board[row][col];
cell.classList.add('taken', currentPlayer === 1 ? 'x' : 'o', 'animate-in');
const winner = checkWinner(row, col);
if (winner) {
gameActive = false;
highlightWinningCells();
if (winner === 'X') {
scores.player1++;
} else {
scores.player2++;
}
updateScoreDisplay();
setTimeout(() => {
showGameOverModal(winner);
}, 800);
} else if (isBoardFull()) {
gameActive = false;
setTimeout(() => {
showTieModal();
}, 300);
} else {
currentPlayer = currentPlayer === 1 ? 2 : 1;
updateTurnIndicator();
}
}
// Check Winner
function checkWinner(row, col) {
const symbol = board[row][col];
const directions = [
[[0, 1], [0, -1]],
[[1, 0], [-1, 0]],
[[1, 1], [-1, -1]],
[[1, -1], [-1, 1]]
];
for (const dir of directions) {
let count = 1;
let cells = [[row, col]];
for (const [dr, dc] of dir) {
let r = row + dr;
let c = col + dc;
while (r >= 0 && r < gridSize && c >= 0 && c < gridSize && board[r][c] === symbol) {
count++;
cells.push([r, c]);
r += dr;
c += dc;
}
}
if (count >= winCondition) {
winningCells = cells;
return symbol;
}
}
return null;
}
// Highlight Winning Cells
function highlightWinningCells() {
const cells = document.querySelectorAll('.cell');
winningCells.forEach(([row, col]) => {
const index = row * gridSize + col;
cells[index].classList.add('winning');
});
}
// Check if Board is Full
function isBoardFull() {
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
if (board[i][j] === '') return false;
}
}
return true;
}
// Update Turn Indicator
function updateTurnIndicator() {
const indicator = document.getElementById('current-player-indicator');
const text = document.getElementById('turn-text');
indicator.className = 'player-indicator ' + (currentPlayer === 1 ? 'cyan-bg' : 'coral-bg');
text.textContent = `${currentPlayer === 1 ? playerNames.player1 : playerNames.player2}'s Turn`;
}
// Update Score Display
function updateScoreDisplay() {
document.getElementById('p1-score').textContent = scores.player1;
document.getElementById('p2-score').textContent = scores.player2;
}
// Reset Board
function resetBoard() {
initBoard();
}
// 🔥 IMPROVED Go to Landing
function goToLanding() {
gameActive = false;
board = [];
winningCells = [];
gamePage.classList.remove('active');
landingPage.classList.add('active');
}
// Quit Game
function quitGame() {
window.close();
setTimeout(() => {
alert('Please close this tab manually to quit the game.');
}, 100);
}
// Show Finish Modal
function showFinishModal() {
document.getElementById('modal-title').textContent = 'Finish Game?';
document.getElementById('modal-message').textContent = 'Are you sure you want to end the current game?';
document.getElementById('resume-btn').style.display = 'block';
finishModal.classList.add('active');
}
// Show Game Over Modal
function showGameOverModal(winner) {
const winnerName = winner === 'X' ? playerNames.player1 : playerNames.player2;
document.getElementById('gameover-title').textContent = 'Victory!';
document.getElementById('gameover-message').textContent = `${winnerName} wins this round!`;
gameoverModal.classList.add('active');
}
// Show Tie Modal
function showTieModal() {
document.getElementById('gameover-title').textContent = 'It\'s a Tie!';
document.getElementById('gameover-message').textContent = 'No winner this round. Try again!';
gameoverModal.classList.add('active');
}