Axel Werner's OPEN SOURCE Knowledge Base
Because Information Wants To Be Free!
Sie befinden sich hier: Willkommen auf meiner persönlichen Homepage! » Meine IT Artikel » My Solution on Assignment 1 - Problem 2
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
| — |
it-artikel:my-solution-on-assignment-1-problem-2 [2009-10-10 06:29] (aktuell) mail@awerner.homeip.net angelegt |
||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| + | ====== My Solution on Assignment 1 - Problem 2 ====== | ||
| + | |||
| + | This is my Solution of Assignment 1 - Problem 2 from the [[http://see.stanford.edu/see/courseinfo.aspx?coll=824a47e1-135f-4508-a5aa-866adcae1111|Computer Science Course CS106A of Prof. Mehran Sahami]] at the [[http://www.stanford.edu|STANDFORD University]]. | ||
| + | |||
| + | <code> | ||
| + | |||
| + | /* | ||
| + | * 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(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </code> | ||