For my Pre-Test in my CPU Prog class, we had to make a ZigZagBug Class.
I used a line from a person that I know in the class, but for a comment with
that line, I don't know how to explain it.
Here is the code.
Now, what is..
This for?
And this for?
I used a line from a person that I know in the class, but for a comment with
that line, I don't know how to explain it.
Here is the code.
Code:
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?
Code:
int counter = 1;
And this for?
Code:
setDirection(getDirection() + 90 + ((counter++)%2 * 180));
