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:
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();
}
}
Why are you modifying the act() method? Take a look at Bug's original canMove() method, and think about when it should move forward.

Also, you should almost never use a loop when dealing with GridWorld because act() is called each time Step is pressed.
We were origionally doing that, where we had it do move and then check to see if the next spot was open, and then move again. However, everytime it moves, it leaves a flower in the grid location it was just in, and we needed to remove the flower, so in other words it would "jump" over a space and not place a flower in it.
Svakk wrote:
We were origionally doing that, where we had it do move and then check to see if the next spot was open, and then move again. However, everytime it moves, it leaves a flower in the grid location it was just in, and we needed to remove the flower, so in other words it would "jump" over a space and not place a flower in it.

So just copy the original act() method verbatim, removing the part where it drops a flower.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement