Making New GridWorld Bugs

We have played around with the sample projects for a bit and now we can start making our own. All that is needed is to tell our IDE (BlueJ or Eclipse) where gridworld.jar lives. It is a library that is quite complete. Details are in the Podcasts, but you can read about the installation of the library here at the College Board

Eclipse Instructions

  1. Start a New Project, and name it something (like "Bugs" for example)
  2. Select (Click once) your Project in the Package Explorer
  3. Select from the MENU Project-> Properties
  4. Click on the Java Build Path
  5. Click on the Libraries Tab
  6. Press the Add External JARs... button
  7. Select the file gridworld.jar (this was extracted from the file GridWorldCode.zip

CircleBug

Lets make a bug like the BoxBug, but instead of having it turn 90 degrees to form squares, lets make it turn once (45 degrees) so it crawls in an octagon shape.

We'll need two Classes: the CircleBug class and the CircleBugRunner Class.

CircleBug.java

import java.awt.Color;

import info.gridworld.actor.Bug;


public class CircleBug extends Bug 
{
	private int steps;
	private int sideLength;

	public CircleBug(int n)
	{
		sideLength=n;
		steps=0;
	}
	
	public void act()
	{
		if (steps < sideLength && canMove())
		{
			move();
			steps++;
		}
		else
		{
			turn();
			steps=0;
		}
	}

}


CircleBugRunner.java

import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.Location;
import java.awt.Color;

/**
 * This class runs a world that contains circle bugs. 
 * This class is not tested on the AP CS A and AB exams.
 */
public class CircleBugRunner 
{
    public static void main(String[] args)
    {
        ActorWorld world = new ActorWorld();
        CircleBug a = new CircleBug(2);
        a.setColor(Color.BLUE);
        
        world.add(new Location(7, 2), a);
        a.setDirection(Location.NORTHEAST);
        world.show();
    }
}

Try This

Make a constructor of CircleBug so that CicleBugRunner2 works!

CircleBugRunner2.java

import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.Location;
import java.awt.Color;

/**
 * This class runs a world that contains circle bugs.
 * This class is not tested on the AP CS A and AB exams.
 */
public class CircleBugRunner2 
{
    public static void main(String[] args)
    {
        ActorWorld world = new ActorWorld();
        CircleBug a = new CircleBug(3, Color.RED, Location.NORTH);
        world.add(new Location(7,2), a);
        world.show();
    }
}