You can teach Karel to turn right by defining a new function (or method)
called turnRight
that makes Karel turn left three times. Since Karel's basic
commands do not include a turn right, turning left three times effectively
achieves the same result as turning right. For example, in Java-like Karel
code:
java
private void turnRight() {
turnLeft();
turnLeft();
turnLeft();
}
Then, whenever you want Karel to turn right, you simply call turnRight()
instead of writing three turnLeft()
commands. This makes your code shorter,
clearer, and easier to read
. In summary:
- Karel only has a
turnLeft()
command by default. - To simulate
turnRight()
, executeturnLeft()
three times. - Define a new method
turnRight()
to encapsulate this behavior. - Use
turnRight()
in your program to make the code more readable and maintainable.
This approach is a common programming technique to extend the capabilities of Karel by creating new commands from existing ones