Hey guys, so me and my friends screwed up and we've got this programming project due for our Java class at midnight tonight, just wondering if anyone has any ideas what to do. Currently, we have a tester and a class file, and here's the code for them. If you want to run them, i'd have to send you the required library. The problem right now is on the grid it is suppose to move forward two spaces, and we can't seem to get it to do that. All it does is move diagonally and then stay there instead of changing direction. Any help is greatly appreciated! Thanks!
Code:
Code:
Code:
import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.Location;
import java.awt.Color;
/**
* This class runs a world that contains box bugs. <br />
* This class is not tested on the AP CS A and AB exams.
*/
public class BoxBugRunner
{
public static void main(String[] args)
{
ActorWorld world = new ActorWorld();
JumperBug alice = new JumperBug(6);
alice.setColor(Color.ORANGE);
JumperBug bob = new JumperBug(3);
world.add(new Location(1, 2), alice);
world.add(new Location(5, 5), bob);
world.show();
}
}
Code:
import info.gridworld.actor.Bug;
import info.gridworld.grid.Location;
public class JumperBug extends Bug
{
private int steps;
private int sideLength;
/**
* Constructs a box bug that traces a square of a given side length
* @param length the side length
*/
public JumperBug(int length)
{
steps = 0;
sideLength = length;
}
/**
* Moves to the next location of the square.
*/
public void act()
{
Location loc = getLocation();
int y = loc.getRow();
int x = loc.getCol();
while(steps < sideLength){
if(canMove() && getDirection()==0){
Location newLoc = new Location(x,y-2);
moveTo(newLoc);
}
else if(canMove() && getDirection()==45){
Location newLoc = new Location(x+2,y-2);
moveTo(newLoc);
}
else if(canMove() && getDirection()==90){
Location newLoc = new Location(x+2,y);
moveTo(newLoc);
}
else if(canMove() && getDirection()==135){
Location newLoc = new Location(x+2,y+2);
moveTo(newLoc);
}
else if(canMove() && getDirection()==180){
Location newLoc = new Location(x,y+2);
moveTo(newLoc);
}
else if(canMove() && getDirection()==225){
Location newLoc = new Location(x-2,y+2);
moveTo(newLoc);
}
else if(canMove() && getDirection()==270){
Location newLoc = new Location(x-2,y);
moveTo(newLoc);
}
else if(canMove() && getDirection()==315){
Location newLoc = new Location(x-2,y-2);
moveTo(newLoc);
}
steps+=2;
}
turn();
}
}