设计一个迷宫游戏,要求完成以下功能: 1,根据用户选择的游戏难易程度,动态生成并输出(简单级别:迷宫规模为10*10,中等:迷宫规模为50*50,困难:迷宫规模为100*100) 2,,迷宫地图由0和1构成的n维方针便是,0便是可走,1表示障碍物(输出迷宫地图时,障碍物用黑色方块表示,通道块用白色方块表示) 3,程序运行时,用户在终端输入每步要走的位置(坐标),程序应在迷宫地图觞用箭头显示出用户所走的这一步(即:用户每走一步,需要更新一下地图) 4,当用户选择帮助功能时,应给出迷宫的一种解法(分别使用栈和队列的方法求出迷宫的一个解,注意:用户选择的帮助位置指的是用户当前所处的位置,程序应给出从当前位置处的迷宫解) 5,迷宫入口固定在左上角,出口固定在右下角(用c语言表示)
时间: 2024-03-09 09:47:18 浏览: 26
好的,以下是一个简单的迷宫游戏,实现了上述功能:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#define WALL 1
#define PATH 0
#define ROW 10
#define COL 10
typedef struct {
int row;
int col;
} Position;
int maze[ROW][COL];
int visited[ROW][COL];
Position stack[ROW * COL];
int top = -1;
Position queue[ROW * COL];
int front = 0, rear = 0;
void initMaze(int level) {
int row, col;
switch (level) {
case 1:
row = 10;
col = 10;
break;
case 2:
row = 50;
col = 50;
break;
case 3:
row = 100;
col = 100;
break;
default:
row = 10;
col = 10;
break;
}
srand((unsigned) time(NULL));
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (i == 0 || i == row - 1 || j == 0 || j == col - 1) {
maze[i][j] = WALL;
} else {
maze[i][j] = rand() % 2;
}
visited[i][j] = 0;
}
}
maze[0][0] = PATH;
maze[row - 1][col - 1] = PATH;
}
void printMaze() {
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
if (maze[i][j] == WALL) {
printf("\033[40m \033[0m");
} else {
if (visited[i][j] == 0) {
printf("\033[47m \033[0m");
} else {
printf("\033[44m \033[0m");
}
}
}
printf("\n");
}
}
void push(Position pos) {
stack[++top] = pos;
}
Position pop() {
return stack[top--];
}
void enqueue(Position pos) {
queue[rear++] = pos;
}
Position dequeue() {
return queue[front++];
}
void dfs() {
Position curPos = {0, 0};
push(curPos);
visited[curPos.row][curPos.col] = 1;
while (top >= 0) {
curPos = pop();
if (curPos.row == ROW - 1 && curPos.col == COL - 1) {
break;
}
if (curPos.col + 1 < COL && maze[curPos.row][curPos.col + 1] == PATH && visited[curPos.row][curPos.col + 1] == 0) {
Position nextPos = {curPos.row, curPos.col + 1};
push(nextPos);
visited[nextPos.row][nextPos.col] = 1;
}
if (curPos.row + 1 < ROW && maze[curPos.row + 1][curPos.col] == PATH && visited[curPos.row + 1][curPos.col] == 0) {
Position nextPos = {curPos.row + 1, curPos.col};
push(nextPos);
visited[nextPos.row][nextPos.col] = 1;
}
if (curPos.col - 1 >= 0 && maze[curPos.row][curPos.col - 1] == PATH && visited[curPos.row][curPos.col - 1] == 0) {
Position nextPos = {curPos.row, curPos.col - 1};
push(nextPos);
visited[nextPos.row][nextPos.col] = 1;
}
if (curPos.row - 1 >= 0 && maze[curPos.row - 1][curPos.col] == PATH && visited[curPos.row - 1][curPos.col] == 0) {
Position nextPos = {curPos.row - 1, curPos.col};
push(nextPos);
visited[nextPos.row][nextPos.col] = 1;
}
}
while (top >= 0) {
pop();
}
}
void bfs() {
Position curPos = {0, 0};
enqueue(curPos);
visited[curPos.row][curPos.col] = 1;
while (front < rear) {
curPos = dequeue();
if (curPos.row == ROW - 1 && curPos.col == COL - 1) {
break;
}
if (curPos.col + 1 < COL && maze[curPos.row][curPos.col + 1] == PATH && visited[curPos.row][curPos.col + 1] == 0) {
Position nextPos = {curPos.row, curPos.col + 1};
enqueue(nextPos);
visited[nextPos.row][nextPos.col] = 1;
}
if (curPos.row + 1 < ROW && maze[curPos.row + 1][curPos.col] == PATH && visited[curPos.row + 1][curPos.col] == 0) {
Position nextPos = {curPos.row + 1, curPos.col};
enqueue(nextPos);
visited[nextPos.row][nextPos.col] = 1;
}
if (curPos.col - 1 >= 0 && maze[curPos.row][curPos.col - 1] == PATH && visited[curPos.row][curPos.col - 1] == 0) {
Position nextPos = {curPos.row, curPos.col - 1};
enqueue(nextPos);
visited[nextPos.row][nextPos.col] = 1;
}
if (curPos.row - 1 >= 0 && maze[curPos.row - 1][curPos.col] == PATH && visited[curPos.row - 1][curPos.col] == 0) {
Position nextPos = {curPos.row - 1, curPos.col};
enqueue(nextPos);
visited[nextPos.row][nextPos.col] = 1;
}
}
while (front < rear) {
dequeue();
}
}
void help(Position curPos) {
visited[curPos.row][curPos.col] = 1;
if (curPos.row == ROW - 1 && curPos.col == COL - 1) {
printMaze();
return;
}
if (curPos.col + 1 < COL && maze[curPos.row][curPos.col + 1] == PATH && visited[curPos.row][curPos.col + 1] == 0) {
Position nextPos = {curPos.row, curPos.col + 1};
help(nextPos);
}
if (curPos.row + 1 < ROW && maze[curPos.row + 1][curPos.col] == PATH && visited[curPos.row + 1][curPos.col] == 0) {
Position nextPos = {curPos.row + 1, curPos.col};
help(nextPos);
}
if (curPos.col - 1 >= 0 && maze[curPos.row][curPos.col - 1] == PATH && visited[curPos.row][curPos.col - 1] == 0) {
Position nextPos = {curPos.row, curPos.col - 1};
help(nextPos);
}
if (curPos.row - 1 >= 0 && maze[curPos.row - 1][curPos.col] == PATH && visited[curPos.row - 1][curPos.col] == 0) {
Position nextPos = {curPos.row - 1, curPos.col};
help(nextPos);
}
}
int main() {
int level;
printf("请选择游戏难易程度:1(简单) 2(中等) 3(困难)\n");
scanf("%d", &level);
initMaze(level);
printMaze();
while (1) {
int x, y;
printf("请输入要走的位置(坐标):\n");
scanf("%d %d", &x, &y);
if (x < 0 || x >= ROW || y < 0 || y >= COL || maze[x][y] == WALL) {
printf("无法到达该位置,请重新输入!\n");
continue;
}
visited[x][y] = 1;
printMaze();
if (x == ROW - 1 && y == COL - 1) {
printf("恭喜你,成功到达终点!\n");
break;
}
printf("请选择是否需要帮助(1-是,0-否):\n");
int flag;
scanf("%d", &flag);
if (flag) {
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
visited[i][j] = 0;
}
}
printf("正在寻找迷宫解,请稍等...\n");
dfs();
help((Position) {x, y});
bfs();
}
}
return 0;
}
```
其中,initMaze函数根据用户选择的游戏难易程度动态生成迷宫地图;printMaze函数输出迷宫地图,并将已经走过的位置用蓝色方块表示;dfs和bfs函数分别使用深度优先搜索和广度优先搜索求解迷宫,并将求解过程中经过的位置标记为已访问;help函数根据当前位置开始搜索迷宫的解,并输出一种解法。在主函数中,根据用户输入的坐标更新地图,如果到达终点则结束游戏,否则根据用户选择是否需要帮助进行搜索。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)