-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.js
More file actions
343 lines (280 loc) · 11.3 KB
/
ui.js
File metadata and controls
343 lines (280 loc) · 11.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
import NeutronGame from './neutron.js'
import { kWhitePiece, kBlackPiece, kNeutralPiece, kEmpty, kWhite1, kWhite2, kBlack1, kBlack2 } from './constants.js';
document.addEventListener('DOMContentLoaded', () => {
// UI handlers. Cell clicks are handled in grid setup.
document.getElementById('resetButton').addEventListener('click', () => {
game.initializeGameState();
resetGameUI(game);
});
document.getElementById('help-button').addEventListener('click', () => {
document.getElementById('help-overlay').style.display = 'flex';
});
document.getElementById('close-help-button').addEventListener('click', () => {
document.getElementById('help-overlay').style.display = 'none';
});
document.getElementById('difficultyButton').addEventListener('click', () => {
game.setDifficulty((game.difficulty === 5) ? 1 : game.difficulty + 2);
updateDifficultyIndicator(game);
});
document.getElementById('toggleModeButton').addEventListener('click', () => {
game.aiEnabled = !game.aiEnabled;
document.getElementById('toggleModeButton').textContent = game.aiEnabled ? "Disable AI" : "Enable AI";
document.getElementById('difficultyButton').disabled = !game.aiEnabled;
});
const game = new NeutronGame(5, 5);
// Render the initial grid
resetGameUI(game);
});
function resetGameUI(game) {
game.initializeGameState();
const gameGridElement = document.getElementById('gameGrid');
setupGridUI(gameGridElement, game);
updateTurnIndicator(game);
updateDifficultyIndicator(game);
document.getElementById('wait-label').style.display='none';
document.getElementById('help-button').style.display = 'flex';
document.getElementById('toggleModeButton').disabled = false;
}
function setupGridUI(gameGridElement, game) {
gameGridElement.innerHTML = '';
for (let y = 0; y < game.gridYsize; y++) {
for (let x = 0; x < game.gridXsize; x++) {
const cellDiv = document.createElement('div');
cellDiv.classList.add('cell');
cellDiv.dataset.x = x;
cellDiv.dataset.y = y;
cellDiv.addEventListener('click', () => {
handleCellClick(x, y, game);
});
gameGridElement.appendChild(cellDiv);
const cellState = game.grid[x][y].state;
if (cellState !== kEmpty) {
const pieceImg = document.createElement('img');
pieceImg.src = getPieceImage(cellState);
pieceImg.classList.add('piece');
cellDiv.appendChild(pieceImg);
}
}
}
}
async function handleAITurn(game) {
if (game.finished) {
console.log("Game is over, no more moves allowed.");
return;
}
console.log("AI is thinking...");
const before = new Date().getTime();
const bestMove = game.getBestMoveForState(game, kBlackPiece);
const after = new Date().getTime();
console.log("AI took", after - before, "ms to make a move.");
console.log("Best neutron move found by AI:", bestMove[0]);
console.log("Best piece move found by AI:", bestMove[1]);
if (bestMove.length > 0) {
await animateMove(bestMove[0][0][0], bestMove[0][0][1], bestMove[0][1][0], bestMove[0][1][1], game);
game.switchTurn();
// Ensure a slight delay between the moves to prevent overlap
await new Promise((resolve) => setTimeout(resolve, 100));
await animateMove(bestMove[1][0][0], bestMove[1][0][1], bestMove[1][1][0], bestMove[1][1][1], game);
game.switchTurn(); // Switch to the next player's turn after the piece move
updateTurnIndicator(game);
document.getElementById('wait-label').style.display = 'none';
document.getElementById('help-button').style.display = 'flex';
} else {
console.log("No valid moves for AI.");
}
const winner = checkGameOver(game);
if (game.finished) {
gameOver(winner);
return;
}
}
function animateMove(fromX, fromY, toX, toY, game) {
const cellElement = document.querySelector(`.cell[data-x="${fromX}"][data-y="${fromY}"]`);
if (cellElement) {
const pieceElement = cellElement.querySelector('.piece');
if (pieceElement) {
const cellSize = cellElement.getBoundingClientRect().width;
const deltaX = (toX - fromX) * cellSize;
const deltaY = (toY - fromY) * cellSize;
// animate in a Promise to be resolved when the animation ends
return new Promise((resolve) => {
pieceElement.style.transition = 'transform 0.5s ease';
pieceElement.style.transform = `translate(${deltaX}px, ${deltaY}px)`;
pieceElement.addEventListener('transitionend', function handleTransition() {
pieceElement.removeEventListener('transitionend', handleTransition);
// Now move the piece in game logic
game.movePiece(fromX, fromY, toX, toY); // Update game state after animation
const targetCellElement = document.querySelector(`.cell[data-x="${toX}"][data-y="${toY}"]`);
if (targetCellElement) {
targetCellElement.appendChild(pieceElement);
}
pieceElement.style.transition = '';
pieceElement.style.transform = '';
clearHighlights();
resolve();
}, { once: true });
});
} else {
console.error(`No piece found in the cell at (${fromX}, ${fromY}) to animate.`);
}
} else {
console.error(`No cell found at (${fromX}, ${fromY}) to animate.`);
}
return Promise.resolve();
}
function handleMove(fromX, fromY, toX, toY, game) {
return animateMove(fromX, fromY, toX, toY, game)
.then(() => {
if (game.turn === kBlackPiece) {
document.getElementById('wait-label').style.display='flex';
document.getElementById('help-button').style.display = 'none';
}
return new Promise((resolve) => setTimeout(resolve, 300));
})
.then(() => {
if (game.turn === kBlackPiece && game.aiEnabled) {
handleAITurn(game);
}
})
.catch((error) => {
console.error("Error in handleMove:", error);
});
}
function handleCellClick(x, y, game) {
if (game.finished) {
console.log("Game is over, no more moves allowed.");
return;
}
const cellState = game.grid[x][y].state;
if (game.selectedPiece !== null) {
if (game.isValidMove(game.selectedPiece.x, game.selectedPiece.y, x, y)) {
handleMove( game.selectedPiece.x, game.selectedPiece.y, x, y, game);
game.switchTurn();
updateTurnIndicator(game);
game.selectedPiece = null;
const winner = checkGameOver(game);
if (game.finished) {
console.log("Game is over, no more moves allowed.");
gameOver(winner);
return;
}
// When the first move has been made, disable the AI toggle button
if (!game.gameStarted) {
game.gameStarted = true;
document.getElementById('toggleModeButton').disabled = true;
document.getElementById('difficultyButton').disabled = true;
}
} else {
// Invalid move, reset
game.selectedPiece = null;
setupGridUI(document.getElementById('gameGrid'), game);
}
} else {
if ((cellState === game.turn && (game.turnStage === kWhite2 || game.turnStage === kBlack2))
|| (cellState === kNeutralPiece && (game.turnStage === kWhite1 || game.turnStage === kBlack1))) {
game.selectedPiece = { x, y };
game.highlightMovesForX(x, y); // highlight in game object
highlightValidMoves(game); // highlight in UI
}
}
}
function checkGameOver(game) {
const neutronPos = game.getPiecesForColour(kNeutralPiece)[0];
if (!neutronPos) {
console.error('Neutron not found on the board');
return null;
}
const neutronY = neutronPos[1];
if (neutronY === 0) {
game.finished = true;
return kBlackPiece; // Black wins
} else if (neutronY === game.gridYsize - 1) {
game.finished = true;
return kWhitePiece; // White wins
}
if (!game.neutronHasMoves()) {
game.finished = true;
return (game.turn === kBlack1) ? kWhitePiece : kBlackPiece; // Other player wins
}
return null; // No winner yet
}
function gameOver(winner) {
const turnIndicator = document.getElementById('turnIndicator');
let turnText = "";
if (winner === kWhitePiece) {
turnText = "White Wins!";
turnIndicator.classList.add('white-turn');
turnIndicator.classList.remove('black-turn');
} else {
turnText = "Black Wins!";
turnIndicator.classList.add('black-turn');
turnIndicator.classList.remove('white-turn');
}
turnIndicator.innerText = turnText;
}
function highlightValidMoves(game) {
const highlightedCells = game.getHighlightedCells();
highlightedCells.forEach(([x, y]) => {
const cellDiv = document.querySelector(`.cell[data-x='${x}'][data-y='${y}']`);
if (cellDiv) {
cellDiv.classList.add('highlight');
}
});
}
function getPieceImage(state) {
switch (state) {
case kBlackPiece:
return 'img/black.png';
case kWhitePiece:
return 'img/white.png';
case kNeutralPiece:
return 'img/neutron.png';
default:
return '';
}
}
function updateTurnIndicator(game) {
const turnIndicator = document.getElementById('turnIndicator');
let turnText = "";
if (game.turn === kWhitePiece) {
turnText = "White: ";
turnIndicator.classList.add('white-turn');
turnIndicator.classList.remove('black-turn');
} else {
turnText = "Black: ";
turnIndicator.classList.add('black-turn');
turnIndicator.classList.remove('white-turn');
}
if (game.turnStage === kWhite1 || game.turnStage === kBlack1) {
turnText += "Move the Neutron";
turnIndicator.classList.add('neutron-stage');
} else {
turnText += "Move a Piece";
turnIndicator.classList.remove('neutron-stage');
}
turnIndicator.innerText = turnText;
}
function updateDifficultyIndicator(game) {
const difficultyIndicator = document.getElementById('difficultyButton');
let difficultyText = '';
switch (game.difficulty) {
case 1:
difficultyText = 'Easy';
break;
case 3:
difficultyText = 'Normal';
break;
case 5:
difficultyText = 'Hard';
break;
default:
difficultyText = 'Unknown';
}
difficultyIndicator.textContent = `Difficulty: ${difficultyText}`;
}
function clearHighlights() {
const highlightedCells = document.querySelectorAll('.highlight');
highlightedCells.forEach(cell => {
cell.classList.remove('highlight');
});
}