Last active
November 27, 2018 19:20
-
-
Save CompSciRocks/fa88930de068f4bfea04ffcefc8f4a23 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int clearHall() { | |
int cnt = 0; | |
while (!hallIsClear()) { | |
move(); | |
cnt++; | |
} | |
return cnt; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private boolean forwardMoveBlocked() { | |
if (facingRight && pos >= hall.length - 1) | |
return true; | |
else if (!facingRight && pos == 0) | |
return true; | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void move() { | |
if (hall[pos] > 0) | |
hall[pos]--; | |
if (hall[pos] == 0) { | |
if (forwardMoveBlocked()) { | |
facingRight = !facingRight; | |
} | |
else if (facingRight) { | |
pos++; | |
} | |
else { | |
pos--; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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