main.cpp
#include "function.h"
char map[10][10] =
{
{'#','#','#','#','#','#','#','#','#','#'},
{'#','0',' ',' ',' ','#',' ','#',' ',' '},
{'#','#',' ','#','#','#',' ','#',' ','#'},
{'#','#',' ',' ',' ',' ',' ','#',' ','#'},
{'#','#','#',' ','#','#',' ','#',' ','#'},
{'#',' ','#',' ',' ','#',' ','#',' ','#'},
{'#',' ','#',' ',' ','#',' ','#',' ','#'},
{'#',' ','#',' ',' ','#',' ','#',' ','#'},
{'#',' ',' ',' ',' ','#',' ',' ',' ','#'},
{'#','#','#','#','#','#','#','#','#','#'},
};
int startX = 1;
int startY = 1;
int endX = 1;
int endY = 9;
char direction = '0';
while (map[endX][endY] != '0') {
printMap(map);
scanf("\n%c", &direction);
switch (direction) {
case 'w':
if (map[startX - 1][startY] == ' ') {
map[startX - 1][startY] = '0';
map[startX][startY] = ' ';
startX = startX - 1;
} else {
printf("此路不通\n");
}
break;
case 's':
if (map[startX + 1][startY] == ' ') {
map[startX + 1][startY] = '0';
map[startX][startY] = ' ';
startX = startX + 1;
} else {
printf("此路不通\n");
}
break;
case 'a':
if (map[startX][startY - 1] == ' ') {
map[startX][startY - 1] = '0';
map[startX][startY] = ' ';
startY = startY - 1;
} else {
printf("此路不通\n");
}
break;
case 'd':
if (map[startX][startY + 1] == ' ') {
map[startX][startY + 1] = '0';
map[startX][startY] = ' ';
startY = startY + 1;
} else {
printf("此路不通\n");
}
break;
default:
printf("请输入正确方向!!!");
break;
}
}
printf("恭喜,成功通过迷宫!!!\n");
function.cpp
void printMap( char ( *map)[10] ) {
for (int i = 0; i < 10; i++) {
for (int j = 0 ; j < 10; j++) {
printf("%c", map[i][j]);
}
printf("\n");
}
printf("请按 wsad 进行上下左右方向的移动:\n");
}