Posted on Leave a comment

Karel the Robot Assignment 1 Problem 2

It looks like I got the assignment solved. It looks a bit like spaghetti code but it works on the default world.

[code lang=”java”]/*
* 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(){
//iterate 13 times(checking 13 columns)
for(int i=0;i<13;i++){
check();
fixColumn();
keepmoving();
}
//pseudocode
//go up the column
//check if there is a beeper
//if there is one
//go back to the start and
//fill in the beeper
//go check the next column
//repeat
}

// move to the wall
private void movetowall(){
while(frontIsClear()){
move();
}
}

//keep karel moving through the next column
private void keepmoving(){
if(frontIsBlocked()){
if(facingNorth()){
backtostart();
}
}

if(facingNorth()){
turnRight();
move();
//turnRight();
}
if(facingSouth()){
turnLeft();
move();
turnLeft();
}
if(facingWest()){
turnAround();
move();
}

}

//skip a beeper
private void skipbeeper(){
if(beepersPresent()){
move();
}
}

//fix the broken column
private void fixColumn(){
backtostart();
while(frontIsClear()){
skipbeeper();
while(noBeepersPresent()){

if(frontIsClear()){
while(noBeepersPresent()){
putBeeper();
move();
}
} else {
if(noBeepersPresent()){
putBeeper();
}
turnAround();
movetowall();
}
}
}
}

//move to the next column
private void nextcolumn(){
turnLeft();
move();
turnLeft();

}

//back to starting position
private void backtostart(){
turnAround();
movetowall();
turnAround();
}

//check if this a column that needs fixing
private void check(){
if(facingEast()){
turnLeft();
}
while(noBeepersPresent()){
if(frontIsClear()){
move();
} else {
turnAround();
movetowall();
nextcolumn();
backtostart();
}
}
}

}
[/code]

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.