1

This is the code that I tried.

function main() {
  while (frontIsClear()) {
    //this tells karel to go and put beepers
    putBeeper();
    move();
  }
  if (frontIsBlocked()) {
    //this tells karel to change the direction if there's a wall
    if (facingEast()) {
      turnLeft();
      putBeeper();
      if (frontIsClear()) {
        //and this brings karel to the upper street
        move();
        turnLeft();
      } else {
        turnRight();
      }
    }
    if (facingWest()) {
      turnRight();
      putBeeper();
      if (frontIsClear()) {
        move();
        turnRight();
      } else {
        turnRight();
      }
    }
  }
}

When I run the code it gives me an error. It says ReferenceError: virtualDirection is not defined. Pls help. Thanks.

This is the goal. This is the Karel website.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
anon
  • 73
  • 9

2 Answers2

1

The problem with that code is that facingWest() and facingEast() don't seem to be functions. Instead, they're properties, so use facingEast and facingWest (i.e. use them without brackets). That gets rid of the error but facingEast and facingWest are never true based on my testing, which means you can't make it turn based on that.

So, I think you'll have to write some code that doesn't rely on the facing commands. As long as you can assume that Karel starts at the bottom left of a grid, you can just turn when the front is blocked, starting left and then alternating to right. Something like this should work, you'll just have to edit this to make sure it doesn't tell you front is blocked at the end of traversing an even-numbered grid.

function main(){
   while (leftIsClear()){
      putBeeperRow();
      turnForClearLeftRow();
      putBeeperRow();
      turnForClearRightRow();      
   }
   putBeeperRow();
}

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

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

function putBeeperRow() {
   while(frontIsClear()){ 
      putBeeper();
      move();
   }
   putBeeper();
}
Shan S
  • 658
  • 5
  • 18
  • I tried it and I have a question. Would you mind giving a bit of explanation for the first part of the code? `while (leftIsClear()){ putBeeperRow(); turnForClearLeftRow(); putBeeperRow(); turnForClearRightRow(); } putBeeperRow(); }` – anon Jul 18 '20 at 14:16
  • To make Karel put beepers in all the rows, starting from the bottom left, it has to do 3 things repeatedly. It has to fill a row with beepers, turn left, fill a new row with beepers, then turn right and fill again. When all the blocks are filled on the left side, it means all the previous rows have been filled with beepers, so I use a `while` loop, knowing that when all the blocks are filled, it will stop. But, when the while loop stops, it doesn't actually fill the last row because the condition isn't met. So, I have to add an extra `putBeeperRow` to finish the last row. Does this make sense? – Shan S Jul 18 '20 at 16:07
1

I changed it a bit. Here's the code that works in both even and odd numbered grids.

function main(){
   while (leftIsClear()){
      putBeeperRow();
      turnForCleanLeftRow();
      putBeeperRow();
      turnForCleanRightRow();
   }
   putBeeperRow();
}
function turnForCleanLeftRow(){
   turnLeft();
   move();
   turnLeft();
}
function turnForCleanRightRow(){
   turnRight();
   if (frontIsClear()){
      move();
      turnRight();
} else {
   turnRight();
   while (frontIsClear()){
      move();
   }
   pickBeeper();
  }
}
function putBeeperRow(){
   while(frontIsClear()){
      putBeeper();
      move();
   }
   putBeeper();
}
anon
  • 73
  • 9