analysis.h
#ifndef ANALYSIS_H
#define ANALYSIS_H
void saveResult_PlayerVsAI(const char *mode, int playerSteps, double playerTime,
const char *aiName, int aiPathLen, int aiExpanded, double aiTime);
#endif
analysis.c
#include "analysis.h"
#include <stdio.h>
void saveResult_PlayerVsAI(const char *mode, int playerSteps, double playerTime,
const char *aiName, int aiPathLen, int aiExpanded, double aiTime) {
FILE *fp = fopen("result.txt", "a");
if (!fp) return;
fprintf(fp, "Mode: %s\n", mode);
fprintf(fp, "PlayerSteps PlayerTime(s): %d %.2f\n", playerSteps, playerTime);
fprintf(fp, "%s PathLen Expanded Time(ms): %s %d %d %.2f\n", aiName, aiName, aiPathLen, aiExpanded, aiTime);
if (playerTime * 1000 < aiTime) fprintf(fp, "Winner: Player\n");
else fprintf(fp, "Winner: AI\n");
fprintf(fp, "-----------------------------\n");
fclose(fp);
}
main.c
#include <stdio.h>
#include <time.h>
#include "maze_gen.h"
#include "search.h"
#include "player.h"
#include "display.h"
#include "analysis.h"
int main() {
Maze maze;
initMaze(&maze, 30, 30); // 30x30 固定
generateMaze(&maze);
printf("=== Maze Escape AI (30x30) ===\n");
printf("Note: Player uses W/A/S/D then Enter. AI will animate its path.\n\n");
displayMaze(&maze);
printf("\nSelect Mode:\n1. Player Mode\n2. AI Mode (A*)\n3. Player vs AI (challenge)\n> ");
int mode;
if (scanf("%d", &mode) != 1) return 0;
// consume newline for later fgets
getchar();
if (mode == 1) {
int steps; double ptime;
playerPlay(&maze, &steps, &ptime);
printf("Player result: %d steps, %.2f s\n", steps, ptime);
saveResult_PlayerVsAI("PlayerOnly", steps, ptime, "None", 0, 0, 0.0);
} else if (mode == 2) {
int path[2000][2];
int pathLen = 0, expanded = 0;
clock_t t0 = clock();
pathLen = AStar_Search(&maze, &pathLen, &expanded, path, 2000);
clock_t t1 = clock();
double ms = (double)(t1 - t0) / CLOCKS_PER_SEC * 1000.0;
if (pathLen > 0) {
// animate path
animateAIPath(&maze, path, pathLen, 25); // 25 ms per step
printf("\nA* Path Length: %d | Expanded: %d | Time: %.2f ms\n", pathLen, expanded, ms);
saveResult_PlayerVsAI("AIOnly", 0, 0.0, "A*", pathLen, expanded, ms);
} else {
printf("A* did not find a path.\n");
}
} else if (mode == 3) {
int psteps; double ptime;
playerPlay(&maze, &psteps, &ptime);
// Re-generate maze grid for AI (player moved dots didn't alter original corridors)
// For simplicity we regenerate new maze with same dimensions (start, end same)
// If you prefer AI to see the exact same maze, ensure player didn't modify maze cells.
// Here we'll keep the same maze but reset any '.' marks from player (player didn't mark)
int path[2000][2];
int pathLen = 0, expanded = 0;
// run A*
clock_t t0 = clock();
pathLen = AStar_Search(&maze, &pathLen, &expanded, path, 2000);
clock_t t1 = clock();
double ms = (double)(t1 - t0) / CLOCKS_PER_SEC * 1000.0;
if (pathLen > 0) {
animateAIPath(&maze, path, pathLen, 25);
printf("\n=== Result Comparison ===\n");
printf("Player: %d steps, %.2f sec\n", psteps, ptime);
printf("AI(A*): %d steps, %.2f ms\n", pathLen, ms);
if (ptime * 1000.0 < ms) printf("🏆 Winner: Player!\n");
else printf("🤖 Winner: AI!\n");
saveResult_PlayerVsAI("PlayerVsAI", psteps, ptime, "A*", pathLen, expanded, ms);
} else {
printf("A* did not find a path.\n");
}
} else {
printf("Invalid mode.\n");
}
freeMaze(&maze);
return 0;
}