GridWorld

The Bug class has three methods that specify how bugs move and turn.
 * The Bug Class:**

1. canMove code Public boolean canMove {   Grid gr = getGrid; if (gr == null) return false;   //If the grid does not exist the bug can not move Location loc = getLocation; //Gets the current location of the bug Location next = loc.getAdjacentLocation(getDirection);  //Gets the location of the square //next to the current location of                                                              //the bug in the direction the bug //is facing. (The destination of                                                              //the bug) if (!gr.isValid(next)) return false;     //If Location next is not valid the bug can not move. Actor neighbor = gr.get(next); return (neighbor == null) || (neighbor instanceOf Flower); //The bug can only move if the //distination is empty or holding //a flower. } code 2. move code public void move {   Grid gr = getGrid; if (gr == null) return;    //kicks out of the program if the grid does not exist Location loc = getLoaction; //Gets the current location of the bug Location next = loc.getAdjacentLocation(getDirection);   //Gets the location of the square //next to the current location of                                                               //the bug in the direction the bug //is facing. if (gr.isValid(next)) moveTo(next);   //moves the bug to the next space if it is valid else removeSelfFromGrid;  //moves the bug from the grid if the next space is not valid Flower flower = new Flower(getColor); flower.putSelfInGrid(gr, loc);    //makes a flower the color of the bug and places it in the //space of the bug after it moves. }

code 3. turn code public void turn {   setDirection(getDirection + Location.HALF_RIGHT);  //Turns the bug 45 degrees to the right //of the current direction of the bug. } code These methods are built into the Bug actor and can be imported and used using this: code import info.gridworld.actor.Bug; code The methods can be rewritten and overwrited to fit your needs but those are the default methods. They are used in the act method to control the behavior of the bug (the way the bug moves and acts). This is an example of a basic act method: code public void act {   if(canMove)  //If canMove is true (If the bug can move) then move the bug move; else turn;  //If it can't than turn the bug. }

code The act method can be altered to effect the behavior and the movement of the bug This act method use two instance variables, sideLength and steps to make the bug move in a square pattern: code public void act {   if (steps < sideLength && canMove) //this is a loop that makes the bug move the number of       {                                 //spaces given by sideLength. move; steps++; }       else        //This turns the bug 90 degrees (45 per turn) when the bug moved sideLength //spaces and starts the process over from the beginning. {       turn; turn; steps = 0; } } code This code can be changed to make the bug move in many different kinds of patterns.

The location class can be imported and used using: code import info.gridworld.grid.Location; code Every actor that is on the grid has it's own location in the grid. The Location class defines the coordinates for an actor's position in a grid and provides methods that determine relationships between locations and compass directions. Compass directions: North = 0 East = 90 South = 180 West = 270 North east = 45 South east = 135 south west = 225 north west = 315
 * The Location Class:**

Compass directions are stored as finial intigers and can be used with their name or their compass direction in degrees (NORTHWEST or 315)

The Location class also stores commonly used turn angles at finial intigers: code public static final int LEFT = -90; public static final int RIGHT = 90; public static final int HALF_LEFT = -45; public static final int HALF_RIGHT = 45; //This is used in the turn method of the Bug actor. public static final int FULL_CIRCLE = 360; public static final int HALF_CIRCLE = 180; public static final int AHEAD = 0;

code A location has a row and a column which are the parameters of the Location constructor code public Location(int r, int c) code Two accessor methods return the row and column for a location code public int getRow //returns the row for a location (loc.getRow) public int getCol //returns the column for a location (loc.getCol) code Two other methods give info about the relationships between locations and directions 1. public Location getAdjacentLocation(int direction) This returns the adjacent location in the direction of int direction code Location loc1 = new Location(5,7) Location loc2 = loc1.getAdjacentLocation(Location.WEST); //loc2 = (5, 6). Is is next too loc1 on the west side.

code 2. public int getDirectionToward(Location target) This returns the direction from a location toward a target code Location loc1 = new Location(5,7); int dir = loc1.getDirectionToward(new Location(6, 8)); //dir = 135 degrees. code