脑袋嗡嗡的,修的修的不知道修哪了,最后一跑,成立!
#include<stdio.h>
int main(){
char map[6][6]={
{'#','#','#','#','#','#'},
{'D',' ','#','#',' ',' '},
{'#',' ','#','#',' ','#'},
{'#',' ','#',' ',' ','#'},
{'#',' ',' ',' ','#','#'},
{'#','#','#','#','#','#'}
};
for (int i = 0; i < 6; i++) { //打印地图
for (int j = 0; j < 6; j++) {
printf("%c",map[i][j]);
}
printf("\n");
}
int currentX = 1;
int currentY = 0;
char ch;
printf("请移动小人w:上\tx:下\ta:左\td:右\tq:退出\n");
while (1) {
if (currentY == 5) {
printf("哇哦,你逃出去了\n");
return 0;
}
scanf("%c",&ch);
switch (ch) {
case 'w' :
case 'W' :
if (map[currentX - 1][currentY] == ' ') {
int temp = map[currentX - 1][currentY];
map[currentX - 1][currentY]=map[currentX][currentY];
map[currentX][currentY]=temp;
currentX--;
for (int i = 0; i < 6; i++) { //打印地图
for (int j = 0; j < 6; j++) {
printf("%c",map[i][j]);
}
printf("\n");
}
}
break;
case 's' :
case 'S' :
if (map[currentX + 1][currentY] == ' ') {
int temp = map[currentX + 1][currentY];
map[currentX + 1][currentY]=map[currentX][currentY];
map[currentX][currentY]=temp;
currentX++;
for (int i = 0; i < 6; i++) { //打印地图
for (int j = 0; j < 6; j++) {
printf("%c",map[i][j]);
}
printf("\n");
}
}
break;
case 'a' :
case 'A' :
if (map[currentX][currentY - 1] == ' ') {
int temp = map[currentX][currentY - 1];
map[currentX ][currentY - 1]=map[currentX][currentY];
map[currentX][currentY]=temp;
currentY--;
for (int i = 0; i < 6; i++) { //打印地图
for (int j = 0; j < 6; j++) {
printf("%c",map[i][j]);
}
printf("\n");
}
}
break;
case 'd' :
case 'D' :
if (map[currentX][currentY + 1] == ' ') {
int temp = map[currentX][currentY + 1];
map[currentX][currentY + 1]=map[currentX][currentY];
map[currentX][currentY]=temp;
currentY++;
for (int i = 0; i < 6; i++) { //打印地图
for (int j = 0; j < 6; j++) {
printf("%c",map[i][j]);
}
printf("\n");
}
}
break;
case 'q' :
case 'Q' :
return 0;
}
}
}
|