Question: It would be interesting for our BattleBug to first examine all of its neighbors and turn to the direction of any of them that happens
It would be interesting for our BattleBug to first examine all of its neighbors and turn to the direction of any of them that happens to be a Flower. Then move in that direction, eating the flower as it walks over it. But assuming we find a Flower in one of the adjacent cells, how can we tell which way to turn? One way would be to make our own direction method that takes two locations, our BattleBugs location (loc) and the neighbors location ( using the getLocation method on each actor in the neighbors list), and returns the direction the BattleBug should face to visit that neighbor on its next move.
so far my code looks like this:
public BattleBug(int length, int winFlower) { steps = 0; sideLength = length; this.winFlower = winFlower; flowers=0; }
public void move() { Grid I Need help writing the Direction method!! So, now, all we have to do is write the direction method. Looking at the GridWorld Javadoc, we see that the Location class has a getRow and getCol method. That lets us do some math based on the row and column of the two locations. public int direction(Location here, Location there) { // find the direction to go from here to there // (either 0, 45, 90, 135, 180,225, 270, 315) // using here.getRow(), there.getRow(), here.getCol(), there.getCol() etc // in a number of if statements } If you can get the direction method to work, your BattleBug should be able to turn directly to a neighboring Flower and eat it.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
