Skip to content

Instantly share code, notes, and snippets.

@NatashaTheRobot
Created October 28, 2011 04:22
Show Gist options
  • 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();
}
}
}
}
@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