Commits

Anonymous committed 5dd3224

Began adding get_letters_of_clue.

Comments (0)

Files changed (2)

abc-path/Games-ABC_Path-Generator/lib/Games/ABC_Path/Generator/RiddleObj.pm

 use strict;
 use warnings;
 
+use Carp;
+
 use integer;
 
 use base 'Games::ABC_Path::Solver::Base';
     return;
 }
 
+=head2 my [$letter1, $letter2] = $riddle->get_letters_of_clue({ type => $type, index => $index, })
+
+Returns the two letters (as an array reference) associated with the 
+clue represented by the hash reference. Type can be:
+
+=over 4
+
+=item * 'col'
+
+A column clue. C<'index'> points to the X coordinate.
+
+=item * 'row'
+
+A row clue. C<'index'> points to the Y coordinate.
+
+=item * 'diag'
+
+The diagonal clue. C<'index'> is ignored.
+
+=item * 'antidiag'
+
+The anti-diagonal clue. C<'index'> is ignored.
+
+=back
+
+Some examples:
+
+    my $letters_aref = $riddle->get_letters_of_clue({ type => 'col', index => 2, });
+    my $letters_aref = $riddle->get_letters_of_clue({ type => 'row', index => 1, });
+    my $letters_aref = $riddle->get_letters_of_clue({ type => 'diag', });
+    my $letters_aref = $riddle->get_letters_of_clue({ type => 'antidiag', });
+
+=cut
+
+sub get_letters_of_clue
+{
+    my ($self, $args) = @_;
+
+    my $get_index = sub {
+        my $i = $args->{index};
+
+        if ($i !~ m{\A[01234]\z})
+        {
+            Carp::confess ('index must be in the range 0-4');
+        }
+
+        return $i;
+    };
+
+    my $clue_idx;
+    my $type = $args->{type};
+
+    if ($type eq 'col')
+    {
+        $clue_idx = 2 + $LEN + $get_index->();
+    }
+    else
+    {
+        Carp::confess ("Unknown type $type.");
+    }
+    
+    return [map { $letters[$_-1] } @{$self->_clues->[$clue_idx]}];
+}
+
 =head2 my $string = $riddle->get_riddle_v1_string()
 
 Returns the riddle version 1 string (without the header). See the documentation

abc-path/Games-ABC_Path-Generator/t/riddle-obj.t

 use strict;
 use warnings;
 
-use Test::More tests => 8;
+use Test::More tests => 9;
 use Test::Differences;
 
 use Games::ABC_Path::Generator;
         "get_riddle_v1_string()",
     );
 
+    # TEST
+    eq_or_diff($riddle->get_letters_of_clue({ type => 'col', index => 1, }),
+        ['B', 'X',],
+        'get_letters_of_clue type col',
+    );
+
     my $layout = $riddle->get_final_layout();
 
     # TEST