1. Shlomi Fish
  2. abc-path

Commits

shl...@b384bcd7-cfd4-0310-aca0-d78b80f7b91b  committed 1581c07

Implemented the recursion.

The interface is a bit clunky at the moment, but I'm happy from my
progress.

  • Participants
  • Parent commits ea2a43b
  • Branches default

Comments (0)

Files changed (2)

File abc-path/perl-quick-and-dirty/abc-path-solver.pl

View file
  • Ignore whitespace
     return;
 }
 
+sub bump
+{
+    my ($self) = @_;
+
+    return ref($self)->new(
+        { 
+            text => $self->get_text(), 
+            depth => ($self->get_depth+1),
+        }
+    );
+}
+
 package Games::ABC_Path::Solver::Board;
 
 use strict;
     return;
 }
 
+sub get_successful_layouts {
+    my ($self) = @_;
+
+    return [@{$self->_successful_layouts}];
+}
+
+sub _successful_layouts {
+    my $self = shift;
+
+    if (@_) {
+        $self->{_successful_layouts} = shift;
+    }
+
+    return $self->{_successful_layouts};
+}
+
 
 sub _layout {
     my $self = shift;
     }
 
     $self->_layout(\$layout_string);
+    $self->_successful_layouts([]);
     $self->_moves([]);
     $self->_iter_changed(0);
 
 
                 if ($existing_verdict == $ABCP_VERDICT_YES)
                 {
-                    die "Mismatched verdict: Should be set to no, but already yes.";
+                    $self->_error(['mismatched_verdict', $x, $y]);
+                    return;
                 }
 
                 if ($existing_verdict == $ABCP_VERDICT_MAYBE)
     return $num_changed;
 }
 
+sub _clone
+{
+    my ($self) = @_;
+
+    return
+        ref($self)->new(
+            {
+                layout => ${$self->_layout()},
+            }
+        );
+}
+
 sub solve
 {
     my ($self) = @_;
 
     if (@min_coords)
     {
+        my ($x, $y) = @min_coords;
         # We have at least one multiple rank cell. Let's recurse there:
+        foreach my $letter (@min_options)
+        {
+            my $recurse_solver = $self->_clone;
+
+            $self->_add_move(
+                Games::ABC_Path::Solver::Move->new(
+                {
+                    text => "We have non-conclusive cells. Trying $letters[$letter] for ($x,$y)",
+                }
+            ),
+            );
+
+            $recurse_solver->set_conclusive_verdict_for_letter(
+                $letter, [$x,$y]
+            );
+
+            $recurse_solver->solve;
+
+            foreach my $move (@{ $recurse_solver->get_moves })
+            {
+                $self->_add_move($move->bump());
+            }
+
+            if ($recurse_solver->_error())
+            {
+                $self->_add_move(
+                    Games::ABC_Path::Solver::Move->new(
+                    {
+                        text => "Trying $letters[$letter] for ($x,$y) results in an error.",
+                    }
+                    )
+                );
+            }
+            else
+            {
+                $self->_add_move(
+                    Games::ABC_Path::Solver::Move->new(
+                        {
+                            text => "Trying $letters[$letter] for ($x,$y) returns a success."
+                        }
+                    )
+                );
+                push @{$self->_successful_layouts}, 
+                    @{$recurse_solver->get_successful_layouts()};
+            }
+        }
+
+        my $count = @{$self->_successful_layouts()};
+        if (! $count)
+        {
+            return ['all_options_bad'];
+        }
+        elsif ($count == 1)
+        {
+            return ['success'];
+        }
+        else
+        {
+            return ['success_multiple'];
+        }
     }
-
-    return;
+    else
+    {
+        $self->_successful_layouts([$self->_clone()]);
+        return ['success'];
+    }
 }
 
 my $letter_re_s = join('', map { quotemeta($_) } @letters);
     return;
 }
 
-sub get_results_text_table
+sub _get_results_text_table
 {
     my ($self) = @_;
 
     return $tb;
 }
 
+sub get_successes_text_tables
+{
+    my ($self) = @_;
+
+    return [map { $_->_get_results_text_table() } @{$self->get_successful_layouts()}];
+}
+
 # Input the board.
 
 sub input_from_file
 
 foreach my $move (@{$solver->get_moves})
 {
-    print $move->get_text(), "\n";
+    print +(' => ' x $move->get_depth()), $move->get_text(), "\n";
 }
 
-print $solver->get_results_text_table;
+print @{$solver->get_successes_text_tables};
 
 =head1 COPYRIGHT AND LICENSE
 

File abc-path/perl-quick-and-dirty/t/results/brain-bashers.2010-12-22.abc-path-sol

View file
  • Ignore whitespace
 The only letter that can be at (3,3) is N. Invalidating it for all other cells.
 For D only (1,0) is possible.
 For P only (2,2) is possible.
+We have non-conclusive cells. Trying I for (1,3)
+ => For K only (0,4) is possible.
+ => J cannot be at (2,4) due to lack of vicinity from K.
+ => L cannot be at (2,4) due to lack of vicinity from K.
+ => For L only (1,4) is possible.
+Trying I for (1,3) results in an error.
+We have non-conclusive cells. Trying K for (1,3)
+ => For I only (1,4) is possible.
+ => For L only (2,4) is possible.
+ => The only letter that can be at (0,4) is J. Invalidating it for all other cells.
+Trying K for (1,3) returns a success.
  | X = 0 | X = 1 | X = 2 | X = 3 | X = 4 | 
  | E     | D     | R     | S     | T     | 
  | F     | C     | B     | Q     | U     | 
  | G     | A     | P     | O     | V     | 
- | H     | I,K   | M     | N     | W     | 
- | J,K   | I,J,L | J,L   | Y     | X     | 
+ | H     | K     | M     | N     | W     | 
+ | J     | I     | L     | Y     | X     |