asked 13.0k views
2 votes
Karel is in an ancient temple that contains a secret room where legends say all the deepest secrets of the universe are hidden. To gain access to the room though, Karel must solve the following puzzle.

All right should be left.
All that is left is right.
When matches are made,
They bring the light.

The room has alcoves on the left and the right. Some of the alcoves contain balls. For each alcove on the left side that contains a ball, karel should put a ball in the opposite alcove.

Karel should look into each alcove on the left side
If the alcove contains a ball, he should go to the opposite alcove and place a ball there.
When he is done, he should be in the top row, on the left side facing east.
None of the right hand alcoves will ever start with a ball.
The puzzle room always has the same layout, so the only difference between worlds is which alcoves contain balls.
ps( i need the code to work in all 4 worlds, and I am using super karel and the function you can use is move(); putBall(); takeBall(); turnLeft(); turnRight() and use loops, while conditions , if conditions and if/ else conditions and start with a start function USES JAVESCRIPT . please I need the code please emergency​

Karel is in an ancient temple that contains a secret room where legends say all the-example-1

1 Answer

5 votes

Answer: Here is the JavaScript code that solves the puzzle you described:

function start() {

while (frontIsClear()) {

checkLeft();

move();

}

checkLeft();

}

function checkLeft() {

if (ballsPresent()) {

takeBall();

turnAround();

move();

putBall();

move();

turnAround();

} else {

move();

}

}

Copy

The start() function is the entry point of the program. It moves Karel to the rightmost wall and then calls checkLeft() function to check each alcove on the left side.

The checkLeft() function checks if there is a ball in the current alcove on the left side. If there is, it takes the ball, moves to the opposite alcove, puts the ball there, and returns to the original alcove. If there isn’t a ball, it just moves to the next alcove.

This is what I think is the answer....Im not so sure

answered
User Ginger Opariti
by
9.0k points