Search code examples
javascript

Stanford Karel Unit 8, Lesson 5


New to coding and to Javascript. Working through the Stanford Karel lessons right now, and have got stumped on this one

Here is my code:

//Make Karel fill the world
//with beepers
function main() {
    step1();
    reset();
    step2();
}

function putBeeperLine(){
    putBeeper();
    while(frontIsClear()) {
        move();
        putBeeper();
    }
}

function right(){
    turnRight();
    move();
    turnRight();
}

function left(){
    turnLeft();
    move();
    turnLeft();
}

function step1(){
    repeat(2){
        putBeeperLine();
        left();
        putBeeperLine();
        right();
    }
    putBeeperLine();
}

function step2() {
    putBeeperLine();
    left();
    putBeeperLine();
    right();
    putBeeperLine();
}

function reset(){
    left();
    while(frontIsClear()){
        move();}
        turnAround();
}

If I run function step1(), the code executes, and the requirements for the 5x5 world are satisfied. If, however, I proceed and run the rest of the code, the 8x8 gets satisfied, but the 5x5 is suddenly no longer valid. Can someone please explain this, and, ideally, help me think through this without giving me the answer?

Thank you all so much!


Solution

  • I've been attempting the lessons as well, and after many attempts I finally managed to get past said problem, but I "cheated" a little by using an 'if' statement, which I'm pretty sure isn't covered yet (I just finished Unit 8).

    This is not made very clear in Lesson 5, but in Unit 8 Lesson 2, the comment in the code given says "no matter how big the world". In other words, your code must be able to automatically fill in any world size (e.g. 5x5, 8x8, 16x16 ...). Which is also why there are 2 goals. My belief is that because you hard-coded the number of times (for instance your use of repeat()) to fill the world row by row, and thus the program doesn't accept your answer because it only works for ONE specific world size.

    I'm currently working on a solution that doesn't involve the use of 'if', and I'll share it here if I can manage to get it working, no guarantees however.

    EDIT: I'm still unable to come up with a solution that doesn't involve 'if' statement, but below is my solution for this exercise (for those who want to progress to the next section). How it works: first fill a line of beepers, Karel will then check if there is not obstruction (walls) above it (using leftIsClear()). If there is indeed no obstruction, Karel will reposition itself to the first column and fill up a line of beepers. If there is obstruction, program terminates. Spoilers below.

    //Make Karel fill the world
    //with beepers
    function main() {
       //your code here
       putBeeperLine();
       while(leftIsClear()){
          checkRowAbove();
          putBeeperLine();
       }
    }
    
    function checkRowAbove(){
       if(leftIsClear()){
          turnLeft();
          move();
          resetPosition();
       }
    }
    
    function resetPosition(){
       turnLeft();
       while(frontIsClear()){
          move();
       }
       turnAround();
    }
    
    function putBeeperLine(){
       putBeeper();
       while(frontIsClear()) {
          move();
          putBeeper();
       }
    }