Commits

shl...@b384bcd7-cfd4-0310-aca0-d78b80f7b91b  committed 7efcf6f

Add ::FinalLayoutObj.

Extract some methods there.

  • Participants
  • Parent commits 69ab057

Comments (0)

Files changed (6)

File abc-path/Games-ABC_Path-Generator/MANIFEST

 Changes
 inc/Test/Run/Builder.pm
 lib/Games/ABC_Path/Generator/App.pm
+lib/Games/ABC_Path/Generator/FinalLayoutObj.pm
 lib/Games/ABC_Path/Generator.pm
 lib/Games/ABC_Path/Generator/RiddleObj.pm
 lib/Games/ABC_Path/MicrosoftRand.pm

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

 use Games::ABC_Path::MicrosoftRand;
 
 use Games::ABC_Path::Generator::RiddleObj;
+use Games::ABC_Path::Generator::FinalLayoutObj;
 
 =head1 NAME
 
 Calculates the final, solved, layout of the board based on the current
 random seed.
 
-Returns the layout as an object.
+Returns the layout as a L<Games::ABC_Path::Generator::FinalLayoutObj>
+object.
 
 =cut
 
 
         if (@dfs_stack == $BOARD_SIZE)
         {
-            return $l;
+            return Games::ABC_Path::Generator::FinalLayoutObj->new(
+                {layout_string => $l, },
+            );
         }
 
         # print "Depth = " . scalar(@dfs_stack) . "\n";
     die "Not found!";
 }
 
-=head2 $gen->get_layout_as_string($layout)
-
-For internal use.
-
-=cut
-
-sub get_layout_as_string
-{
-    my ($self, $l) = @_;
-
-    my $render_row = sub {
-        my $y = shift;
-
-        return join(" | ", 
-            map { my $x = $_; my $v = vec($l, $self->_xy_to_int([$y,$x]), 8);
-            $v ? $letters[$v-1] : '*' } (0 .. $LEN - 1));
-    };
-
-    return join('', map { $render_row->($_) . "\n" } (0 .. $LEN-1));
-}
 
 my $NUM_CLUES = (2+5+5);
 
 
     my $layout = $self->calc_final_layout();
 
