Skip to content

Instantly share code, notes, and snippets.

@NatashaTheRobot
Created October 28, 2011 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save NatashaTheRobot/1321614 to your computer and use it in GitHub Desktop.
Save NatashaTheRobot/1321614 to your computer and use it in GitHub Desktop.
This is the solution to the StoneMasonKarel problem in the online Stanford CS 106A class
/*
* 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<4; i++) {
move();
}
}
private void MoveDown() {
turnLeft();
if (frontIsClear()) {
for (int i=0; i<4; i++) {
move();
}
}
}
}
@michaelGregoire
Copy link

Your solution actually breaks when columns are not the same height. If you load the "SampleQuad2" world, you'll see what I mean. The following is my solution which works on all 3 StoneMasonKarel worlds. My solution also reflects a more lifelike approach. When one fixes columns, I'd imagine, you'd climb them, fix them and then climb back down to move over to the next column via the floor:

/*
 * 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() {
        repairColumn();
    }

    private void repairColumn() {
        turnLeft();
        ascendColumn();
        turnAround();
        descendColumn();
        turnLeft();
        if(frontIsClear()) {
            moveToNextColumn();
            repairColumn();
        }
    }   
    private void ascendColumn() {
        while(frontIsClear()) {
            if(noBeepersPresent()) {
                putBeeper();
            }
            move();
        }
        if(noBeepersPresent()) {
            putBeeper();
        }
    }
    private void descendColumn() {
        while(frontIsClear()) {
            move();
        }
    }
    private void moveToNextColumn() {
        for(int i=0; i<4; i++) {
            move();
        }
    }
}

@NatashaTheRobot
Copy link
Author

Michael, thanks for posting the correct solution. I give not guarantee that any of my solutions are correct, as I was just learning this stuff at the time. In fact, there are probably much better solutions than mine to all the problems in the course.

@frxntier
Copy link

I created my own version of this before looking at too much detail here. I think Michael's solution might be more elegant, because everything is separated more logically...

/*
 * 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 {

//  "run" enables overall process

    public void run() {
        while (frontIsClear()) {
            fixArch();
            goHome();
            moveNext();
        }
        fixArch();
        goHome();
    }

//  fixArch enables Karel to fill in blanks
//  on each arch he comes to

    private void fixArch() {
        turnLeft();
        fillAllHoles();
    }

//  inside the arch, while Karel's front is clear,
//  he will fill all holes that need filling.
//  When his front is NOT clear, he has come to the end,
//  and so will fill the hole if needed then move to
//  "goHome"

    private void fillAllHoles() {
        while (frontIsClear()) {
            fixHole();
            move();
        }
        fixHole();
    }

    private void fixHole() {
        if (noBeepersPresent()) {
            putBeeper();
        }
    }

    private void goHome() {
        turnAround();
        while (frontIsClear()) {
            move();
        }
    }

    private void moveNext() {
        turnLeft();
        for (int i=0; i<4; i++) {
            move();
        }
    }
}

@qunYang
Copy link

qunYang commented Nov 12, 2016

import stanford.karel.*;

public class StoneMasonKarel extends SuperKarel {

public void run() {

	while(frontIsClear()) {
		findTheWay();
		repaironecolumn();
		findthedirection();
		movetonextcolumn();
	}
	findTheWay();
	repaironecolumn();

}
//while on the button turnLeft ,on the top turnRight
private void findTheWay() {
if(leftIsBlocked()){
turnRight();
}else{
turnLeft();
}
}
//repair one column

private void repaironecolumn() {
while (frontIsClear()){
if (beepersPresent()){
move();
}else{
putBeeper();
move();
}
}
if (beepersPresent()){

}else{
	putBeeper();
}

}
//while finish one column turn to east
private void findthedirection() {
if(notFacingNorth()){
turnLeft();
}else{
turnRight();
}
}

private void movetonextcolumn(){
for(int i=0;i<4;i++){
move();
}
}
}

@Harmonyka
Copy link

import stanford.karel.*;

public class StoneMasonKarel extends SuperKarel {

public void run(){
while(frontIsClear()){
repairColumns();
moveToNextColumns();
}
repairColumns();
}

private void repairColumns(){

if(rightIsBlocked()){
turnLeft();
}
else {
turnRight();
}

while (frontIsClear()){
if(noBeepersPresent()){
putBeeper();}
move();
}
}

private void moveToNextColumns(){
if(facingNorth()){
turnRight();
}
else {
turnLeft();}
for (int i = 0; i < 4 ; i++){
move();
}
}
}

@Rohit5544
Copy link

/* Hey Check out my solution for Stone Mason Karek the robot problem */

import stanford.karel.*;

public class StoneMasonKarel extends SuperKarel {

public void run() {
	turnLeft();
	moveForward();
	turnRight();
	moveIt();
	putBeeper();
	turnRight();
	moveForward();
	turnLeft();
	moveIt();
	putBeeper();
	turnLeft();
	moveForward();
	turnRight();
	moveIt();
	turnRight();
	moveForward();
	turnLeft();

}

private void moveForward() {
	for (int i = 0; i <= 3; i++) {
		if (beepersPresent()) {
			move();
		} else {
			putBeeper();
			move();
		}
	}

}

private void moveIt() {
	for (int i = 0; i <= 3; i++) {
		move();
	}
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment