You are on page 1of 2

/*

* File: StoneMasonKarel.java
* --------------------------
* The StoneMasonKarel subclass solves the "repair the quad"
* problem from Assignment 1.
*/

import stanford.karel.*;

public class StoneMasonKarel extends SuperKarel {


public void run(){

while (frontIsClear()) {
repairColumn();
}
while(frontIsBlocked()) {
if (facingNorth()){
goBack();
if (frontIsClear()){
changeColumn();
repairColumn();
}
}
}
}
/*
* Pre-condition = facing East at the bottom of the world
* Post-condition=facing North and front is blocked at the top of
the world.
* makes Karel repair a whole column using beepers
*/

private void repairColumn(){


turnLeft();
if (noBeepersPresent()){
putBeeper();
}
while (frontIsClear()){
move();
if (noBeepersPresent()) {
putBeeper();
}
}
}
/*
* Pre-condition : facing north and front is blocked at the top of
the world
* Post-condition : facing east at the bottom of the world
*/

private void goBack(){


turnAround();
while (frontIsClear()){
move();
}
if (frontIsBlocked()){
turnLeft();
}
}
/*
* makes Karel move to next column
* NOTE: The distance between every two columns to repair is 4
units
*/

private void changeColumn(){


if (frontIsClear()){
move();
}
if (frontIsClear()){
move();
}
if (frontIsClear()){
move();
}
if (frontIsClear()){
move();
}
}
}

You might also like