-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ghost_ai.html
More file actions
254 lines (220 loc) · 10.5 KB
/
test_ghost_ai.html
File metadata and controls
254 lines (220 loc) · 10.5 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ghost AI Test</title>
<style>
body {
font-family: monospace;
background: #000;
color: #0f0;
padding: 20px;
}
canvas {
border: 1px solid #0f0;
background: #000;
}
.test-results {
margin-top: 20px;
}
.ghost-info {
margin: 10px 0;
padding: 10px;
border: 1px solid #333;
background: #111;
}
</style>
</head>
<body>
<h1>Ghost AI System Test</h1>
<canvas id="testCanvas" width="400" height="300"></canvas>
<div class="test-results" id="testResults"></div>
<div id="ghostInfo"></div>
<script src="js/mazeGraph.js"></script>
<script src="js/inputHandler.js"></script>
<script src="js/pacman.js"></script>
<script src="js/ghost.js"></script>
<script src="js/ghostAI.js"></script>
<script>
// Test the Ghost AI implementation
function runTests() {
const results = document.getElementById('testResults');
const ghostInfo = document.getElementById('ghostInfo');
const canvas = document.getElementById('testCanvas');
const ctx = canvas.getContext('2d');
let testsPassed = 0;
let totalTests = 0;
function test(name, condition) {
totalTests++;
if (condition) {
testsPassed++;
results.innerHTML += `<div style="color: #0f0;">✓ ${name}</div>`;
} else {
results.innerHTML += `<div style="color: #f00;">✗ ${name}</div>`;
}
}
// Create test environment
const maze = new MazeGraph(20, 15);
maze.createDemoMaze();
const pacman = new PacMan(10, 7);
const ghostAI = new 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();
// Basic tests
test('GhostAI initialized', ghostAI !== null);
test('Correct number of ghosts created', ghosts.length === 4);
test('Ghost colors assigned correctly',
ghosts[0].color === 'red' &&
ghosts[1].color === 'pink' &&
ghosts[2].color === 'cyan' &&
ghosts[3].color === 'orange'
);
test('Ghost personalities assigned correctly',
ghosts[0].personality === 'aggressive' &&
ghosts[1].personality === 'ambush' &&
ghosts[2].personality === 'patrol' &&
ghosts[3].personality === 'random'
);
// Test state machine
const redGhost = ghosts[0];
test('Ghost starts in scatter state', redGhost.currentState === 'scatter');
test('Ghost has valid state configuration', redGhost.stateConfig.scatter !== undefined);
test('Ghost can transition states', redGhost.stateConfig.scatter.canTransitionTo.includes('chase'));
// Test AI targeting
const gameState = {
powerPelletActive: false,
powerPelletTimer: 0,
score: 0,
lives: 3,
level: 1
};
// Update ghosts once
ghostAI.update(16.67, pacman, maze, gameState);
test('Ghosts have targets after update', ghosts.every(ghost => ghost.target !== null));
test('Ghost state info available', redGhost.getStateInfo().state !== undefined);
// Test collision detection
const collisions = ghostAI.checkCollisions(pacman);
test('Collision detection works', Array.isArray(collisions));
// Test performance metrics
const metrics = ghostAI.getPerformanceMetrics();
test('Performance metrics available', metrics.lastUpdateTime !== undefined);
// Visual test - draw ghosts and their states
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
const pixelSize = 20;
// Draw maze walls
maze.getAllWalls().forEach(wall => {
ctx.fillStyle = '#00f';
ctx.fillRect(wall.x * pixelSize, wall.y * pixelSize, pixelSize, pixelSize);
});
// Draw PacMan
const pacmanPos = pacman.getRenderPosition();
ctx.fillStyle = '#ff0';
ctx.beginPath();
ctx.arc(pacmanPos.x * pixelSize + pixelSize/2, pacmanPos.y * pixelSize + pixelSize/2, 8, 0, Math.PI * 2);
ctx.fill();
// Draw ghosts
const ghostColors = ['#f00', '#ffc0cb', '#00ffff', '#ffa500'];
ghosts.forEach((ghost, index) => {
const pos = ghost.getRenderPosition();
ctx.fillStyle = ghostColors[index];
ctx.fillRect(pos.x * pixelSize + 2, pos.y * pixelSize + 2, pixelSize - 4, pixelSize - 4);
// Draw target if available
if (ghost.target) {
ctx.strokeStyle = ghostColors[index];
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(pos.x * pixelSize + pixelSize/2, pos.y * pixelSize + pixelSize/2);
ctx.lineTo(ghost.target.x * pixelSize + pixelSize/2, ghost.target.y * pixelSize + pixelSize/2);
ctx.stroke();
}
});
// Show ghost information
ghostInfo.innerHTML = '<h3>Ghost States:</h3>';
ghosts.forEach(ghost => {
const stateInfo = ghost.getStateInfo();
ghostInfo.innerHTML += `
<div class="ghost-info">
<strong>${ghost.color.toUpperCase()} Ghost (${ghost.personality})</strong><br>
State: ${stateInfo.state.toUpperCase()}<br>
Target: ${stateInfo.target ? `(${stateInfo.target.x}, ${stateInfo.target.y})` : 'None'}<br>
Reason: ${stateInfo.reason}<br>
Position: (${ghost.position.x}, ${ghost.position.y})
</div>
`;
});
// Show results
results.innerHTML += `<div style="color: #ff0; margin-top: 10px;">Tests passed: ${testsPassed}/${totalTests}</div>`;
if (testsPassed === totalTests) {
results.innerHTML += `<div style="color: #0f0; font-weight: bold;">All tests passed! ✓</div>`;
} else {
results.innerHTML += `<div style="color: #f00; font-weight: bold;">Some tests failed! ✗</div>`;
}
// Show AI performance
results.innerHTML += `<div style="margin-top: 10px;">AI Performance:</div>`;
results.innerHTML += `<div>Last update time: ${metrics.lastUpdateTime.toFixed(2)}ms</div>`;
results.innerHTML += `<div>Average update time: ${metrics.averageUpdateTime.toFixed(2)}ms</div>`;
results.innerHTML += `<div>Ghost count: ${metrics.ghostCount}</div>`;
}
// Run tests when page loads
window.addEventListener('load', runTests);
// Update ghost states periodically for demonstration
setInterval(() => {
if (window.ghostAI && window.pacman && window.maze) {
const gameState = {
powerPelletActive: Math.random() < 0.1, // 10% chance of power mode
powerPelletTimer: 5000,
score: 0,
lives: 3,
level: 1
};
window.ghostAI.update(16.67, window.pacman, window.maze, gameState);
// Update display
const ghostInfo = document.getElementById('ghostInfo');
const ghosts = window.ghostAI.getGhosts();
ghostInfo.innerHTML = '<h3>Ghost States (Live Update):</h3>';
ghosts.forEach(ghost => {
const stateInfo = ghost.getStateInfo();
ghostInfo.innerHTML += `
<div class="ghost-info">
<strong>${ghost.color.toUpperCase()} Ghost (${ghost.personality})</strong><br>
State: ${stateInfo.state.toUpperCase()}<br>
Target: ${stateInfo.target ? `(${stateInfo.target.x}, ${stateInfo.target.y})` : 'None'}<br>
Reason: ${stateInfo.reason}<br>
Position: (${ghost.position.x}, ${ghost.position.y})<br>
Timer: ${(stateInfo.stateTimer / 1000).toFixed(1)}s
</div>
`;
});
}
}, 1000);
// Make objects globally available for live updates
window.addEventListener('load', () => {
setTimeout(() => {
const maze = new MazeGraph(20, 15);
maze.createDemoMaze();
const pacman = new PacMan(10, 7);
const ghostAI = new GhostAI();
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);
window.maze = maze;
window.pacman = pacman;
window.ghostAI = ghostAI;
}, 100);
});
</script>
</body>
</html>