-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_collision_unit.html
More file actions
348 lines (275 loc) · 12.7 KB
/
test_collision_unit.html
File metadata and controls
348 lines (275 loc) · 12.7 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Collision Detection Unit Tests</title>
<style>
body {
margin: 0;
padding: 20px;
background: #000;
color: #00ff00;
font-family: monospace;
}
.test-result {
padding: 5px;
margin: 5px 0;
}
.pass {
color: #00ff00;
}
.fail {
color: #ff0000;
}
.section {
margin: 20px 0;
border: 1px solid #333;
padding: 10px;
}
</style>
</head>
<body>
<h2>Ghost-PacMan Collision Detection Unit Tests</h2>
<div id="testResults"></div>
<!-- Include 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/gameEngine.js"></script>
<script>
const results = [];
function assert(condition, testName) {
const passed = condition;
results.push({ testName, passed });
const resultDiv = document.createElement('div');
resultDiv.className = `test-result ${passed ? 'pass' : 'fail'}`;
resultDiv.textContent = `${passed ? '✓' : '✗'} ${testName}`;
document.getElementById('testResults').appendChild(resultDiv);
return passed;
}
function runTests() {
console.log('Running collision detection tests...');
// Test 1: Ghost collision detection method
testGhostCollisionMethod();
// Test 2: GhostAI checkCollisions method
testGhostAICollisionCheck();
// Test 3: Normal mode collision handling
testNormalModeCollision();
// Test 4: Power mode collision handling
testPowerModeCollision();
// Test 5: Multiple ghost collision
testMultipleGhostCollision();
// Test 6: No collision when far apart
testNoCollision();
// Summary
const totalTests = results.length;
const passedTests = results.filter(r => r.passed).length;
const summaryDiv = document.createElement('div');
summaryDiv.className = 'section';
summaryDiv.innerHTML = `<h3>Summary: ${passedTests}/${totalTests} tests passed</h3>`;
document.getElementById('testResults').appendChild(summaryDiv);
console.log(`Tests complete: ${passedTests}/${totalTests} passed`);
}
function testGhostCollisionMethod() {
const section = document.createElement('div');
section.className = 'section';
section.innerHTML = '<h3>Test 1: Ghost.checkCollisionWithPacman()</h3>';
document.getElementById('testResults').appendChild(section);
const ghost = new Ghost(10, 10, 'red', 'aggressive');
const pacman = new PacMan(10, 10);
// Test collision at same position
assert(
ghost.checkCollisionWithPacman(pacman),
'Ghost and PacMan at same position should collide'
);
// Test collision at close positions
pacman.setPosition(10.3, 10.3);
assert(
ghost.checkCollisionWithPacman(pacman),
'Ghost and PacMan within collision radius should collide'
);
// Test no collision when far apart
pacman.setPosition(15, 15);
assert(
!ghost.checkCollisionWithPacman(pacman),
'Ghost and PacMan far apart should not collide'
);
}
function testGhostAICollisionCheck() {
const section = document.createElement('div');
section.className = 'section';
section.innerHTML = '<h3>Test 2: GhostAI.checkCollisions()</h3>';
document.getElementById('testResults').appendChild(section);
const ghostAI = new GhostAI();
const mazeGraph = new MazeGraph(20, 15);
mazeGraph.createDemoMaze();
const ghostConfigs = [
{ x: 10, y: 10, color: 'red', personality: 'aggressive' },
{ x: 15, y: 10, color: 'pink', personality: 'ambush' }
];
ghostAI.initializeGhosts(ghostConfigs, mazeGraph);
const pacman = new PacMan(10, 10);
// Test collision with one ghost
const collisions1 = ghostAI.checkCollisions(pacman);
assert(
collisions1.length === 1,
'Should detect collision with one ghost'
);
// Test collision with multiple ghosts
pacman.setPosition(15, 10);
const collisions2 = ghostAI.checkCollisions(pacman);
assert(
collisions2.length === 1,
'Should detect collision with second ghost'
);
// Test no collision
pacman.setPosition(5, 5);
const collisions3 = ghostAI.checkCollisions(pacman);
assert(
collisions3.length === 0,
'Should detect no collisions when far from all ghosts'
);
}
function testNormalModeCollision() {
const section = document.createElement('div');
section.className = 'section';
section.innerHTML = '<h3>Test 3: Normal Mode Collision Handling</h3>';
document.getElementById('testResults').appendChild(section);
const canvas = document.createElement('canvas');
canvas.width = 800;
canvas.height = 600;
const gameEngine = new GameEngine(canvas);
const mazeGraph = new MazeGraph(20, 15);
mazeGraph.createDemoMaze();
const inputHandler = new InputHandler();
gameEngine.initializeEntities(inputHandler, mazeGraph);
const initialLives = gameEngine.gameState.lives;
// Position PacMan and ghost at same location
const pacman = gameEngine.getPacMan();
const ghosts = gameEngine.getGhosts();
pacman.setPosition(10, 10);
ghosts[0].setPosition(10, 10);
// Ensure power mode is off
gameEngine.gameState.powerPelletActive = false;
// Check collision
const collisions = gameEngine.getGhostAI().checkCollisions(pacman);
gameEngine.handleGhostCollisions(collisions);
assert(
gameEngine.gameState.lives === initialLives - 1,
'Normal mode collision should reduce lives by 1'
);
}
function testPowerModeCollision() {
const section = document.createElement('div');
section.className = 'section';
section.innerHTML = '<h3>Test 4: Power Mode Collision Handling</h3>';
document.getElementById('testResults').appendChild(section);
const canvas = document.createElement('canvas');
canvas.width = 800;
canvas.height = 600;
const gameEngine = new GameEngine(canvas);
const mazeGraph = new MazeGraph(20, 15);
mazeGraph.createDemoMaze();
const inputHandler = new InputHandler();
gameEngine.initializeEntities(inputHandler, mazeGraph);
const initialScore = gameEngine.gameState.score;
const initialLives = gameEngine.gameState.lives;
// Enable power mode
gameEngine.activatePowerPellet(10000);
// Position PacMan and ghost at same location
const pacman = gameEngine.getPacMan();
const ghosts = gameEngine.getGhosts();
const ghostHomeX = ghosts[0].homePosition.x;
const ghostHomeY = ghosts[0].homePosition.y;
pacman.setPosition(10, 10);
ghosts[0].setPosition(10, 10);
// Check collision
const collisions = gameEngine.getGhostAI().checkCollisions(pacman);
gameEngine.handleGhostCollisions(collisions);
assert(
gameEngine.gameState.score > initialScore,
'Power mode collision should increase score'
);
assert(
gameEngine.gameState.lives === initialLives,
'Power mode collision should not reduce lives'
);
assert(
ghosts[0].position.x === ghostHomeX && ghosts[0].position.y === ghostHomeY,
'Ghost should be reset to home position after being eaten'
);
}
function testMultipleGhostCollision() {
const section = document.createElement('div');
section.className = 'section';
section.innerHTML = '<h3>Test 5: Multiple Ghost Collision</h3>';
document.getElementById('testResults').appendChild(section);
const canvas = document.createElement('canvas');
canvas.width = 800;
canvas.height = 600;
const gameEngine = new GameEngine(canvas);
const mazeGraph = new MazeGraph(20, 15);
mazeGraph.createDemoMaze();
const inputHandler = new InputHandler();
gameEngine.initializeEntities(inputHandler, mazeGraph);
const initialScore = gameEngine.gameState.score;
// Enable power mode
gameEngine.activatePowerPellet(10000);
// Position PacMan and multiple ghosts at same location
const pacman = gameEngine.getPacMan();
const ghosts = gameEngine.getGhosts();
pacman.setPosition(10, 10);
ghosts[0].setPosition(10, 10);
ghosts[1].setPosition(10, 10);
// Check collision
const collisions = gameEngine.getGhostAI().checkCollisions(pacman);
gameEngine.handleGhostCollisions(collisions);
assert(
collisions.length === 2,
'Should detect collision with 2 ghosts'
);
assert(
gameEngine.gameState.score > initialScore + 200,
'Multiple ghost collision should award bonus points'
);
}
function testNoCollision() {
const section = document.createElement('div');
section.className = 'section';
section.innerHTML = '<h3>Test 6: No Collision Detection</h3>';
document.getElementById('testResults').appendChild(section);
const canvas = document.createElement('canvas');
canvas.width = 800;
canvas.height = 600;
const gameEngine = new GameEngine(canvas);
const mazeGraph = new MazeGraph(20, 15);
mazeGraph.createDemoMaze();
const inputHandler = new InputHandler();
gameEngine.initializeEntities(inputHandler, mazeGraph);
const initialLives = gameEngine.gameState.lives;
// Position PacMan and ghosts far apart
const pacman = gameEngine.getPacMan();
const ghosts = gameEngine.getGhosts();
pacman.setPosition(5, 5);
ghosts.forEach((ghost, i) => {
ghost.setPosition(15 + i, 15);
});
// Check collision
const collisions = gameEngine.getGhostAI().checkCollisions(pacman);
assert(
collisions.length === 0,
'Should not detect collision when entities are far apart'
);
assert(
gameEngine.gameState.lives === initialLives,
'Lives should remain unchanged when no collision'
);
}
// Run tests when page loads
window.addEventListener('load', runTests);
</script>
</body>
</html>