User Tools

Site Tools


it-artikel:java:my-solution-on-assignment-1-problem-2

My Solution on Assignment 1 - Problem 2

This is my Solution of Assignment 1 - Problem 2 from the Computer Science Course CS106A of Prof. Mehran Sahami at the STANFORD University.

StoneMasonKarel.java
/*
 * 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 {
 
	public void run() {
		// 1st column is allways at starting point
		while( frontIsClear() ) {
			repairColumn();
			walkNext();
		}
		repairColumn();
	}
 
	private void repairColumn() {
		/* Climbs a Columne and fixes it. Falls back down when done.
		 * facing EAST again.
		 */
 
		turnLeft();
		while( frontIsClear() ) {
			if( noBeepersPresent() ) {
				putBeeper();
			}
			move();
		}
		// Front is now blocked (top on column), drop another beeper if needed.
		if( noBeepersPresent() ) {
			putBeeper();
		}
		turnAround();
		moveToWall();
		turnLeft();
	}
 
 
	private void moveToWall() {
		// Moves Karel all the way straight forward until aproaching a wall
		while( frontIsClear() ) {
			move();
		}
	}
 
 
	private void walkNext() {
		/* walks 4 steps forward
		 */
		for( int x=0; x < 4 ; x++ ) {
			move();
		}
	}
}

Axel Werner 2011-02-05 17:40

it-artikel/java/my-solution-on-assignment-1-problem-2.txt · Last modified: 2022-08-31 12:30 by 127.0.0.1