//
// main.c
// 迷宫游戏代码实现
// #include <stdio.h>
#define ROW 6 //宏定义行
#define COL 6 //宏定义列 /**
* 打印地图
*
* @param arr 地图数组
*/
void print_arr (char arr[ROW][COL]) {
for (int i = 0; i < ROW; i ++) {
for (int j = 0; j < COL; j ++) {
printf("%c", arr[i][j]);
}
printf("\n");
}
}
int main(int argc, const char * argv[]) {
char map[ROW][COL] = {{'#', '#', '#', '#', '#', '#'},
{'#', '@', '#', '#', ' ', ' '},
{'#', ' ', '#', '#', ' ', '#'},
{'#', ' ', ' ', '#', ' ', '#'},
{'#', '#', ' ', ' ', ' ', '#'},
{'#', '#', '#', '#', '#', '#'}}; print_arr(map);
printf("迷宫游戏:w.上, s.下, a.左, d.右 q.退出\n");
char direction, ch, street = ' ', people = '@';
int currentX = 1, currentY = 1;
while (1) {
scanf("%c", &direction);
scanf("%c", &ch);
switch (direction) {
case 'w':
case 'W':
if (map[currentX-1][currentY] == street) {
map[currentX-1][currentY] = people;
map[currentX][currentY] = ' ';
currentX--;
}
break;
case 's':
case 'S':
if (map[currentX+1][currentY] == street) {
map[currentX+1][currentY] = people;
map[currentX][currentY] = ' ';
currentX++;
}
break;
case 'a':
case 'A':
if (map[currentX][currentY-1] == street) {
map[currentX][currentY-1] = people;
map[currentX][currentY] = ' ';
currentY--;
}
break;
case 'd':
case 'D':
if (map[currentX][currentY+1] == street) {
map[currentX][currentY+1] = people;
map[currentX][currentY] = ' ';
currentY++;
}
break;
case 'q':
case 'Q':
printf("退出游戏\n");
return 0;
break;
}
print_arr(map);
if (currentY == 5) {
printf("恭喜你,走出迷宫!");
break;
}
}
return 0;
}