Basic Karel J Robot Commands
Boolean Tests
frontIsClear() // returns true if front is not blocked
nextToABeeper() // returns true if standing on a beeper
nextToARobot() // returns true if at same location as another robot
facingNorth() // returns true if facing north
facingSouth() // returns true if facing south
facingEast() // returns true if facing east
facingWest() // returns true if facing west
anyBeepersInBeeperBag() // returns true if there are any beeper in bag
Location Methods
int avenue() // returns the avenue number of the robot
int street() // returns the street number of the robot
The 'if/else' Instruction
if ( <boolean test> ) { <instruction-list> } else { <instruction-list> }EXAMPLE: if robot is on a beeper, pick it up, otherwise move
The 'for' instruction allows Karel to repeat another instruction a number of times.
for (int i = 0; i < someNumber; ++i)
{
<instruction-list>
}
The WHILE instruction repeats itself as long as <test> is true.
while ( <boolean test> ) { <instruction-list> }EXAMPLE: move forward 5 times
for (int i = 0; i < 5; ++i)
{
move()
}
EXAMPLE: pick up all beepers on a corner
while ( nextToABeeper() )
{
pickBeeper();
}
public boolean rightIsClear() { turnRight(); if ( frontIsClear() ) { turnLeft(); return true; } turnLeft(); return false; }