The basics of problem 2 is “Karel has been hired to repair the damage done to the Quad in the 1989 earthquake. In particular, Karel is to repair a set of arches where some of the stones (represented by beepers, of course) are missing from the columns supporting the arches”, which means that you have to replace empty places with a beeper!. The rules are:
Karel may count on the following facts about the world:
- Karel starts at 1st Avenue and 1st Street, facing east, with an infinite number of beepers.
- The columns are exactly four units apart, on 1st, 5th, 9th Avenue, and so forth.
- The end of the columns is marked by a wall immediately after the final column. This wall section appears after 13th Avenue in the example, but your program should work for any number of columns.
- The top of the column is marked by a wall, but Karel cannot assume that columns are always five units high, or even that all columns are the same height.
- Some of the corners in the column may already contain beepers representing stones that are still in place. Your program should not put a second beeper on these corners.
Remember to change the run configurations as from the previous post on how to setup the environment to the StoneMasonKarel for the main class, here is the my java file for this problem 2.
My way of fixing the stones is to go up the supporting line and replace the stones (beepers) and then come back down again and move along 4 positions if able to, and repeat, since in the rules you the height of the building can change which is why I come back down again from the top of the buildings.
/* * File: StoneMasonKarel.java * -------------------------- * The StoneMasonKarel subclass as it appears here does nothing. * When you finish writing it, it should solve the "repair the quad" * problem from Assignment 1. In addition to editing the program, * you should be sure to edit this comment so that it no longer * indicates that the program does nothing. */ import stanford.karel.*; public class StoneMasonKarel extends SuperKarel { // move up the row and find replace any missing "stones" // checking to make sure that we have not reached the top private void replaceStones() { // check for a stone or wall! move in that direction while (true) { if (!beepersPresent()) putBeeper(); if (frontIsBlocked()) break; move(); } } private void comeBackDown() { turnAround(); while (!frontIsBlocked()) move(); } // need to go up and down move 4 and then up and down private void moveAlong() { while (true) { turnLeft(); replaceStones(); comeBackDown(); turnLeft(); if (!frontIsBlocked()) for (int i =0; i < 4; i++) move(); else break; } } public void run() { moveAlong(); } } |
again the zile file attached is the whole assignment 1 problems, so you can download that for the full source code and the PDF assignment details.