#include <stdio.h>
#define COL 6
#define ROW 6
void printMap(char map[ROW][COL]){
for (int i=0; i<ROW; i++) {
for (int j=0; j<COL; j++) {
printf("%c",map[i][j]);
}
printf("\n");
}
}
void personMove(char map[ROW][COL],int oldX,int oldY,int newX,int newY){
char temp;
temp = map[oldX][oldY];
map[oldX][oldY]=map[newX][newY];
map[newX][newY]=temp;
}
int main(int argc, const char * argv[]) {
//定义地图
char map[ROW][COL]={
{'#','#','#','#','#','#'},
{'#','0','#','#',' ',' '},
{'#',' ','#','#',' ','#'},
{'#',' ',' ','#',' ','#'},
{'#','#',' ',' ',' ','#'},
{'#','#','#','#','#','#'},
};
//保存用户输入的方向
char derection;
//定义变量保存小人当前的位置
int currentX=1;
int currentY=1;
//定义变量 保存路
char street=' ';
//先 打印一遍地图
printMap(map);
printf("请控制小人移动:w.上 s.下 a.左 d.右 q.退出\n" );
while (1) {
scanf("%c",&derection);
getchar();//scanf("%c",&ch);//吸收多余的\n
switch (derection) {
case 'w':
case 'W':
if (map[currentX-1][currentY]==street) {
personMove(map,currentX,currentY,currentX-1,currentY);
currentX--;
}
break;
case 's':
case 'S':
if (map[currentX+1][currentY]==street) {
personMove(map,currentX,currentY,currentX+1,currentY);
currentX++;
}
break;
case 'a':
case 'A':
if (map[currentX][currentY-1]==street) {
personMove(map,currentX,currentY,currentX,currentY-1);
currentY--;
}
break;
case 'd':
case 'D':
if (map[currentX][currentY+1]==street) {
personMove(map,currentX,currentY,currentX,currentY+1);
currentY++;
}
break;
case 'q':
case 'Q':
return 0;
break;
default:
break;
}
//重绘地图
printMap(map);
if (currentY==5) {
printf("恭喜你!走出迷宫~(づ ̄ 3 ̄)づ\n");
break;
}
}
return 0;
} |
|