Based on a handout by Eric Roberts
Handouts:
KarelReference
Worked Example:
Place100
Day1: BuildEfes.java
Karel has been hired to build the columns of the Charles Bridge. In particular, there are a set of arches where the stones (represented by beepers, of course) are missing as follows:
When Karel is done, the missing columns should be replaced by beepers, so the final picture would look like this:
Karel may count on the following facts about the world, listed below:
Even though the program is only a few lines, it is still worth getting a little practice in decomposition. For example, it would make sense to have a buildColumn
method.
One thing that computers are really good at is repeating commands.
In general, if you know before hand that there is a block of code you would like to repeat many times you can use a "for" loop, which looks like this:
for(int i = 0; i < N; i++) {
your code
}
For example if you want to move forward ten times, instead of writing the move(); command ten times you could write
for(int i = 0; i < 10; i++) {
move();
}
Your program will be much easier to write and easier to read, if you use for loops. There are multiple opportunities to use for loops in this problem!
Good luck and ask questions!