-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_checkpoint.html
More file actions
478 lines (392 loc) · 22.6 KB
/
test_checkpoint.html
File metadata and controls
478 lines (392 loc) · 22.6 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Checkpoint 8 - Core AI and Gameplay Mechanics Test</title>
<style>
body {
margin: 0;
padding: 20px;
background: #000;
color: #00ff00;
font-family: monospace;
}
.test-section {
margin: 20px 0;
border: 1px solid #333;
padding: 15px;
background: #111;
}
.test-result {
padding: 5px;
margin: 5px 0;
}
.pass {
color: #00ff00;
}
.fail {
color: #ff0000;
}
.warn {
color: #ffff00;
}
.summary {
margin-top: 20px;
padding: 15px;
border: 2px solid #00ff00;
background: #002200;
}
canvas {
border: 1px solid #333;
margin: 10px 0;
}
</style>
</head>
<body>
<h1>Checkpoint 8: Core AI and Gameplay Mechanics Test</h1>
<div id="testResults"></div>
<canvas id="testCanvas" width="400" height="300"></canvas>
<div id="summary" class="summary"></div>
<!-- Include all game modules -->
<script src="js/mazeGraph.js"></script>
<script src="js/pacman.js"></script>
<script src="js/ghost.js"></script>
<script src="js/ghostAI.js"></script>
<script src="js/inputHandler.js"></script>
<script src="js/renderer.js"></script>
<script src="js/gameEngine.js"></script>
<script>
class CheckpointTester {
constructor() {
this.results = [];
this.canvas = document.getElementById('testCanvas');
this.ctx = this.canvas.getContext('2d');
this.resultsDiv = document.getElementById('testResults');
this.summaryDiv = document.getElementById('summary');
}
test(name, condition, section = 'General') {
const passed = condition;
const result = { name, passed, section };
this.results.push(result);
this.logResult(name, passed, section);
return passed;
}
logResult(name, passed, section) {
const resultDiv = document.createElement('div');
resultDiv.className = `test-result ${passed ? 'pass' : 'fail'}`;
resultDiv.textContent = `${passed ? '✓' : '✗'} [${section}] ${name}`;
this.resultsDiv.appendChild(resultDiv);
}
warn(message, section = 'Warning') {
const warnDiv = document.createElement('div');
warnDiv.className = 'test-result warn';
warnDiv.textContent = `⚠ [${section}] ${message}`;
this.resultsDiv.appendChild(warnDiv);
}
createSection(title) {
const sectionDiv = document.createElement('div');
sectionDiv.className = 'test-section';
sectionDiv.innerHTML = `<h3>${title}</h3>`;
this.resultsDiv.appendChild(sectionDiv);
return sectionDiv;
}
async runAllTests() {
console.log('Starting Checkpoint 8 comprehensive tests...');
// Test 1: Core Module Loading
this.createSection('Module Loading Tests');
this.testModuleLoading();
// Test 2: Maze Graph System
this.createSection('Maze Graph System Tests');
this.testMazeGraph();
// Test 3: PacMan Entity
this.createSection('PacMan Entity Tests');
this.testPacManEntity();
// Test 4: Ghost AI System
this.createSection('Ghost AI System Tests');
await this.testGhostAI();
// Test 5: Game Engine Integration
this.createSection('Game Engine Integration Tests');
await this.testGameEngine();
// Test 6: Collision Detection
this.createSection('Collision Detection Tests');
this.testCollisionDetection();
// Test 7: Input Handling
this.createSection('Input Handling Tests');
this.testInputHandling();
// Test 8: Rendering System
this.createSection('Rendering System Tests');
this.testRenderingSystem();
// Generate summary
this.generateSummary();
}
testModuleLoading() {
this.test('MazeGraph class available', typeof MazeGraph === 'function', 'Modules');
this.test('PacMan class available', typeof PacMan === 'function', 'Modules');
this.test('Ghost class available', typeof Ghost === 'function', 'Modules');
this.test('GhostAI class available', typeof GhostAI === 'function', 'Modules');
this.test('GameEngine class available', typeof GameEngine === 'function', 'Modules');
this.test('InputHandler class available', typeof InputHandler === 'function', 'Modules');
this.test('Renderer class available', typeof Renderer === 'function', 'Modules');
}
testMazeGraph() {
try {
const maze = new MazeGraph(20, 15);
this.test('MazeGraph constructor works', maze !== null, 'MazeGraph');
this.test('MazeGraph has correct dimensions', maze.width === 20 && maze.height === 15, 'MazeGraph');
this.test('MazeGraph initializes empty tiles', maze.getAllEmpty().length > 0, 'MazeGraph');
// Test tile operations
maze.setTileType(5, 5, maze.TILE_TYPES.WALL);
this.test('Can set wall tiles', maze.getTileType(5, 5) === 'wall', 'MazeGraph');
this.test('Wall tiles are not walkable', !maze.isWalkable(5, 5), 'MazeGraph');
// Test demo maze creation
const created = maze.createDemoMaze();
this.test('Demo maze creation succeeds', created === true, 'MazeGraph');
this.test('Demo maze has walls', maze.getAllWalls().length > 0, 'MazeGraph');
this.test('Demo maze has pellets', maze.getPelletCount() > 0, 'MazeGraph');
this.test('Demo maze has power pellets', maze.getPowerPelletCount() > 0, 'MazeGraph');
// Test pathfinding
const path = maze.findPathBFS(1, 1, 5, 5);
this.test('BFS pathfinding returns array', Array.isArray(path), 'MazeGraph');
const pathAStar = maze.findPathAStar(1, 1, 5, 5);
this.test('A* pathfinding returns array', Array.isArray(pathAStar), 'MazeGraph');
} catch (error) {
this.test('MazeGraph basic functionality', false, 'MazeGraph');
console.error('MazeGraph test error:', error);
}
}
testPacManEntity() {
try {
const pacman = new PacMan(10, 10);
this.test('PacMan constructor works', pacman !== null, 'PacMan');
this.test('PacMan has correct initial position', pacman.position.x === 10 && pacman.position.y === 10, 'PacMan');
this.test('PacMan starts with no direction', pacman.direction.name === 'none', 'PacMan');
// Test position setting
pacman.setPosition(15, 12);
this.test('Can set PacMan position', pacman.position.x === 15 && pacman.position.y === 12, 'PacMan');
// Test movement history
const history = pacman.getMovementHistory();
this.test('Movement history is array', Array.isArray(history), 'PacMan');
// Test predicted position
const predicted = pacman.getPredictedPosition(4);
this.test('Predicted position calculation works', predicted && typeof predicted.x === 'number', 'PacMan');
// Test debug info
const debugInfo = pacman.getDebugInfo();
this.test('Debug info available', debugInfo && typeof debugInfo === 'object', 'PacMan');
} catch (error) {
this.test('PacMan basic functionality', false, 'PacMan');
console.error('PacMan test error:', error);
}
}
async testGhostAI() {
try {
const ghostAI = new GhostAI();
const maze = new MazeGraph(20, 15);
maze.createDemoMaze();
this.test('GhostAI constructor works', ghostAI !== null, 'GhostAI');
// Test ghost initialization
const ghostConfigs = [
{ x: 5, y: 5, color: 'red', personality: 'aggressive' },
{ x: 15, y: 5, color: 'pink', personality: 'ambush' },
{ x: 15, y: 10, color: 'cyan', personality: 'patrol' },
{ x: 5, y: 10, color: 'orange', personality: 'random' }
];
ghostAI.initializeGhosts(ghostConfigs, maze);
const ghosts = ghostAI.getGhosts();
this.test('Correct number of ghosts initialized', ghosts.length === 4, 'GhostAI');
this.test('Ghosts have correct colors',
ghosts[0].color === 'red' &&
ghosts[1].color === 'pink' &&
ghosts[2].color === 'cyan' &&
ghosts[3].color === 'orange', 'GhostAI');
// Test ghost personalities
this.test('Ghosts have correct personalities',
ghosts[0].personality === 'aggressive' &&
ghosts[1].personality === 'ambush' &&
ghosts[2].personality === 'patrol' &&
ghosts[3].personality === 'random', 'GhostAI');
// Test ghost state machine
const redGhost = ghosts[0];
this.test('Ghost starts in scatter state', redGhost.currentState === 'scatter', 'GhostAI');
this.test('Ghost has state configuration', redGhost.stateConfig && typeof redGhost.stateConfig === 'object', 'GhostAI');
// Test AI update
const pacman = new PacMan(10, 7);
const gameState = {
powerPelletActive: false,
powerPelletTimer: 0,
score: 0,
lives: 3,
level: 1
};
ghostAI.update(16.67, pacman, maze, gameState);
this.test('AI update completes without error', true, 'GhostAI');
// Test collision detection
const collisions = ghostAI.checkCollisions(pacman);
this.test('Collision detection returns array', Array.isArray(collisions), 'GhostAI');
// Test performance metrics
const metrics = ghostAI.getPerformanceMetrics();
this.test('Performance metrics available', metrics && typeof metrics.lastUpdateTime === 'number', 'GhostAI');
} catch (error) {
this.test('GhostAI basic functionality', false, 'GhostAI');
console.error('GhostAI test error:', error);
}
}
async testGameEngine() {
try {
const gameEngine = new GameEngine(this.canvas);
this.test('GameEngine constructor works', gameEngine !== null, 'GameEngine');
this.test('GameEngine has canvas reference', gameEngine.canvas === this.canvas, 'GameEngine');
this.test('GameEngine has initial game state', gameEngine.gameState && typeof gameEngine.gameState === 'object', 'GameEngine');
// Test entity initialization
const maze = new MazeGraph(20, 15);
maze.createDemoMaze();
const inputHandler = new InputHandler();
gameEngine.initializeEntities(inputHandler, maze);
this.test('PacMan entity created', gameEngine.getPacMan() !== null, 'GameEngine');
this.test('Ghosts created', gameEngine.getGhosts().length > 0, 'GameEngine');
this.test('GhostAI system created', gameEngine.getGhostAI() !== null, 'GameEngine');
// Test game state management
const initialScore = gameEngine.gameState.score;
gameEngine.addScore(100);
this.test('Score system works', gameEngine.gameState.score === initialScore + 100, 'GameEngine');
// Test power pellet activation
gameEngine.activatePowerPellet(5000);
this.test('Power pellet activation works', gameEngine.gameState.powerPelletActive === true, 'GameEngine');
// Test performance metrics
const metrics = gameEngine.getPerformanceMetrics();
this.test('Performance metrics available', metrics && typeof metrics === 'object', 'GameEngine');
} catch (error) {
this.test('GameEngine basic functionality', false, 'GameEngine');
console.error('GameEngine test error:', error);
}
}
testCollisionDetection() {
try {
const pacman = new PacMan(10, 10);
const ghost = new Ghost(10, 10, 'red', 'aggressive');
// Test collision at same position
this.test('Collision detected at same position', ghost.checkCollisionWithPacman(pacman), 'Collision');
// Test no collision when far apart
ghost.setPosition(15, 15);
this.test('No collision when far apart', !ghost.checkCollisionWithPacman(pacman), 'Collision');
// Test collision within radius
ghost.setPosition(10.3, 10.3);
this.test('Collision detected within radius', ghost.checkCollisionWithPacman(pacman), 'Collision');
// Test GhostAI collision system
const ghostAI = new GhostAI();
const maze = new MazeGraph(20, 15);
maze.createDemoMaze();
const ghostConfigs = [
{ x: 10, y: 10, color: 'red', personality: 'aggressive' },
{ x: 15, y: 10, color: 'pink', personality: 'ambush' }
];
ghostAI.initializeGhosts(ghostConfigs, maze);
const collisions = ghostAI.checkCollisions(pacman);
this.test('GhostAI collision detection works', Array.isArray(collisions), 'Collision');
this.test('Collision detected with overlapping ghost', collisions.length > 0, 'Collision');
} catch (error) {
this.test('Collision detection functionality', false, 'Collision');
console.error('Collision detection test error:', error);
}
}
testInputHandling() {
try {
const inputHandler = new InputHandler();
this.test('InputHandler constructor works', inputHandler !== null, 'Input');
this.test('InputHandler has direction mappings', inputHandler.directions && typeof inputHandler.directions === 'object', 'Input');
this.test('InputHandler starts with no direction', inputHandler.currentDirection.name === 'none', 'Input');
// Test direction setting
const testDirection = { x: 1, y: 0, name: 'right' };
inputHandler.setCurrentDirection(testDirection);
this.test('Can set current direction', inputHandler.getCurrentDirection().name === 'right', 'Input');
// Test input state
const inputState = inputHandler.getInputState();
this.test('Input state available', inputState && typeof inputState === 'object', 'Input');
// Test reset functionality
inputHandler.reset();
this.test('Input handler reset works', inputHandler.getCurrentDirection().name === 'none', 'Input');
} catch (error) {
this.test('Input handling functionality', false, 'Input');
console.error('Input handling test error:', error);
}
}
testRenderingSystem() {
try {
const renderer = new Renderer(this.canvas, this.ctx);
this.test('Renderer constructor works', renderer !== null, 'Renderer');
this.test('Renderer has canvas reference', renderer.canvas === this.canvas, 'Renderer');
this.test('Renderer has color palette', renderer.colors && typeof renderer.colors === 'object', 'Renderer');
// Test basic rendering operations
renderer.clear();
this.test('Canvas clear operation works', true, 'Renderer');
// Test maze rendering
const maze = new MazeGraph(20, 15);
maze.createDemoMaze();
renderer.drawMaze(maze);
this.test('Maze rendering completes', true, 'Renderer');
// Test entity rendering
renderer.drawPacMan(10, 10, { name: 'right' }, 0);
this.test('PacMan rendering completes', true, 'Renderer');
renderer.drawGhost(5, 5, '#ff0000', 'chase', 0);
this.test('Ghost rendering completes', true, 'Renderer');
// Test animation update
const initialFrame = renderer.animationFrame;
renderer.updateAnimation();
this.test('Animation update works', renderer.animationFrame > initialFrame, 'Renderer');
} catch (error) {
this.test('Rendering system functionality', false, 'Renderer');
console.error('Rendering system test error:', error);
}
}
generateSummary() {
const totalTests = this.results.length;
const passedTests = this.results.filter(r => r.passed).length;
const failedTests = totalTests - passedTests;
const passRate = ((passedTests / totalTests) * 100).toFixed(1);
const sections = {};
this.results.forEach(result => {
if (!sections[result.section]) {
sections[result.section] = { total: 0, passed: 0 };
}
sections[result.section].total++;
if (result.passed) {
sections[result.section].passed++;
}
});
let summaryHTML = `
<h2>Checkpoint 8 Test Summary</h2>
<div style="font-size: 18px; margin: 10px 0;">
<strong>Overall: ${passedTests}/${totalTests} tests passed (${passRate}%)</strong>
</div>
`;
if (passRate >= 90) {
summaryHTML += `<div style="color: #00ff00; font-weight: bold;">✓ CHECKPOINT 8 PASSED - Core AI and gameplay mechanics are working correctly!</div>`;
} else if (passRate >= 75) {
summaryHTML += `<div style="color: #ffff00; font-weight: bold;">⚠ CHECKPOINT 8 PARTIAL - Most systems working, some issues need attention</div>`;
} else {
summaryHTML += `<div style="color: #ff0000; font-weight: bold;">✗ CHECKPOINT 8 FAILED - Significant issues need to be resolved</div>`;
}
summaryHTML += '<h3>Results by Section:</h3>';
Object.entries(sections).forEach(([section, stats]) => {
const sectionRate = ((stats.passed / stats.total) * 100).toFixed(1);
const color = sectionRate >= 90 ? '#00ff00' : sectionRate >= 75 ? '#ffff00' : '#ff0000';
summaryHTML += `<div style="color: ${color};">${section}: ${stats.passed}/${stats.total} (${sectionRate}%)</div>`;
});
if (failedTests > 0) {
summaryHTML += '<h3>Failed Tests:</h3>';
this.results.filter(r => !r.passed).forEach(result => {
summaryHTML += `<div style="color: #ff0000;">✗ [${result.section}] ${result.name}</div>`;
});
}
this.summaryDiv.innerHTML = summaryHTML;
console.log(`Checkpoint 8 Complete: ${passedTests}/${totalTests} tests passed (${passRate}%)`);
}
}
// Run tests when page loads
window.addEventListener('load', async () => {
const tester = new CheckpointTester();
await tester.runAllTests();
});
</script>
</body>
</html>