StoneMasonKarel Solution

22 Oct

So the second assignment is a bit harder and requires some “while” and “if” logic. Here is the problem:

Here is my solution:


Update: I’ve had several people email me and say that this is not the fully correct solution. Although my solution works in Karel’s current world, you can’t assume the columns he’s stoning are 5 units high. Check out the comments for some better solutions or post your own!

<pre>/*
 * 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 () {
		while (frontIsClear()) {
			BeeperUp();
			MoveUp();
			BeeperDown();
			MoveDown();
		}
	}
	private void BeeperUp() {
		turnLeft();
		while (frontIsClear()) {
			if (noBeepersPresent()) {
			putBeeper();
			}
			move();
		}
	}
	private void BeeperDown() {
		turnRight();
		while (frontIsClear()) {
			if (noBeepersPresent()) {
			putBeeper();
			}
			move();
		}
	}
	private void MoveUp() {
		turnRight();
		for (int i=0; i&lt;4; i++) {
			move();
		}
	}
	private void MoveDown() {
		turnLeft();
		if (frontIsClear()) {
			for (int i=0; i&lt;4; i++) {
				move();
			}
		}
	}
}</pre>

I will try to put in more explanation for what my logic is for future solution. This one took me a while to figure out, and I was really really proud when I made it work!

Advertisements

15 Responses to “StoneMasonKarel Solution”

  1. nick January 5, 2012 at 4:54 pm #

    this doesn’t solve the problem i’m afraid!

    i’m very new to this, so i can’t tell you the right way yet, but the columns can be *different* heights. if you lower one column below the first you get an error. i’ll see if i can find the answer.

    • nick January 5, 2012 at 5:31 pm #

      finally got it, hope this helps someone:

      import stanford.karel.*;
      
      public class StoneMasonKarel extends SuperKarel {
      
      	public void run () {
      		while (frontIsClear()) {
      			turnLeft();
      			fixColumn();
      			returnToStart();
      			moveToNext();
      		}
      		fixLastColumn();
      	}
      	
      	private void fixColumn() {
      		while (frontIsClear()) {
      			if (noBeepersPresent()) {
      				putBeeper();
      			} else { 
      				move();
      			}
      		}
      	}
      	
      	private void returnToStart() {
      		turnAround();
      		fixColumn();
      		turnLeft();
      	}
      	
      	private void moveToNext() {
      		for (int x=0; x&lt;4; x++) {
      			move();
      		}
      	}
      	
      	private void fixLastColumn() {
      		if (leftIsBlocked()){
      			if (noBeepersPresent()) {
      				putBeeper();
      		} else {
      			turnLeft();
      			fixColumn();
      			returnToStart();
      		}
      		}
      	}
      }
      
      • Natasha Murashev January 5, 2012 at 5:32 pm #

        you can put the following tags around your code so it’s nicer to read:

        [sourcecode language=""css""]

        //your code goes here

        [/sourcecode]

        Thanks for sharing!

    • Natasha Murashev January 5, 2012 at 5:31 pm #

      Looks like you are correct. Karel needs to first check if there is a wall above or below it once it reaches a new column, and if there isn’t, Karel needs to clean up the extra beepers above / below it.

  2. nick January 5, 2012 at 5:41 pm #

    oh, and thanks for the site btw, very useful!

    • Natasha Murashev January 5, 2012 at 5:45 pm #

      Glad you like it 🙂 I started it because I had a hard time finding some of the solutions myself. The disclaimer of course is that I’m a newbie just like everyone else, so if you find mistakes, definitely let me know like you did just now.

  3. Sanjay Singh February 10, 2012 at 4:41 am #

    Code posted by nick does not work. It has quite few flaws and does not work under other worlds. My code for same is given below, it factors all of conditions required in Assignment file :

     
    import stanford.karel.*;
    
    public class StoneMasonKarel extends SuperKarel {
    
    	public void run() {
    		while (facingEast()) {
    			ascendColumn();
    			descendColumn();
    			moveFourSteps();
    		}
    		turnLeft();
    	}
    
    	private void ascendColumn() {
    		if (leftIsClear()) {
    			turnLeft();
    			while (frontIsClear()) {
    				move();
    			}
    			turnAround();
    		} else {
    			dropBeeper();
    
    		}
    	}
    
    	private void descendColumn() {
    		if (facingSouth()) {
    			dropBeeper();
    			while (frontIsClear()) {
    				move();
    				dropBeeper();
    			}
    			if (leftIsClear()) {
    				turnLeft();
    			}
    		}
    	}
    
    	private void dropBeeper() {
    		if (noBeepersPresent()) {
    			putBeeper();
    		}
    	}
    
    	private void moveFourSteps() {
    		if (frontIsClear()) {
    			for (int i = 0; i < 4; i++) {
    				move();
    			}
    		}
    	}
    }
    
     
    
  4. Sanjay Singh February 10, 2012 at 4:43 am #
    import stanford.karel.*;
    
    public class StoneMasonKarel extends SuperKarel {
    
    	public void run() {
    		while (facingEast()) {
    			ascendColumn();
    			descendColumn();
    			moveFourSteps();
    		}
    		turnLeft();
    	}
    
    	private void ascendColumn() {
    		if (leftIsClear()) {
    			turnLeft();
    			while (frontIsClear()) {
    				move();
    			}
    			turnAround();
    		} else {
    			dropBeeper();
    
    		}
    	}
    
    	private void descendColumn() {
    		if (facingSouth()) {
    			dropBeeper();
    			while (frontIsClear()) {
    				move();
    				dropBeeper();
    			}
    			if (leftIsClear()) {
    				turnLeft();
    			}
    		}
    	}
    
    	private void dropBeeper() {
    		if (noBeepersPresent()) {
    			putBeeper();
    		}
    	}
    
    	private void moveFourSteps() {
    		if (frontIsClear()) {
    			for (int i = 0; i &lt; 4; i++) {
    				move();
    			}
    		}
    	}
    }
    
  5. ProgrammingZero February 18, 2012 at 6:33 am #

    Thanks for a great blog for us doing CS106a through iTunes U. Here is my solution, also posted here: http://programzero.wordpress.com/2012/02/18/stanford-cs106a-assignment-1-problem-2/

    import stanford.karel.*;
    
    public class StoneMasonKarel extends SuperKarel {
    	public void run(){
    		while(frontIsClear()){	//Karel will do this loop as ong as its front is clear
    			fixColumn();
    			moveToNext();
    		}
    		fixColumn(); //To fix the last column, avoid OBOB
    	}
    
    	//Moves Karel to next column. Pre cond: facing east
    	private void moveToNext() {
    		for (int i=0; i<4; i++){
    			move();
    		}
    
    	}
    
    	//Makes Karel fix one column. Pre cond: facing east.
    	//End condition: facing east
    	private void fixColumn(){
    		turnLeft();
    		while(frontIsClear()){			//If the front is clear
    			if(beepersPresent()){		//AND any beepers present at current corner
    				move();					//Karel will move
    			}
    
    			else{						//if not, he will put a beeper and move
    				putBeeper();
    				move();
    			}
    		}
    
    		if(noBeepersPresent()){ //fixes the top of the column
    			putBeeper();
    		}
    		returnToBottom();
    		turnLeft();
    	}
    
    	//Makes Karel return to bottom om column. Pre cond: facing North
    	//End cond: facing south, 1st avenue
    	private void returnToBottom(){
    		turnAround();
    		while(frontIsClear()){
    			move();
    		}
    	}
    }
  6. Tamar March 4, 2012 at 11:04 pm #

    Dear Natasha,

    I appreciate your site very much!
    It is very clearly set up and I enjoy all the aids you provide.
    I’ve just started the CS106A myself but have a hard tme concentrating on it 100% (I also have a full-time job and 4 kids so you can imagine 😉 )
    Anyway, at this time I am struggling with the second assignment and looked at your solutions here, I hope they wilol help me understand better and find my own original solution, I only have experience with programming RPG on IBM-AS400 systems and that is quite different.

    I guess I will gbe comming back here a lot so I just wanted to say that your efforts are appreciated!
    Thanks!

    Tamar

    • Natasha Murashev March 5, 2012 at 9:05 am #

      Hi Tamar,

      Glad to hear you find these helpful 🙂 Keep going with the course – it is definitely worth it! The first few assignments are extra hard to “weed out” the weak students in the class, since this is Stanford, but after the third assignment it gets A LOT easier.

      Best,
      Natasha

  7. Tamar March 6, 2012 at 7:14 am #

    Just changed my program and it is working much better, it really helps looking at other persons solutions to start understanding the stuff better, i am enjoying it very much, too bad i have only about two hours each week, but at least i print out the code and think of ideas on the bus, which can be very helpfull if i dont fall asleep on the way 😉

    • Scott June 19, 2012 at 7:20 pm #

      So, looks like “ProgrammingZero” nailed it. I finally got mine working. It isn’t as bulletproof, but I got it working for a few “worlds” and I thought I’d share anyways. I’m glad to see others out there taking this course and I’m glad we can all learn from each other. Thanks for the site!

      import stanford.karel.SuperKarel;
      
      public class StoneMasonKarel extends SuperKarel {
      	public void run(){
      		turnLeft();
      		putBeeper();
      		placeIfNoBeepersFound();
      		runGauntlet();
      	}
      	
      	private void placeIfNoBeepersFound() {
      		while (frontIsClear()){
      			move();
      			if(noBeepersPresent()) {
      			  putBeeper();
      			  }
      		}
      	}
      
      	private void runGauntlet() {
      		for(int i=0; i<5; i++){
      		turnIfBlocked();
      		moveEveryFour();
      		}
      	}
      	
      	private void turnIfBlocked() {
      		while (frontIsBlocked()) {
      			if (facingNorth() && rightIsClear()) {
      				turnRight();
      			} else if (facingEast() && rightIsClear()) {
      				turnRight();
      				move();
      				turnRight();
      					if (noBeepersPresent()){
      						putBeeper();
      					}
      			} else if (facingWest() && leftIsClear()) {
      				turnLeft();
      				move();
      				turnLeft();
      					if (noBeepersPresent()){
      						putBeeper();
      						}
      			} else if (facingWest() && leftIsBlocked()) {
      				turnAround();
      					if (noBeepersPresent()){
      						putBeeper();
      						}			
      				} else if (frontIsClear()) {
      			moveEveryFour();
      			}
      		}
      	}
      
      	private void moveEveryFour() {
      		while (frontIsClear()) {
      			for(int j=0; j<4; j++){
      				move();
      			}
      			if (noBeepersPresent()) {
      			putBeeper();			
      			}
      		}
      	}
      }
      
      • Natasha Murashev June 20, 2012 at 7:33 am #

        Thanks for sharing Scott! My answers may or may not be “correct” 🙂

Trackbacks/Pingbacks

  1. Stanford CS106a Assignment 1 – Problem #2 « Programming… - February 18, 2012

    […] Natasha The Robot’s solution Share this:TwitterFacebookLike this:LikeBe the first to like this post. Filed under CS106a, JAVA and tagged assignment #1, Computer Programming, CS106a, Eclipse, Java, Karel the robot, solution, source code, Stanford | Leave a comment […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s