Help with a line of my code.
I am using this code to start a class ZigZagBug in a "game"
known as GridWorld. We use this in our Programming class right now.
Anyway, my code does what it is supposed to do, I know what the lines do, but
how can I describe them with comments?
Here is the code.
Code Java:
import info.gridworld.actor.Bug;
public class ZigZagBug extends Bug {
int counter = 1;
public ZigZagBug(){
turn();
}
public void act(){
if(canMove()){
//This gets the adjacent location, of the direction that the bug is facing.
//IE - Faces Right-North, the next location is the location directly in front of what the bug faces.
moveTo(getLocation().getAdjacentLocation(getDirection()));
setDirection(getDirection() + 90 + ((counter++)%2 * 180));
}else{
//If the bug cannot move, it turns 180 degrees and then moves again.
setDirection(getDirection() + 180);
}
}
}
Now, what is..
This for?
And this for?
Code Java:
setDirection(getDirection() + 90 + ((counter++)%2 * 180));
Re: Help with a line of my code.
I would hazard a guess that int counter = 1; is an integer variable called counter that's purpose is to keep count of something.
If you look through the code you will see counter++. This means increment the value by 1.
The second one is just as easy to work out.
You are calling the setDirection method and passing to it the getDirection value plus 90 plus the incremented value of counter%2 times 180.
When run it could look something like: setDirection(1420);