#include <stdio.h>
#define row 6
#define col 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 swith(char map[row][col], int oldX, int oldY, int newX, int newY){
char temp = map[oldX][oldY];
map[oldX][oldY] = map[newX][newY];
map[newX][newY] = temp;
}
void movePerson(char m , char n){
char temp = m;
m = n;
n = temp;
}
int main(int argc, const char * argv[])
{
// 1.定义变量:地图,方向,坐标,小人位置
char map[row][col] ={
{'#','#','#','#','#','#'},
{'#','0','#','#',' ',' '},
{'#',' ','#','#',' ','#'},
{'#',' ',' ','#',' ','#'},
{'#','#',' ',' ',' ','#'},
{'#','#','#','#','#','#'}
};
// 路
int road = ' ';
// 方向
char direct;
// 小人当前的位置
int currentx = 1;
int currenty = 1;
// 2.打印地图,
printMap(map);
// 3.告诉用户怎么玩
printf("游戏玩法,w 向上 s 向左 x 向下 f向右,q 退出\n");
char ch ;
while(1){
// 4.接受用户的方向
scanf("%c",&direct);
scanf("%c",&ch);//去除\n
// 5.判断输入的方向
switch (direct) {
case 'W':
case 'w':
//6.判断小人是否移动,就是判断小人的位置是不是' '
if (map[currentx -1][currenty] == road) {
//7.开始交换小人与路的位置
//switch(map[currentx][currenty],map[currentx-1][currenty]);
swith(map, currentx, currenty, currentx-1, currenty);
currentx --;
}
break;
case 'X':
case 'x':
//6.判断小人是否移动,就是判断小人的位置是不是' '
if (map[currentx + 1][currenty] == road) {
//7.开始交换小人与路的位置
//movePerson(map[currentx][currenty],map[currentx+1][currenty]);
swith(map, currentx, currenty, currentx+1, currenty);
currentx ++;
}
break;
case 'S':
case 's':
//6.判断小人是否移动,就是判断小人的位置是不是' '
if (map[currentx][currenty-1] == road) {
//7.开始交换小人与路的位置
//movePerson(map[currentx][currenty],map[currentx][currenty-1]);
swith(map, currentx, currenty, currentx, currenty-1);
currenty --;
}
break;
case 'F':
case 'f':
//6.判断小人是否移动,就是判断小人的位置是不是' '
if (map[currentx][currenty+1] == road) {
//7.开始交换小人与路的位置
//movePerson(map[currentx][currenty],map[currentx][currenty+1]);
swith(map, currentx, currenty, currentx, currenty+1);
currenty ++;
}
break;
case 'q':
case 'Q':
return 0;
break;
default:
break;
}
// 6.打印完成的地图
printMap(map);
// 7.判断是否出来了
if (currenty == 5) {
printf("you win");
break;
}
}
printf("\n");
return 0;
}
标签:case,map,游戏,int,迷宫,char,currenty,currentx,简单 From: https://blog.51cto.com/u_12516227/6055868