#include <stdio.h>
#define COL 6
#define ROW 6
int main(int argc, const char * argv[]) {
//定义地图变量 坐标变量 方向变量 路的变量
// 定义地图变量
char map[ROW][COL]={
{'#','#','#','#','#','#'},
{'#','O','#','#',' ',' '},
{'#',' ','#','#',' ','#'},
{'#',' ',' ','#',' ','#'},
{'#','#',' ',' ',' ','#'},
{'#','#','#','#','#','#'}
};
// 定义小人坐标变量
int currentx = 1;
int currenty = 1;
// 定义方向输入变量
char derection;
// 定义路的变量
char street = ' ';
for (int i = 0; i < ROW;i++) {
for (int j = 0; j < COL; j++) {
printf("%c",map[i][j]);
}
printf("\n");
}
// 提示玩家输入方向
printf("请输入方向: W 上 S 下 A 左 D 右\n");
// 判断是否是路, 是路则和路的进行交换
while (1) {
char ch;
scanf("%c",&derection);
scanf("%c",&ch);
switch (derection) {
case 'w':
case 'W':
if (map[currentx-1][currenty] == street) {
char temp;
temp = map[currentx][currenty];
map[currentx][currenty] = map[currentx-1][currenty];
map[currentx-1][currenty] = temp;
currentx--;}
break;
case 's':
case 'S':
if (map[currentx+1][currenty] == street) {
char temp;
temp = map[currentx][currenty];
map[currentx][currenty] = map[currentx+1][currenty];
map[currentx+1][currenty] = temp;
currentx++;}
break;
case 'a':
case 'A':
if (map[currentx][currenty-1] == street) {
char temp;
temp = map[currentx][currenty];
map[currentx][currenty] = map[currentx][currenty-1];
map[currentx][currenty-1] = temp;
currenty--;}
break;
case 'd':
case 'D':
if (map[currentx][currenty+1]) {
char temp;
temp = map[currentx][currenty];
map[currentx][currenty] = map[currentx][currenty+1];
map[currentx][currenty+1] = temp;
currenty++;}
break;
case 'q':
case 'Q':
return 0;
default:
break;
}
// 不是路什么也不操作
// 循环打出地图显示当前小人位置
for (int i=0; i<ROW; i++) {
for (int j=0; j<COL; j++) {
printf("%c",map[i][j]);
}
printf("\n");
}
// for(int i=0; i < ROW; i++){
//
// for (int j = 0; j < COL; j++) {
// printf("%c",map[i][j]);
// }
// printf("\n");
// }
if (currenty == 5) {
printf("哈哈!!恭喜你 通关了!!");
break;
}
}
// 判断是否通关,
return 0;
} |
|