Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment 1 - StoneMasonKarel #1

Open
ghost opened this issue Feb 20, 2012 · 0 comments
Open

Assignment 1 - StoneMasonKarel #1

ghost opened this issue Feb 20, 2012 · 0 comments

Comments

@ghost
Copy link

ghost commented Feb 20, 2012

Your solution breaks on the SampleQuad2 map. Ive made a solution for this but not sure if it's the best way of doing it.

/*
 * 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()) {
        ColRepair();
        nextCol();
        for(int i = 0; i < 4; i++) {
            move();
            if(frontIsBlocked()) {
                ColRepair();
                nextCol();
            }
        }
    }
}
private void ColRepair() {
    turnLeft();
    for(int i = 0; i < 4; i++) {
        while(beepersPresent() && frontIsClear()) {
            move();
            if(!beepersPresent()) {
                putBeeper();
            }
        }
        beeperCheck();
    }
}
private void beeperCheck() {
    if(noBeepersPresent()) {
        putBeeper();
        if(frontIsClear()) {
            move();
        }
    }
}
private void nextCol() {
    turnAround();
    while(frontIsClear()) {
        move();
    }
    turnLeft();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants