c++迷宫游戏 用c++代码输出
时间: 2023-08-04 10:10:34 浏览: 84
好的,以下是一个简单的 C 语言实现的迷宫游戏:
```c
#include <stdio.h>
#define ROW 10
#define COL 10
int map[ROW][COL] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};
int main() {
int x = 1, y = 1;
int end_x = 8, end_y = 8;
int found = 0;
map[x][y] = 2;
while (!found) {
if (x == end_x && y == end_y) {
found = 1;
break;
}
if (map[x - 1][y] == 0) {
map[x - 1][y] = 2;
x--;
} else if (map[x + 1][y] == 0) {
map[x + 1][y] = 2;
x++;
} else if (map[x][y - 1] == 0) {
map[x][y - 1] = 2;
y--;
} else if (map[x][y + 1] == 0) {
map[x][y + 1] = 2;
y++;
} else {
map[x][y] = 3;
if (map[x - 1][y] == 2) {
x--;
} else if (map[x + 1][y] == 2) {
x++;
} else if (map[x][y - 1] == 2) {
y--;
} else if (map[x][y + 1] == 2) {
y++;
}
}
}
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
printf("%d", map[i][j]);
}
printf("\n");
}
return 0;
}
```
这是一个基于二维数组的简单迷宫游戏,其中 1 代表墙,0 代表通路,2 代表已经走过的路,3 代表无路可走。程序会自动从左上角出发,走到右下角的通路。
阅读全文