Results 1 to 3 of 3
  1. #1
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default 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. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default 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...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default 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.

Similar Threads

  1. for loop -anyone can explain me how it works
    By zanetti77 in forum New To Java
    Replies: 6
    Last Post: 07-21-2012, 02:37 PM
  2. Replies: 8
    Last Post: 01-10-2012, 04:40 AM
  3. First post-Simple question help please
    By Teesea in forum New To Java
    Replies: 2
    Last Post: 08-11-2011, 01:03 PM
  4. Please explain how this bit of code works.
    By Allspark in forum New To Java
    Replies: 4
    Last Post: 09-03-2010, 03:56 AM
  5. Explain how to troubleshoot simple java pg
    By senthil in forum Advanced Java
    Replies: 2
    Last Post: 09-01-2009, 01:28 PM

Posting Permissions

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