-    my $A_pos = index($layout, chr(1));
+    my $A_pos = $layout->get_A_pos;
 
     my %init_state = (pos_taken => '', 
         clues =>
                 my $handle_clue = sub {
                     my @cells = @{shift->{cells}};
                     $self->_fisher_yates_shuffle(\@cells);
-                    return [map { vec($layout, $_, 8) } @cells];
+                    return [map { $layout->get_cell_contents($_) } @cells];
                 };
                 my $riddle =
                 Games::ABC_Path::Generator::RiddleObj->new(
 {
     my ($self,$riddle) = @_;
 
-    my $layout_string = $self->get_layout_as_string($riddle->_solution());
+    my $layout_string = $riddle->get_final_layout_as_string({});
     
     my $riddle_string = $riddle->get_riddle_v1_string;
 

File abc-path/Games-ABC_Path-Generator/lib/Games/ABC_Path/Generator/FinalLayoutObj.pm

+package Games::ABC_Path::Generator::FinalLayoutObj;
+
+use 5.006;
+
+use strict;
+use warnings;
+
+use integer;
+
+use base 'Games::ABC_Path::Solver::Base';
+
+=head1 NAME
+
+Games::ABC_Path::Generator::FinalLayoutObj - represents a final layout.
+
+=head1 VERSION
+
+Version 0.0.1
+
+=cut
+
+our $VERSION = '0.0.1';
+
+# TODO : Merge with Games::ABC_Path::Generator
+my $Y = 0;
+my $X = 1;
+my $NUM_CLUES = (2+5+5);
+my @letters = ('A' .. 'Y');
+my $LEN = 5;
+
+=head1 SYNOPSIS
+
+    use Games::ABC_Path::Generator;
+
+    my $gen = Games::ABC_Path::Generator->new({seed => 1});
+
+    # Returns a Games::ABC_Path::Generator::RiddleObj object.
+    my $riddle = $gen->calc_riddle();
+
+    print $riddle->get_riddle_v1_string();
+    print $riddle->get_riddle_string_with_header();
+
+
+=head1 SUBROUTINES/METHODS
+
+=head2 my $riddle = Games::ABC_Path::Generator::RiddleObj->new({%args}); 
+
+Initialised a new riddle. Arguments are:
+
+=over 4
+
+=item * solution
+
+The solution layout.
+
+=item * clues
+
+An array of the clues.
+
+=item * A_pos
+
+The position of the A cell.
+
+=back
+
+=cut
+
+sub _s
+{
+    my $self = shift;
+
+    if (@_)
+    {
+        $self->{_s} = shift;
+    }
+
+    return $self->{_s};
+}
+
+sub _init
+{
+    my $self = shift;
+    my $args = shift;
+
+    $self->_s($args->{layout_string});
+
+    return;
+}
+
+=head2 $layout->get_A_pos()
+
+Returns the position of the letter 'A'.
+
+=cut
+
+sub get_A_pos
+{
+    my ($self) = @_;
+
+    return index($self->_s, chr(1));
+}
+
+=head2 $layout->get_cell_contents($index)
+
+Returns the cell at index L<$index> (where index is C< $Y*5 + $X>).
+
+=cut
+
+sub get_cell_contents
+{
+    my ($self, $index) = @_;
+
+    return vec($self->_s, $index, 8) ;
+}
+
+=head2 $layout->as_string($args);
+
+Represents the layout as string.
+
+=cut
+
+sub _xy_to_int
+{
+    my ($self, $xy) = @_;
+
+    return $xy->[$Y] * $LEN + $xy->[$X];
+}
+
+sub as_string
+{
+    my ($l, $args) = @_;
+
+    my $render_row = sub {
+        my $y = shift;
+
+        return join(" | ", 
+            map {
+                my $x = $_; 
+                my $v = $l->get_cell_contents($l->_xy_to_int([$y,$x]));
+            $v ? $letters[$v-1] : '*' } (0 .. $LEN - 1));
+    };
+
+    return join('', map { $render_row->($_) . "\n" } (0 .. $LEN-1));
+}
+=head1 AUTHOR
+
+Shlomi Fish, C<< <shlomif at cpan.org> >>
+
+=head1 BUGS
+
+Please report any bugs or feature requests to C<bug-games-abc_path-generator at rt.cpan.org>, or through
+the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-ABC_Path-Generator>.  I will be notified, and then you'll
+automatically be notified of progress on your bug as I make changes.
+
+
+
+
+=head1 SUPPORT
+
+You can find documentation for this module with the perldoc command.
+
+    perldoc Games::ABC_Path::Generator
+
+
+You can also look for information at:
+
+=over 4
+
+=item * RT: CPAN's request tracker (report bugs here)
+
+L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Games-ABC_Path-Generator>
+
+=item * AnnoCPAN: Annotated CPAN documentation
+
+L<http://annocpan.org/dist/Games-ABC_Path-Generator>
+
+=item * CPAN Ratings
+
+L<http://cpanratings.perl.org/d/Games-ABC_Path-Generator>
+
+=item * Search CPAN
+
+L<http://search.cpan.org/dist/Games-ABC_Path-Generator/>
+
+=back
+
+
+=head1 ACKNOWLEDGEMENTS
+
+
+=head1 LICENSE AND COPYRIGHT
+
+Copyright 2011 Shlomi Fish.
+
+This program is distributed under the MIT (X11) License:
+L<http://www.opensource.org/licenses/mit-license.php>
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+
+
+=cut
+
+1; # End of Games::ABC_Path::Generator

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

     return $s;
 }
 
+=head2 my $string = $riddle->get_final_layout_as_string({%args})
+
+Returns the final layout as a string. %args is included for future extension.
+
+=cut
+
+sub get_final_layout_as_string
+{
+    my ($self, $args) = @_;
+
+    return $self->_solution->as_string($args);
+}
+
 =head1 AUTHOR
 
 Shlomi Fish, C<< <shlomif at cpan.org> >>

File abc-path/Games-ABC_Path-Generator/scripts/abc-path-gen

 
 if ($mode eq 'final')
 {
-    print $gen->get_layout_as_string($gen->calc_final_layout());
+    print $gen->calc_final_layout()->as_string({});
 }
 elsif ($mode eq 'riddle')
 {

File abc-path/Games-ABC_Path-Generator/t/00-load.t

-#!perl -T
+#!perl
 
 use Test::More tests => 3;