-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
415 lines (374 loc) · 16.3 KB
/
Copy pathindex.html
File metadata and controls
415 lines (374 loc) · 16.3 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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta
name="description"
content="Play Connect 4 against a Java minimax AI with alpha-beta pruning, or challenge a friend. A live Node.js + Socket.IO game client."
/>
<title>Connect 4 — AI & Human Matchmaker</title>
<link rel="stylesheet" href="style.css?v=2.0.0" />
<link rel="stylesheet" href="/assets/fonts/fonts.css" />
<link rel="preconnect" href="https://cdnjs.cloudflare.com" crossorigin />
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.1/css/all.min.css"
crossorigin="anonymous"
/>
<!-- Favicons & Mobile Device Integration -->
<link rel="apple-touch-icon" sizes="180x180" href="/images/mstile-150x150.png" />
<link rel="icon" type="image/png" sizes="32x32" href="/images/mstile-70x70.png" />
<link rel="manifest" href="/images/manifest.json" />
<link rel="mask-icon" href="/images/safari-pinned-tab.svg" color="#1a3478" />
<meta name="msapplication-TileColor" content="#1a3478" />
<meta name="msapplication-TileImage" content="/images/mstile-144x144.png" />
<meta name="msapplication-config" content="/images/browserconfig.xml" />
<meta name="theme-color" content="#f7f8fa" media="(prefers-color-scheme: light)" />
<meta name="theme-color" content="#12141a" media="(prefers-color-scheme: dark)" />
<!-- Open Graph / Facebook / LinkedIn -->
<meta property="og:type" content="website" />
<meta property="og:url" content="https://agreddy.com/connect4/" />
<meta property="og:title" content="Connect 4 — AI & Human Matchmaker" />
<meta
property="og:description"
content="Play Connect 4 against a Java minimax AI with alpha-beta pruning, or challenge a friend in real time."
/>
<meta property="og:image" content="https://agreddy.com/images/profile.png" />
<meta property="og:site_name" content="Aadarsha Gopala Reddy" />
<!-- Twitter Cards -->
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:url" content="https://agreddy.com/connect4/" />
<meta name="twitter:title" content="Connect 4 — AI & Human Matchmaker" />
<meta
name="twitter:description"
content="Play Connect 4 against a Java minimax AI with alpha-beta pruning, or challenge a friend in real time."
/>
<meta name="twitter:image" content="https://agreddy.com/images/profile.png" />
<meta name="twitter:site" content="@aadarsha2002" />
<!-- Canonical Link -->
<link rel="canonical" href="https://agreddy.com/connect4/" />
<!-- Schema.org JSON-LD -->
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "VideoGame",
"name": "Connect 4 — AI & Human Matchmaker",
"url": "https://agreddy.com/connect4/",
"applicationCategory": "Game",
"author": {
"@type": "Person",
"name": "Aadarsha Gopala Reddy",
"url": "https://agreddy.com"
}
}
</script>
<!-- Google Tag Manager -->
<script>
(function (w, d, s, l, i) {
w[l] = w[l] || [];
w[l].push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' });
var f = d.getElementsByTagName(s)[0],
j = d.createElement(s),
dl = l != 'dataLayer' ? '&l=' + l : '';
j.async = true;
j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'GTM-N83BNQ64');
</script>
<!-- End Google Tag Manager -->
</head>
<body>
<!-- Google Tag Manager (noscript) -->
<noscript
><iframe
src="https://www.googletagmanager.com/ns.html?id=GTM-N83BNQ64"
height="0"
width="0"
style="display: none; visibility: hidden"
></iframe
></noscript>
<!-- End Google Tag Manager (noscript) -->
<!-- Skip Link for Keyboard Accessibility -->
<a href="#game-board" class="skip-link">Skip to Game Board</a>
<!-- Top Navigation Bar (Back link & Theme Switcher) -->
<header class="top-header">
<div class="top-bar container">
<a href="/" class="back-home-link" aria-label="Back to Showcase Home">
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2.5"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<line x1="19" y1="12" x2="5" y2="12" />
<polyline points="12 19 5 12 12 5" />
</svg>
<span>Showcase Portfolio</span>
</a>
<div id="theme-toggle" aria-label="Toggle theme"></div>
</div>
</header>
<div class="app-container">
<main>
<div class="page-header">
<span class="header-accent" aria-hidden="true">Minimax Alpha-Beta Solver</span>
<h1 class="logo">CONNECT<span>4</span></h1>
<p class="tagline">Java Decision Engine & Game Client</p>
</div>
<!-- Game Configuration Panel -->
<div id="setup-panel" class="card spotlight-card fade-in">
<div class="card-header">
<h2>Game Setup</h2>
<p>Configure match parameters to initialize the Java game loop.</p>
</div>
<div class="card-body">
<div id="human-question" class="question-step">
<p class="prompt-text">Select Opponent Model:</p>
<div class="btn-group">
<button id="btn-vs-ai" class="btn btn-primary">Play Against AI</button>
<button id="btn-vs-human" class="btn btn-secondary">Play Against Human</button>
</div>
</div>
<div id="first-question" class="question-step hidden">
<p class="prompt-text">Do you want to take the first turn?</p>
<div class="btn-group">
<button id="btn-first-yes" class="btn btn-primary">Yes, Go First</button>
<button id="btn-first-no" class="btn btn-secondary">No, AI First</button>
</div>
</div>
</div>
</div>
<!-- Main Play Area (Hidden until setup is done) -->
<div id="game-panel" class="hidden">
<div class="game-layout">
<!-- The Grid Board -->
<div class="board-container spotlight-card">
<div class="column-indicators">
<button class="col-drop-btn" data-col="1" aria-label="Drop in column 1">↓</button>
<button class="col-drop-btn" data-col="2" aria-label="Drop in column 2">↓</button>
<button class="col-drop-btn" data-col="3" aria-label="Drop in column 3">↓</button>
<button class="col-drop-btn" data-col="4" aria-label="Drop in column 4">↓</button>
<button class="col-drop-btn" data-col="5" aria-label="Drop in column 5">↓</button>
<button class="col-drop-btn" data-col="6" aria-label="Drop in column 6">↓</button>
<button class="col-drop-btn" data-col="7" aria-label="Drop in column 7">↓</button>
</div>
<div class="board" id="game-board">
<!-- JS will generate 42 cells (6 rows * 7 columns) -->
</div>
</div>
<!-- Sidebar Status panel -->
<div class="sidebar-panel">
<div class="card status-card spotlight-card">
<h3>Match Status</h3>
<div class="status-indicator" id="status-indicator">
<div class="pulse-indicator" aria-hidden="true"></div>
<span id="status-text" aria-live="polite">Initializing game server...</span>
</div>
<div class="turn-display" id="turn-display">
<div class="player-pill player-h" id="pill-player1">Human</div>
<span class="vs">vs</span>
<div class="player-pill player-a" id="pill-player2">AI</div>
</div>
<button id="btn-restart" class="btn btn-danger">Start New Game</button>
</div>
<div class="card log-card spotlight-card">
<h3>JVM Engine Logs</h3>
<pre id="console-log">Connecting to Java terminal...</pre>
</div>
</div>
</div>
</div>
</main>
</div>
<!-- Win/Loss Overlay Modal -->
<dialog id="game-over-modal" class="game-over-modal">
<div class="modal-card">
<h2 id="modal-title">Match Finished!</h2>
<p id="modal-desc">The solver has completed the game.</p>
<button id="btn-show-results" class="btn btn-primary">Show Results</button>
</div>
</dialog>
<!-- Socket.IO Client Lib -->
<script src="/connect4/socket.io/socket.io.js"></script>
<script src="/assets/js/theme.js"></script>
<script>
const socket = io({
path: '/connect4/socket.io',
});
let gameState = {
board: null,
prompt: null,
winner: null,
gameOver: false,
againstHuman: false,
};
const setupPanel = document.getElementById('setup-panel');
const gamePanel = document.getElementById('game-panel');
const humanQuestion = document.getElementById('human-question');
const firstQuestion = document.getElementById('first-question');
const boardElement = document.getElementById('game-board');
const statusText = document.getElementById('status-text');
const consoleLog = document.getElementById('console-log');
const gameOverModal = document.getElementById('game-over-modal');
const modalTitle = document.getElementById('modal-title');
const modalDesc = document.getElementById('modal-desc');
const statusIndicator = document.getElementById('status-indicator');
const btnShowResults = document.getElementById('btn-show-results');
// Create Grid Cells
const initializeGrid = () => {
boardElement.innerHTML = '';
for (let r = 0; r < 6; r++) {
for (let c = 0; c < 7; c++) {
const cell = document.createElement('div');
cell.className = 'cell';
cell.dataset.row = r;
cell.dataset.col = c;
const slot = document.createElement('div');
slot.className = 'slot empty';
cell.appendChild(slot);
// Allow clicking any cell in a column to make a move
cell.addEventListener('click', () => {
const col = c + 1; // 1-indexed column
const firstBtn = document.querySelector('.col-drop-btn');
if (firstBtn && !firstBtn.disabled) {
socket.emit('input', col.toString());
}
});
boardElement.appendChild(cell);
}
}
};
initializeGrid();
// Start Java Process Socket Connection
socket.emit('start-game');
// Action Handlers
document.getElementById('btn-vs-ai').addEventListener('click', () => {
socket.emit('input', 'N');
gameState.againstHuman = false;
humanQuestion.classList.add('hidden');
firstQuestion.classList.remove('hidden');
document.getElementById('pill-player1').textContent = 'Human';
document.getElementById('pill-player2').textContent = 'AI';
document.getElementById('pill-player1').className = 'player-pill player-h';
document.getElementById('pill-player2').className = 'player-pill player-a';
});
document.getElementById('btn-vs-human').addEventListener('click', () => {
socket.emit('input', 'Y');
gameState.againstHuman = true;
setupPanel.classList.add('hidden');
gamePanel.classList.remove('hidden');
statusText.textContent = 'Player 1 Turn';
document.getElementById('pill-player1').textContent = 'Player 1';
document.getElementById('pill-player2').textContent = 'Player 2';
document.getElementById('pill-player1').className = 'player-pill player-1';
document.getElementById('pill-player2').className = 'player-pill player-2';
});
document.getElementById('btn-first-yes').addEventListener('click', () => {
socket.emit('input', 'Y');
setupPanel.classList.add('hidden');
gamePanel.classList.remove('hidden');
statusText.textContent = 'Your Turn';
});
document.getElementById('btn-first-no').addEventListener('click', () => {
socket.emit('input', 'N');
setupPanel.classList.add('hidden');
gamePanel.classList.remove('hidden');
statusText.textContent = 'AI is calculating...';
});
const resetGame = () => {
gameOverModal.close();
setupPanel.classList.remove('hidden');
gamePanel.classList.add('hidden');
humanQuestion.classList.remove('hidden');
firstQuestion.classList.add('hidden');
statusIndicator.classList.remove('game-over');
document.querySelectorAll('.col-drop-btn').forEach((b) => (b.disabled = false));
initializeGrid();
socket.emit('start-game');
};
document.getElementById('btn-restart').addEventListener('click', resetGame);
btnShowResults.addEventListener('click', () => {
gameOverModal.close();
statusIndicator.classList.add('game-over');
statusText.textContent =
gameState.winner === 'Tie'
? "It's a Tie! Game Over."
: `${gameState.winner} Wins! Game Over.`;
});
// Column Drop Buttons
document.querySelectorAll('.col-drop-btn').forEach((btn) => {
btn.addEventListener('click', () => {
const col = btn.dataset.col;
socket.emit('input', col);
});
});
// Socket Events
socket.on('game-update', (data) => {
console.log('Received Game Update', data);
gameState = { ...gameState, ...data };
if (data.raw) {
consoleLog.textContent = data.raw;
consoleLog.scrollTop = consoleLog.scrollHeight;
}
if (data.board) {
renderBoard(data.board);
}
if (data.gameOver) {
showGameOver(data.winner);
} else if (data.prompt === 'PLAY_MOVE') {
statusText.textContent = gameState.againstHuman ? "Player's turn" : 'Your turn';
document.querySelectorAll('.col-drop-btn').forEach((b) => (b.disabled = false));
} else {
if (!gameState.againstHuman && data.prompt === null) {
statusText.textContent = 'AI thinking...';
document.querySelectorAll('.col-drop-btn').forEach((b) => (b.disabled = true));
}
}
});
const renderBoard = (board) => {
for (let r = 0; r < 6; r++) {
for (let c = 0; c < 7; c++) {
const cell = document.querySelector(`.cell[data-row="${r}"][data-col="${c}"]`);
if (cell) {
const slot = cell.querySelector('.slot');
const val = board[r][c];
slot.className = 'slot';
if (val === 'H') {
slot.classList.add('human');
} else if (val === 'A') {
slot.classList.add('ai');
} else if (val === '1') {
slot.classList.add('p1');
} else if (val === '2') {
slot.classList.add('p2');
} else {
slot.classList.add('empty');
}
}
}
}
};
const showGameOver = (winner) => {
modalTitle.textContent = winner === 'Tie' ? "It's a Tie!" : `${winner} Wins!`;
modalDesc.textContent =
winner === 'Tie'
? 'The game board is completely full.'
: `Congratulations to ${winner} for taking the victory!`;
if (!gameOverModal.open) gameOverModal.showModal();
document.querySelectorAll('.col-drop-btn').forEach((b) => (b.disabled = true));
};
// ── Spotlight cursor glow tracking ──
document.querySelectorAll('.spotlight-card').forEach((card) => {
card.addEventListener('mousemove', (e) => {
const rect = card.getBoundingClientRect();
card.style.setProperty('--mouse-x', `${e.clientX - rect.left}px`);
card.style.setProperty('--mouse-y', `${e.clientY - rect.top}px`);
});
});
</script>
</body>
</html>