If you have no idea what gridworld is...
GridWorld is the case study for the 2008 AP CS exam (and beyond). GridWorld uses an engaging environment that allows students to create and test actors with a wide variety of behaviors.
The Official website is http://www.collegebo...sci_a/case.html
Anyway on to the actual program.
My Jumper is supposed to be able to do certain things
-Jump 2 spaces in the grid
- If it runs into any other actor besides another jumper it should eat it
- If it runs into another Jumper then just ignore it.
- If location two cells in front of jumper is out of grid, move ONE cell forward then turn 90 degress. then move ONE cell forward, else repeat 90 degrees until he can move and then step one cell ahead.
- If location one cell ahead of the Jumper is a rock, wait for two tries then crush the rock.
I think I have everything except the last two, If you have any knowledge in gridworld I would love some advice, Here is my code
import info.gridworld.actor.*;
import info.gridworld.grid.*;
import java.awt.*;
public class Jumper extends Actor
{
public Jumper(Color jumperColor)
{
setColor(jumperColor);
}
public void act()
{
if (canMove())
{ move();
move();}
else
turn();
}
public boolean canMove()
{
Grid<Actor> gr = getGrid();
if (gr == null)
return false;
Location loc = getLocation();
Location next = loc.getAdjacentLocation(getDirection());
if (!gr.isValid(next))
return false;
Actor neighbor = gr.get(next);
return (neighbor == null) || (neighbor instanceof Jumper) || (neighbor instanceof Flower);
}
public void move()
{
Grid<Actor> gr = getGrid();
if (gr == null)
return;
Location loc = getLocation();
Location next = loc.getAdjacentLocation(getDirection());
if (gr.isValid(next))
moveTo(next);
else
turn();
}
public void turn()
{
setDirection(getDirection() + Location.RIGHT);
}
}
Also I will include the runner I use
import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.Location;
import info.gridworld.actor.*;
import info.gridworld.actor.Bug;
import info.gridworld.actor.Rock;
import info.gridworld.actor.Flower;
import java.awt.Color;
/**
* This class runs a world that contains jumper. <br />
* This class is not tested on the AP CS A and AB exams.
*/
public class JumperRunner
{
public static void main(String[] args)
{
ActorWorld world = new ActorWorld();
Jumper alice = new Jumper(Color.BLUE);
Jumper bob = new Jumper(Color.GREEN);
world.add(new Bug());
world.add(new Rock());
world.add(new Flower(Color.RED));
//world.add(new Location(4, 5), Flower);
world.add(new Location(4, 3), alice);
world.add(new Location(5, 5), bob);
world.show();
}
}

New Topic/Question
Reply


MultiQuote





|