-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixRoad.java
138 lines (125 loc) · 2.8 KB
/
fixRoad.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import stanford.karel.*;
/*File: fixRoad.java
* This is just try to practice chapter 2 problem: put beeper to the pothole
* Use world file named: practice_chapter4
*
*/
public class fixRoad extends Karel {
/* public void run() {
* while (frontIsClear()){
* checkForPothole();
* move();
* }
* checkForPothole();
* }
*/
public void run() {
collectAllBeepers();
dropAllBeepers();
returnHome();
}
/*collects beepers from every tower by moving along 1st street, calling collectOneTower()
* at every corner. the postcondition is that Karel is in the easternmost corner of 1st
* street facing east
*
*/
private void collectAllBeepers() {
while (frontIsClear()) {
collectOneTower();
move();
}
collectOneTower();
}
/*make Karel collect all beepers in one tower and return to 1st street facing east.
* precondition: Karel is in 1st street facing east
* postcondition: Karel is in 1st street facing east at same corner
*
*
*/
private void collectOneTower() {
turnLeft();
collectLineOfBeepers();
turnAround();
moveToWall();
turnLeft();
}
/*collect all beepers in this avenue.
* precondition: Karel is in 1st street facing north
* postcondition: Karel stops when there is no beeper at this corner
*
*/
private void collectLineOfBeepers() {
while (beepersPresent()) {
pickBeeper();
move();
}
}
/*move back to 1st street
* precondition: facing south
* postcondition: karel is in 1st street facing south
*
*/
private void moveToWall() {
while (frontIsClear()) {
move();
}
}
/*
* drop all beepers at this corner
* precondition: Karel is in easternmost corner in 1st street facing east
* postcondition: Karel is this corner and drop all beepers collected facing east
*/
private void dropAllBeepers() {
while (beepersInBag()) {
putBeeper();
}
}
/*
* return back to starting point
* precondition: Karel is in easternmost corner in 1st street facing east
* postcondition: karel is in westernmost corner in 1st street facing east
*/
private void returnHome() {
turnAround();
moveToWall();
turnAround();
}
/* turn karel to right
*
*/
private void turnRight() {
turnLeft();
turnLeft();
turnLeft();
}
/*turn karel around
*
*/
private void turnAround() {
turnLeft();
turnLeft();
}
/*make Karel to fill pothole
*
*/
private void fillPothole() {
turnRight();
move();
if (noBeepersPresent()) {
if (beepersInBag()) {
putBeeper();
}
}
turnAround();
move();
turnRight();
}
/*make Karel to check the pothole: if there is pothole, fill it
*
*/
private void checkForPothole() {
if (rightIsClear()) {
fillPothole();
}
}
}