Commits

Anonymous committed 653cb35

Add the LastRemainingCellForLetter move.

Comments (0)

Files changed (4)

abc-path/Games-ABC_Path-Solver/MANIFEST

 inc/Test/Run/Builder.pm
 lib/Games/ABC_Path/Solver/Base.pm
 lib/Games/ABC_Path/Solver/Board.pm
+lib/Games/ABC_Path/Solver/Move/LastRemainingCellForLetter.pm
 lib/Games/ABC_Path/Solver/Move.pm
 lib/Games/ABC_Path/Solver.pm
 Makefile.PL

abc-path/Games-ABC_Path-Solver/lib/Games/ABC_Path/Solver/Board.pm

 use base 'Games::ABC_Path::Solver::Base';
 
 use Games::ABC_Path::Solver::Move;
+use Games::ABC_Path::Solver::Move::LastRemainingCellForLetter;
 
 my $ABCP_VERDICT_NO = 0;
 my $ABCP_VERDICT_MAYBE = 1;
             {
                 $self->_set_conclusive_verdict_for_letter($letter, $xy);
                 $self->_add_move(
-                    Games::ABC_Path::Solver::Move->new(
+                    Games::ABC_Path::Solver::Move::LastRemainingCellForLetter->new(
                         {
-                            text => sprintf(
-                                "For %s only (%d,%d) is possible.", 
-                                $letters[$letter], $xy->[0]+1, $xy->[1]+1,
-                            ),
+                            vars =>
+                            {
+                                letter => $letter,
+                                coords => $xy,
+                            },
                         }
                     )
                 );

abc-path/Games-ABC_Path-Solver/lib/Games/ABC_Path/Solver/Move.pm

 
 our $VERSION = '0.0.1';
 
-
 =head1 SYNOPSIS
 
-Quick summary of what the module does.
-
-Perhaps a little code snippet.
-
     use Games::ABC_Path::Solver::Move;
 
     my $foo = Games::ABC_Path::Solver::Move->new();
 
 sub get_text
 {
+    return shift->_get_text();
+}
+
+sub _get_text
+{
     my ($self) = @_;
     
     return $self->{_text};
 
     $self->{_text} = $args->{text};
     $self->_depth($args->{depth} || 0);
+    $self->{_vars} = ($args->{vars} || {});
 
     return;
 }
         { 
             text => $self->get_text(), 
             depth => ($self->get_depth+1),
+            vars => { %{$self->{_vars}}, },
         }
     );
 }
 
+=head2 $self->get_var($name)
+
+This method returns the raw, unformatted value of the move's variable (or its 
+parameter) called $name. Each move class contains several parameters that can
+be accessed programatically.
+
+=cut
+
+sub get_var
+{
+    my ($self, $name) = @_;
+
+    return $self->{_vars}->{$name};
+}
+
+# TODO : duplicate code with ::Board
+my @letters = (qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y));
+
+sub _expand_format
+{
+    my ($self, $name, $type) = @_;
+
+    my $value = $self->get_var($name);
+
+    if ($type eq "letter")
+    {
+        return $letters[$value];
+    }
+    elsif ($type eq "coords")
+    {
+        return sprintf("(%d,%d)", $value->[0]+1, $value->[1]+1);
+    }
+    else
+    {
+        die "Unknown format type '$type'!";
+    }
+}
+
 =head1 AUTHOR
 
 Shlomi Fish, C<< <shlomif at cpan.org> >>

abc-path/Games-ABC_Path-Solver/lib/Games/ABC_Path/Solver/Move/LastRemainingCellForLetter.pm

+package Games::ABC_Path::Solver::Move::LastRemainingCellForLetter;
+
+use strict;
+use warnings;
+
+use base 'Games::ABC_Path::Solver::Move';
+
+=head1 NAME
+
+Games::ABC_Path::Solver::Move::LastRemainingCellForLetter - an ABC Path move
+that indicates it's the last remaining cell for a given letter.
+
+=head1 VERSION
+
+Version 0.0.1
+
+=cut
+
+our $VERSION = '0.0.1';
+
+=head1 SYNOPSIS
+
+    use Games::ABC_Path::Solver::Move::LastRemainingCellForLetter;
+
+    my $move = Games::ABC_Path::Solver::Move::LastRemainingCellForLetter->new(
+        {
+            vars =>
+            {
+                coords => [1,2],
+                letter => 5,
+            },
+        }
+    );
+
+=head1 DESCRIPTION
+
+This is a move that indicates that the C<'letter'> has the last remaining cell
+as C<'coords'>.
+
+=cut
+
+sub _get_text {
+    my $self = shift;
+
+    my $text = $self->_format;
+
+    $text =~ s/%\((\w+)\)\{(\w+)\}/
+        $self->_expand_format($1,$2)
+        /ge;
+
+    return $text;
+}
+
+sub _format {
+    return "For %(letter){letter} only %(coords){coords} is possible.";
+}
+
+1;
+