int CanGo(int xxx,int yyy,int ddir)
/* input: xxx,yyy - location of ant
ddir - now dir
output: 0 - forward and left and right can go
1 - forward can not go
2 - left can not go
3 - right can not go
4 - forward and left can not go
5 - forward and right can not go
6 - left and right can not go
7 - forward and left and right all can not go
*/
{
int tx,ty,tdir;
int okf,okl,okr;
/* forward can go ? */
tdir = ddir;
tx = xxx;
ty = yyy;
switch(tdir)
{
case UP: ty--;
break;
case DOWN: ty++;
break;
case LEFT: tx--;
break;
case RIGHT: tx++;
break;
default: break;
} /* of switch dir */
if(JudgeCanGo(tx,ty)) okf = 1;
else okf = 0;
/* turn left can go ? */
tdir = TurnLeft(ddir);
tx = xxx;
ty = yyy;
switch(tdir)
{
case UP: ty--;
break;
case DOWN: ty++;
break;
case LEFT: tx--;
break;
case RIGHT: tx++;
break;
default: break;
} /* of switch dir */
if(JudgeCanGo(tx,ty)) okl = 1;
else okl = 0;
/* turn right can go ? */
tdir = TurnRight(ddir);
tx = xxx;
ty = yyy;
switch(tdir)
{
case UP: ty--;
break;
case DOWN: ty++;
break;
case LEFT: tx--;
break;
case RIGHT: tx++;
break;
default: break;
} /* of switch dir */
if(JudgeCanGo(tx,ty)) okr = 1;
else okr = 0;
if(okf&&okl&&okr) return(0);
if(!okf&&okl&&okr) return(1);
if(okf&&!okl&&okr) return(2);
if(okf&&okl&&!okr) return(3);
if(!okf&&!okl&&okr) return(4);
if(!okf&&okl&&!okr) return(5);
if(okf&&!okl&&!okr) return(6);
if(!okf&&!okl&&!okr) return(7);
return(7);
}
int JudgeCanGo(int xxx,int yyy)
/* input: location to judeg
output: 0 -- can not go
1 -- can go
*/
{
int i,j;
if(xxx<=0||xxx>MAXX) return(0);
if(yyy<=0||yyy>MAXY) return(0);
if(block[xxx][yyy]) return(0);
return(1);
}
int TurnLeft(int ddir)
{
switch(ddir)
{
case UP: return(LEFT);
case DOWN: return(RIGHT);
case LEFT: return(DOWN);
case RIGHT: return(UP);
default: break;
} /* of switch dir */
}
int TurnRight(int ddir)
{
switch(ddir)
{
case UP: return(RIGHT);
case DOWN: return(LEFT);
case LEFT: return(UP);
case RIGHT: return(DOWN);
default: break;
} /* of switch dir */
}
int TurnBack(int ddir)
{
switch(ddir)
{
case UP: return(DOWN);
case DOWN: return(UP);
case LEFT: return(RIGHT);
case RIGHT: return(LEFT);
default: break;
} /* of switch dir */
}