Axel Werner's OPEN SOURCE Knowledge Base

Because Information Wants To Be Free!

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen gezeigt.

it-artikel:my-solution-on-assignment-1-problem-3 [2009-10-10 06:30] (aktuell)
mail@awerner.homeip.net angelegt
Zeile 1: Zeile 1:
 +====== My Solution on Assignment 1 - Problem 3 ======
 +
 +This is my Solution of Assignment 1 - Problem 3 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: CheckerboardKarel.java
 + * ----------------------------
 + * When you finish writing it, the CheckerboardKarel class should draw
 + * a checkerboard using beepers, as described in Assignment 1.  You
 + * should make sure that your program works for all of the sample
 + * worlds supplied in the starter folder.
 + */
 +
 +import stanford.karel.*;
 +
 +public class CheckerboardKarel extends SuperKarel {
 +
 +
 + public void run() {
 + /* fills a checkerboard of ANY SIZE.
 + *
 + * Pre-condition: Karel starts at 1,1 facing EAST
 + */
 + while( leftIsClear() ) {
 + fillLine();
 + goLineUp();
 + }
 + fillLine();
 + }
 +
 +
 + private void fillLine() {
 + /* Fills a whole Line with either Pattern1 or Pattern2
 + * depending on what kind of Pattern has been set on the
 + * last line (1 row down)
 + */
 +
 + // check if this is NOT the FIRST LINE
 + if( rightIsClear() ) {
 +
 + turnRight();
 + move();
 + if ( beepersPresent() ) {
 + turnAround();
 + move();
 + turnRight();
 + fillLineWithPattern2();
 + }
 + else {
 + turnAround();
 + move();
 + turnRight();
 + fillLineWithPattern1();
 + }
 + }
 + else {
 + // So this is the FIRST line ...
 + fillLineWithPattern1();
 + }
 +
 + }
 +
 + private void fillLineWithPattern1() {
 + /* Fills a whole Line with Pattern1
 + */
 + while( frontIsClear() ) {
 + putBeeper();
 + move();
 + // Front STILL clear?? Then do another Step.
 + if( frontIsClear() ) {
 + move();
 + }
 + }
 +
 + // GO ONE STEP BACK
 + turnAround();
 + move();
 + turnAround();
 +
 + if( noBeepersPresent() ) {
 + move();
 + putBeeper();
 + }
 +
 + returnToX1();
 + }
 +
 + private void returnToX1() {
 +
 + // Now Go Back to X=1 (Left Wall) facing EAST
 + turnAround();
 + while( frontIsClear() ) {
 + move();
 + }
 + turnAround();
 + }
 +
 +
 + private void fillLineWithPattern2() {
 + /* Fills a whole Line with Pattern2
 + */
 + while( frontIsClear() ) {
 + move();
 + putBeeper();
 + // Front STILL clear?? Then do another Step.
 + if( frontIsClear() ) {
 + move();
 + }
 + }
 + returnToX1();
 + }
 +
 +
 +
 + private void goLineUp() {
 + /* Jumps ONE LINE UP
 + */
 + turnLeft();
 + move();
 + turnRight();
 + }
 +}
 +
 +</code>

Eigene Werkzeuge