设计一个迷宫游戏,要求完成以下功能: 1,根据用户选择的游戏难易程度,动态生成并输出(简单级别:迷宫规模为10*10,中等:迷宫规模为50*50,困难:迷宫规模为100*100) 2,,迷宫地图由0和1构成的n维方针便是,0便是可走,1表示障碍物(输出迷宫地图时,障碍物用黑色方块表示,通道块用白色方块表示) 3,程序运行时,用户在终端输入每步要走的位置(坐标),程序应在迷宫地图觞用箭头显示出用户所走的这一步(即:用户每走一步,需要更新一下地图) 4,当用户选择帮助功能时,应给出迷宫的一种解法(分别使用栈和队列的方法求出迷宫的一个解,注意:用户选择的帮助位置指的是用户当前所处的位置,程序应给出从当前位置处的迷宫解) 5,迷宫入口固定在左上角,出口固定在右下角(用c语言表示)
时间: 2024-03-12 14:44:42 浏览: 47
好的,下面是C语言实现的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAZE_SIZE 10 // 迷宫规模
#define WALL 1 // 墙
#define ROAD 0 // 路
#define PATH 2 // 路径
// 定义坐标结构体
typedef struct {
int x;
int y;
} Point;
// 定义栈结构体
typedef struct {
Point *data;
int top;
} Stack;
// 初始化栈
void initStack(Stack *s, int size) {
s->data = (Point *)malloc(sizeof(Point) * size);
s->top = -1;
}
// 入栈
void push(Stack *s, Point p) {
s->top++;
s->data[s->top] = p;
}
// 出栈
Point pop(Stack *s) {
Point p = s->data[s->top];
s->top--;
return p;
}
// 判断栈是否为空
int isEmpty(Stack *s) {
return s->top == -1;
}
// 定义队列结构体
typedef struct {
Point *data;
int front;
int rear;
} Queue;
// 初始化队列
void initQueue(Queue *q, int size) {
q->data = (Point *)malloc(sizeof(Point) * size);
q->front = 0;
q->rear = -1;
}
// 入队
void enqueue(Queue *q, Point p) {
q->rear++;
q->data[q->rear] = p;
}
// 出队
Point dequeue(Queue *q) {
Point p = q->data[q->front];
q->front++;
return p;
}
// 判断队列是否为空
int isQueueEmpty(Queue *q) {
return q->front > q->rear;
}
// 生成迷宫
void generateMaze(int maze[MAZE_SIZE][MAZE_SIZE]) {
srand(time(NULL));
int i, j;
// 初始化迷宫
for (i = 0; i < MAZE_SIZE; i++) {
for (j = 0; j < MAZE_SIZE; j++) {
maze[i][j] = WALL;
}
}
// 随机生成迷宫
for (i = 1; i < MAZE_SIZE - 1; i += 2) {
for (j = 1; j < MAZE_SIZE - 1; j += 2) {
maze[i][j] = ROAD;
if (i > 1) {
if (rand() % 2) {
maze[i - 1][j] = ROAD;
} else {
maze[i][j] = WALL;
}
}
if (j > 1) {
if (rand() % 2) {
maze[i][j - 1] = ROAD;
} else {
maze[i][j] = WALL;
}
}
}
}
}
// 输出迷宫
void printMaze(int maze[MAZE_SIZE][MAZE_SIZE]) {
int i, j;
for (i = 0; i < MAZE_SIZE; i++) {
for (j = 0; j < MAZE_SIZE; j++) {
if (maze[i][j] == WALL) {
printf("\033[40m \033[0m");
} else if (maze[i][j] == ROAD) {
printf("\033[47m \033[0m");
} else if (maze[i][j] == PATH) {
printf("\033[42m \033[0m");
}
}
printf("\n");
}
}
// 深度优先搜索
int dfs(int maze[MAZE_SIZE][MAZE_SIZE], Point start, Point end) {
Stack s;
initStack(&s, MAZE_SIZE * MAZE_SIZE);
push(&s, start);
while (!isEmpty(&s)) {
Point cur = pop(&s);
if (cur.x == end.x && cur.y == end.y) {
return 1;
}
if (maze[cur.x][cur.y] == PATH) {
continue;
}
maze[cur.x][cur.y] = PATH;
if (cur.y < MAZE_SIZE - 1 && maze[cur.x][cur.y + 1] == ROAD) {
push(&s, (Point){cur.x, cur.y + 1});
}
if (cur.x < MAZE_SIZE - 1 && maze[cur.x + 1][cur.y] == ROAD) {
push(&s, (Point){cur.x + 1, cur.y});
}
if (cur.y > 0 && maze[cur.x][cur.y - 1] == ROAD) {
push(&s, (Point){cur.x, cur.y - 1});
}
if (cur.x > 0 && maze[cur.x - 1][cur.y] == ROAD) {
push(&s, (Point){cur.x - 1, cur.y});
}
}
return 0;
}
// 广度优先搜索
int bfs(int maze[MAZE_SIZE][MAZE_SIZE], Point start, Point end) {
Queue q;
initQueue(&q, MAZE_SIZE * MAZE_SIZE);
enqueue(&q, start);
while (!isQueueEmpty(&q)) {
Point cur = dequeue(&q);
if (cur.x == end.x && cur.y == end.y) {
return 1;
}
if (maze[cur.x][cur.y] == PATH) {
continue;
}
maze[cur.x][cur.y] = PATH;
if (cur.x > 0 && maze[cur.x - 1][cur.y] == ROAD) {
enqueue(&q, (Point){cur.x - 1, cur.y});
}
if (cur.y > 0 && maze[cur.x][cur.y - 1] == ROAD) {
enqueue(&q, (Point){cur.x, cur.y - 1});
}
if (cur.x < MAZE_SIZE - 1 && maze[cur.x + 1][cur.y] == ROAD) {
enqueue(&q, (Point){cur.x + 1, cur.y});
}
if (cur.y < MAZE_SIZE - 1 && maze[cur.x][cur.y + 1] == ROAD) {
enqueue(&q, (Point){cur.x, cur.y + 1});
}
}
return 0;
}
int main() {
int maze[MAZE_SIZE][MAZE_SIZE];
Point start = {0, 0};
Point end = {MAZE_SIZE - 1, MAZE_SIZE - 1};
generateMaze(maze);
printMaze(maze);
int x, y;
while (1) {
printf("请输入坐标(x y):");
scanf("%d %d", &x, &y);
if (x == end.x && y == end.y) {
printf("恭喜你,已到达终点!\n");
break;
}
if (maze[x][y] == WALL) {
printf("该位置为障碍物,请重新输入!\n");
continue;
}
maze[x][y] = PATH;
printMaze(maze);
maze[x][y] = ROAD;
char c;
printf("是否需要帮助(y/n):");
scanf(" %c", &c);
if (c == 'y') {
int maze_copy[MAZE_SIZE][MAZE_SIZE];
int i, j;
for (i = 0; i < MAZE_SIZE; i++) {
for (j = 0; j < MAZE_SIZE; j++) {
maze_copy[i][j] = maze[i][j];
}
}
if (dfs(maze_copy, (Point){x, y}, end)) {
printf("迷宫有解(深度优先搜索)\n");
printMaze(maze_copy);
} else {
printf("迷宫无解(深度优先搜索)\n");
}
for (i = 0; i < MAZE_SIZE; i++) {
for (j = 0; j < MAZE_SIZE; j++) {
maze_copy[i][j] = maze[i][j];
}
}
if (bfs(maze_copy, (Point){x, y}, end)) {
printf("迷宫有解(广度优先搜索)\n");
printMaze(maze_copy);
} else {
printf("迷宫无解(广度优先搜索)\n");
}
}
}
return 0;
}
```
以上代码中使用了ANSI控制字符来输出彩色的迷宫地图,需要在支持ANSI控制字符的终端中运行才能正常显示。在Windows系统中,可以使用cmd或PowerShell来运行。
阅读全文