# Thread: Explain how this simple A.I works and post alternatives

1. Member
Join Date
Nov 2010
Posts
37
Rep Power
0

## Explain how this simple A.I works and post alternatives

I don't understand how this A.I works, it's a pathfinder for monsters to follow blocks that are road.

This is how I read it with direction set to left and I will need to go up after one step.

for one unit of time
move right pixel by pixel until one complete step
when step complete increase block coordinate to signal one step made to the right and set postLeft to true

!postup is true, check if there is ground down
no there is not

!postdown is true, check there is ground up
yes there is, set direction to up

!postLeft is false ignore

!postRight is true, check there is ground left
yes there is, set direction left

and so I would think that instead of going up on the corner I would head back the way I came... but this is not so as the code works fine. what can't I see?

and for fun and because A.I is very interesting and I would like to compare, please post different solutions to learn from.

Java Code:
```public void physic()
{
if( walkFrame >= walkSpeed )
{
if( direction == up )
{
y -= 1;
}
else if( direction == right )
{
x += 1;
}
else if ( direction == down )
{
y += 1;
}
else if (direction == left )
{
x -= 1;
}

progressToNextStep += 1;

if( progressToNextStep == screen.room.blockSize)
{
if( direction == up )
{
myY -= 1;
postUp = true;
}
else if( direction == right )
{
myX += 1;
postRight = true;
}
else if ( direction == down )
{
myY += 1;
postDown = true;
}
else if (direction == left )
{
myX -=1;
postLeft = true;
}

if( !postUp )
{
try
{
if( screen.room.block[myY + 1][myX].groundID == Sprite.groundRoad)
{
direction = down;
}
}
catch(Exception e)
{
}
}
if(!postDown)
{
try
{
if( screen.room.block[myY - 1][myX].groundID == Sprite.groundRoad)
{
direction = up;
}
}
catch(Exception e)
{
}
}
if(!postLeft)
{
try
{
if( screen.room.block[myY][myX + 1].groundID == Sprite.groundRoad)
{
direction = right;
}
}
catch(Exception e)
{
}
}
if(!postRight)
{
try
{
if( screen.room.block[myY][myX - 1].groundID == Sprite.groundRoad)
{
direction = left;
}
}
catch(Exception e)
{
}
}

if(screen.room.block[myY][myX].airID == Sprite.airSandwich)
{
deleteMonster();
damage();
}

postUp = false;
postRight = false;
postDown = false;
postRight = false;

progressToNextStep = 0;
}

walkFrame = 0;
}
else
{
walkFrame +=1;
}
}```

2. Moderator
Join Date
Apr 2009
Posts
13,321
Rep Power
23

## Re: Explain how this simple A.I works and post alternatives

It's hard to tell without the other data, but it looks to me like some sort of patrolling AI.
In other words, keep the beastie moving.
It's not a "goto" one, that is the monster doesn't seem to have a target to get to.

And I have no idea what airSandwich is...:)

3. Member
Join Date
Nov 2010
Posts
37
Rep Power
0

## Re: Explain how this simple A.I works and post alternatives

yeah the monsters just follow a road to the end... think tower defence. It works but I'm just not sure how, because with my logic (above) they would go back the way they came.

the goal at the end of the path is a sandwich. Because I wanted to make it realistic... don't you hate it when you make a sandwich and then an army of monsters try to steel it and you must set up lasers. hate it when that happens.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•