NAME

Karel::Robot::WithGrid

DESCRIPTION

A robot with an associated grid. To create the robot, use

my $robot = 'Karel::Robot'->new;
my $grid  = 'Karel::Grid'->new(x => 10, y => 12);
$robot = $robot->set_grid($grid, 1, 1);

METHODS

$robot->x, $robot->y
my ($x, $y) = map $robot->$_, qw( x y );

Coordinates of the robot in its grid.

$robot->grid
my $grid = $robot->grid;

The associated Karel::Grid object.

$robot->set_grid($grid, $x, $y, $direction);

Initialize the grid. Grid must be an object of the Karel::Grid type, $x and $y are coordinates of the robot, $direction is one of N E S W (defaults to N). Dies if the robot's place is occupied by a wall.

$robot->drop_mark

Drop mark in the current location. Dies if there are already 9 marks.

$robot->pick_mark

Picks up one mark from the current location. Dies if there's nothing to pick.

$robot->direction
my $direction = $robot->direction;

Returns the robot's direction: one of qw( N W S E ).

$robot->left

Turn the robot to the left.

$robot->coords

Returns the robot's coordinates, i.e. x and y.

$robot->cover

Returns the grid element at the robot's coordinates, i.e.

$r->grid->at($r->coords)
$robot->facing_coords

Returns the coordinates of the grid element the robot is facing.

$robot->facing

Returns the contents of the grid element the robot is facing.

$robot->current

For debugging Karel programs: returns the source of the currently executed command, current position in the source and the length of the command.

$robot->run($command_name)

Run the given command.

$robot->forward

Moves the robot one cell forward in its direction.

$robot->repeat($count, $commands)

Runs the repeat command: decreases the counter, and if it's non-zero, pushes the body to the stack. Returns 0 (CONTINUE) when it should stay in the stack, 1 (FINISHED) otherwise.

$isnot_south = $robot->condition('!S')

Solve the given condition. Supported parameters are:

  • N E S W

    Facing North, East, South, West

  • m

    Covering mark(s).

  • w

    Facing a wall.

  • !

    Negates the condition.

Returns true or false, dies on invalid condition.

$robot->If($condition, $commands, $else)

If $condition is true, puts $commands to the stack, otherwise puts $else to the stack. Returns 2 (FINISH_DELAYED) in the former case, 1 (FINISHED) in the latter one.

$robot->While($condition, $commands)

Similar to If, but returns 0 (CONTINUE) if the condition is true, i.e. it stays in the stack.

$robot->call($command)

Checks whether the robot knows the command, and if so, pushes its definition to the stack. Dies otherwise. Returns 2 (FINISH_DELAYED).

$robot->stop

Stops execution of the current program and clears the stack. Returns -1 (QUIT).

$robot->step

Makes one step in the currently running program.

RETURN VALUES

There are three special return values corresponding to the stack handling:

0 CONTINUE
1 FINISHED
2 FINISHED_DELAYED

If a command returns CONTINUE, the stack doesn't change. If it returns FINISHED, the following command in the stack is executed. If it returns FINISHED_DELAYED, new commands are put in the stack, but once they're finished, the command behaves as if finished, too.