#include<stdio.h>
#include<string.h>
int x=1;
int y=1;
char map[7][6]={
{'#','#','#','#','#','#'},
{'#','0','#',' ',' ',' '},
{'#',' ','#',' ','#','#'},
{'#',' ','#',' ',' ','#'},
{'#',' ',' ','#',' ','#'},
{'#','#',' ',' ',' ','#'},
{'#','#','#','#','#','#'}
};
int printmap(char s[][6])
{
int i,j;
for(i=0;i<7;i++)
{
for(j=0;j<6;j++)
{
printf("%c",s[i][j]);
}
printf("\n");
}
return 0;
}
int cao(char c,char s[][6])
{
int flag=0;
if(c=='W'||c=='w')
{
if(x-1<0)
{
flag=1;
goto loop;
}
if(s[x-1][y]!='#'&&x-1>=0)
{
s[x-1][y]='0';
s[x][y]=' ';
x=x-1;
y=y;
}
}
else if(c=='s'||c=='S')
{
if(x+1>7)
{
flag=1;
goto loop;
}
if(s[x+1][y]!='#'&&x+1<=7)
{
s[x+1][y]='0';
s[x][y]=' ';
x=x+1;
y=y;
}
}
else if(c=='a'||c=='A')
{
if(y-1<0)
{
flag=1;
goto loop;
}
if(s[x][y-1]!='#'&&y-1>=0)
{
s[x][y-1]='0';
s[x][y]=' ';
x=x;
y=y-1;
}
}
else if(c=='d'||c=='D')
{
if(y+1>=6)
{
flag=1;
goto loop;
}
if(s[x][y+1]!='#'&&y+1<6)
{
s[x][y+1]='0';
s[x][y]=' ';
x=x;
y=y+1;
}
}
printf("**(%d,%d)\n",x,y);
loop: return flag;
}
int main()
{
//坐标
int i,j,k;
char d; //指令
int flag;
while(1)
{
printmap(map);
printf("请输入W/S/A/D来操纵方向...\n");
d=getchar();
getchar();
flag=cao(d,map);
if(flag==1)
{
printf("恭喜你,成功了\n");
break;
}
else
{
printmap(map);
printf("(%d,%d)\n",x,y);
}
}
return 0;
}
迷宫(改)
最新推荐文章于 2019-10-11 14:15:46 发布