#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("\n");
}
}
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)\n");
scanf("%c", &c);
getchar();
if (c != 'w' && c != 's' && c != 'a' && c != 'd') {
printf("输入错误!\n");
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!!\n");
}
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!!\n");
}
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!!\n");
}
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!!\n");
}
break;
default:
break;
}
if (flag) {
printf("恭喜你走出迷宫!!");
break;
}
}
return 0;
}
迷宫
最新推荐文章于 2024-04-27 13:41:46 发布