-
Notifications
You must be signed in to change notification settings - Fork 0
/
VotingBox.java
70 lines (49 loc) · 1.36 KB
/
VotingBox.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Karel_the_Robot_Voting_Helper - Stanford Assignment #1
// TESTING 123
import stanford.karel.*;
public class VotingBox extends SuperKarel {
public void run() {
// Karel starts on column 1 - moves him into the voting area
move();
checkForBeepersPresent();
}
//******************* MY METHODS START ***************************
// BEEPERS PRESENT IN MIDDLE - move to next voting column
private void checkForBeepersPresent() {
while(frontIsClear()) {
if(beepersPresent()) {
move();
move();
}
// NO BEEPER IN MIDDLE - look for beepers in the top box of that row
if(noBeepersPresent()) {
turnLeft();
move();
}
// BEEPER FOUND IN TOP BOX - pick all the beepers
while (beepersPresent()) {
pickBeeper();
}
// NO BEEPER PRESENT IN THE TOP BOX - turn around to check the bottom box & reset to the middle spot.
if (noBeepersPresent()) {
turnAround();
move();
move();
if(frontIsBlocked()) {
turnAround();
}
while (beepersPresent()) {
pickBeeper();
}
move();
turnRight();
move();
if(frontIsBlocked()) {
return;
}
move();
}
} // CLOSE METHOD FOR - while(frontIsClear() loop
} // CLOSE METHOD FOR - checkForBeepersPresent()
//************************* FINAL BRACE **********************************
}