John Gamble


Array::Tour::RandomWalk - Return coordinates to take a random path.


      use Array::Tour::RandomWalk qw(:directions);
      my $rndwalk = Array::Tour::RandomWalk->new(
          dimensions => [13, 7, 1],
          start => [0, 0, 0]
          backtrack => "queue",

The object is created with the following attributes:


Set the size of the grid:

        my $spath1 = Array::Tour->new(dimensions => [16, 16]);

If the grid is going to be square, a single integer is sufficient:

        my $spath1 = Array::Tour->new(dimensions => 16);

Default value: [0, 0, 0]. Starting point of the random walk.


Default value: 'queue'. Method of looking up cells to backtrack to. As the random walk is made, cells where there were more than one direction to go are stored in a list. When the random walk hits a dead end, it goes back to a cell in the list. By default, the list is treated as a queue: the first cell on the list is the first cell used.

If backtrack is set to 'stack', the list is treated as a stack: the last cell on the list is the first cell used.

The final choice, 'random', will choose a cell at random from the list.

The new() method is defined in the Array::Tour class. Attributes unique to this class are dealt with in its own _set() method.


Perl 5.8 or later. This is the version of perl under which this module was developed.


A simple iterator that will return the coordinates of the next cell if one were to randomly tour a matrix.


Returns the current direction as found in the :directions EXPORT tag.

Overrides Array::Tour's direction() method.

Tour Object Methods


Returns an array reference to the next coordinates to use. Returns undef if the object is finished.

Overrides Array::Tour's next() method.

Internal Tour Object Methods



Overrides Array::Tour's _set() method.


The default function used to perform the random walk.

The function may be overridden if a function is referenced in {wander}. This function will take two arguments, a reference the list of possible directions to move to, and a reference to the position (an array of [column, row, level]).


  @directions = $obj->_collect_dirs($c, $r, $l);

Find all of our possible directions to wander through the array. You are only allowed to go into not-yet-broken cells. The directions are deliberately accumulated in a counter-clockwise fashion.


See Also

Array::Tour Games::Maze


John M. Gamble may be found at <>