Skip to content

Instantly share code, notes, and snippets.

@CompSciRocks CompSciRocks/Robot.java
Last active Nov 27, 2018

Embed
What would you like to do?
public int clearHall() {
int cnt = 0;
while (!hallIsClear()) {
move();
cnt++;
}
return cnt;
}
private boolean forwardMoveBlocked() {
if (facingRight && pos >= hall.length - 1)
return true;
else if (!facingRight && pos == 0)
return true;
return false;
}
private void move() {
if (hall[pos] > 0)
hall[pos]--;
if (hall[pos] == 0) {
if (forwardMoveBlocked()) {
facingRight = !facingRight;
}
else if (facingRight) {
pos++;
}
else {
pos--;
}
}
}
public class Robot {
private int[] hall;
private int pos;
private boolean facingRight;
public boolean forwardMoveBlocked() { // part A }
private void move() { // part B }
public int clearHall() { // part C }
private boolean hallIsClear() {
// implementation not shown
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.