C 程式架構模板
📂 main.c
#include <stdio.h>
#include <time.h>
#include "maze_gen.h"
#include "search.h"
#include "display.h"
#include "analysis.h"
int main() {
Maze maze;
initMaze(&maze, 20, 20);
generateMaze(&maze);
clock_t start, end;
int pathLength, expandedNodes;
printf("=== Maze Escape AI ===\n");
displayMaze(&maze);
printf("\nChoose algorithm: 1.DFS 2.BFS 3.A*\n> ");
int choice;
scanf("%d", &choice);
start = clock();
if (choice == 1)
DFS_Search(&maze, &pathLength, &expandedNodes);
else if (choice == 2)
BFS_Search(&maze, &pathLength, &expandedNodes);
else
AStar_Search(&maze, &pathLength, &expandedNodes);
end = clock();
double timeUsed = (double)(end - start) / CLOCKS_PER_SEC * 1000;
displayMaze(&maze);
saveResult(choice, pathLength, expandedNodes, timeUsed);
printf("\nPath Length: %d | Expanded Nodes: %d | Time: %.2f ms\n",
pathLength, expandedNodes, timeUsed);
return 0;
}
📂 search.c(演算法骨架)
#include "search.h"
#include <stdlib.h>
#include <math.h>
void DFS_Search(Maze *maze, int *pathLength, int *expanded) {
// 遞迴深度優先搜尋
}
void BFS_Search(Maze *maze, int *pathLength, int *expanded) {
// 使用 queue 實作廣度搜尋
}
void AStar_Search(Maze *maze, int *pathLength, int *expanded) {
// 開啟/關閉集合 + 優先佇列 (f = g + h)
}
int heuristic(int x, int y, int goalX, int goalY) {
return abs(x - goalX) + abs(y - goalY); // 曼哈頓距離
}
📂 maze_gen.c
#include "maze_gen.h"
#include <stdlib.h>
#include <time.h>
void generateMaze(Maze *maze) {
srand(time(NULL));
// 用 DFS 生成隨機迷宮
}
📂 analysis.c
#include "analysis.h"
#include <stdio.h>
void saveResult(int algorithm, int path, int expanded, double time) {
FILE *fp = fopen("result.txt", "a");
const char *name = (algorithm == 1) ? "DFS" :
(algorithm == 2) ? "BFS" : "A*";
fprintf(fp, "%s %d %d %.2f\n", name, path, expanded, time);
fclose(fp);
}