Basic Karel J Robot Commands


move();                // moves 1 unit forward
turnLeft();            // turns to the left 90 degrees
pickBeeper();      // picks up 1 beeper and puts in bag
putBeeper();        // puts 1 beeper down
turnOff();            // shuts off

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
if ( nextToABeeper())
{
    pickBeeper();
}
else
{
    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();
}


EXAMPLE: creates a new boolean method called rightIsClear
public boolean rightIsClear()
{	
  turnRight();
  if ( frontIsClear() ) 
  {
    turnLeft();
    return true;
  }
  turnLeft();
  return false;
}