#include <stdio.h>
void print(char cs[6][6])
{
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
printf("%c", cs[i][j]);
}
printf("
");
}
}
int isSuccess(int x, int y)
{
if (x == 1 && y == 5) {
return 1;
}
return 0;
}
int main(int argc, const char * argv[]) {
char cs[6][6] = {{'#','#','#','#','#','#'},
{'#','O',' ','#',' ',' '},
{'#',' ','#','#',' ','#'},
{'#',' ',' ','#',' ','#'},
{'#','#',' ',' ',' ','#'},
{'#','#','#','#','#','#'}};
print(cs);
char c;
int x = 1, y = 1;
while (1) {
int flag = 0;
printf("请输入方向(上w,下s,左a,右d)
");
scanf("%c", &c);
getchar();
if (c != 'w' && c != 's' && c != 'a' && c != 'd') {
printf("输入错误!
");
continue;
}
switch (c) {
case 'w': // 上
if (cs[x-1][y] == ' ') {
cs[x-1][y] = cs[x][y];
cs[x][y] = ' ';
x--;
print(cs);
flag = isSuccess(x,y);
}else{
printf("此路不通w!!
");
}
break;
case 's': // 下
if (cs[x+1][y] == ' ') {
cs[x+1][y] = cs[x][y];
cs[x][y] = ' ';
x++;
print(cs);
flag = isSuccess(x,y);
}else{
printf("此路不通s!!
");
}
break;
case 'a': //左
if (cs[x][y-1] == ' ') {
cs[x][y-1] = cs[x][y];
cs[x][y] = ' ';
y--;
print(cs);
flag = isSuccess(x,y);
}else{
printf("此路不通a!!
");
}
break;
case 'd': // 右
if (cs[x][y+1] == ' ') {
cs[x][y+1] = cs[x][y];
cs[x][y] = ' ';
y++;
print(cs);
flag = isSuccess(x,y);
}else{
printf("此路不通d!!
");
}
break;
default:
break;
}
if (flag) {
printf("恭喜你走出迷宫!!");
break;
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。