Maze Bug

<< 2005Average | APQuestionsTrailIndex | Dahlia >>

MazeBug

If you do not have the Question handy, you can download the pdf document MazeBugQuestion.pdf

import info.gridworld.grid.*; 
import info.gridworld.actor.*; 
import info.gridworld.actor.Critter; 

import java.awt.Color; 
import java.util.ArrayList;public class MazeBug extends Bug
  {
    // returns true if this bug is in a grid and all other
    // actors in its grid are either rocks or flowers;
    // otherwise returns false
    public boolean isValidGrid()
    {  /* to be implemented in part (a) */  

    // moves left if it can move left; otherwise acts
    // like a regular bug


      Grid<Actor> gr = getGrid();
      if (gr == null)
      return false;

      ArrayList<Location> occupiedLocs =
                         gr.getOccupiedLocations();
      for (Location loc : occupiedLocs)
      {
         Actor a = gr.get(loc);
         if (!(a instanceof Rock || a instanceof Flower)
                                             && a != this)
      return false;
       }
      return true;
    }
    public void act()
    {
      if (canMoveLeft())
        moveLeft();
      else
        super.act();
    }

    // turns 90 degrees to the left, then moves like a
    // regular bug
    public void moveLeft()
    {  /* to be implemented in part (b) */ 
       setDirection(getDirection() + Location.LEFT);
       super.move(); //I think just "move();" would work too!

    }

    // returns true if this bug is located next to a left turn
    // of the wall, that is, the adjacent location to the
    // left of this bug is valid and is either empty or holds
    // a flower, while the adjacent location diagonally to the
    // left and back is either invalid or holds a rock;
    // otherwise returns false  
    public boolean canMoveLeft()
    {  /* to be implemented in part (c) */  
       Grid gr = getGrid();
       if (gr == null)
           return false;
       Location loc = getLocation();
       Location next =
            loc.getAdjacentLocation(getDirection() - 90);
       Location corner =
            loc.getAdjacentLocation(getDirection() - 135);
       return gr.isValid(next) &&
           !(gr.get(next) instanceof Rock) &&
            (!gr.isValid(corner) ||
                       gr.get(corner) instanceof Rock);

    }

    // possibly other methods (not shown)
    /* to be discussed in part (d) */
    public void turn()
    {
       super.turn();
       super.turn();
     }
  }

MazeBugRunner

import info.gridworld.grid.*; 
import info.gridworld.actor.*; 
import java.awt.Color; 
public class MazeBugRunner
{


	public static void main (String[] args)
	{
		ActorWorld world = new ActorWorld(new BoundedGrid(8,10) );

		MazeBug m=new MazeBug();
		world.add(new Location (5,1),m);

		world.add(new Location (0,0), new Rock());
		world.add(new Location (1,0), new Rock());
		world.add(new Location (2,0), new Rock());
		world.add(new Location (4,0), new Rock());
		world.add(new Location (5,0), new Rock());
		world.add(new Location (2,4), new Rock());
		for (int i=1; i< 5;i++)
		   world.add(new Location (3,i), new Rock());
		for (int i=2; i< 7;i++)
		   world.add(new Location (0,i), new Rock());
		for (int i=3; i< 7;i++)
		   world.add(new Location (5,i), new Rock());
		for (int i=1; i< 5;i++)
		   world.add(new Location (i,7), new Rock());

        world.show();
	}


}