1. Shlomi Fish
  2. nurikabe

Commits

shl...@b384bcd7-cfd4-0310-aca0-d78b80f7b91b  committed da09bb8

Convert _cells_loop and _calc_vicinity to ::Coords.

  • Participants
  • Parent commits 7f175f6
  • Branches default

Comments (0)

Files changed (2)

File nurikabe-solver/Games-Nurikabe-Solver/lib/Games/Nurikabe/Solver/Board.pm

View file
         my $x = 0;
         foreach my $cell (@$row)
         {
-            $callback->([$y,$x], $cell);
+            $callback->(
+                Games::Nurikabe::Solver::Coords->new({y => $y, x => $x,}),
+                $cell,
+            );
         }
         continue
         {
 
 sub _calc_vicinity
 {
-    my $self = shift;
-    my ($y, $x) = @{shift()};
+    my ($self, $coords) = @_;
+
+    my $y = $coords->y;
+    my $x = $coords->x;
 
     my @ret;
 
+    my $add_coord = sub {
+        my ($new_y, $new_x) = @_;
+
+        push @ret, 
+            Games::Nurikabe::Solver::Coords->new({y => $new_y, x => $new_x});
+    };
+
     if ($y > 0)
     {
-        push @ret, [$y-1,$x];
+        $add_coord->($y-1,$x);
     }
 
     if ($x > 0)
     {
-        push @ret, [$y,$x-1];
+        $add_coord->($y,$x-1);
     }
 
     if ($x+1 < $self->_width())
     {
-        push @ret, [$y, $x+1];
+        $add_coord->($y, $x+1);
     }
 
     if ($y+1 < $self->_height())
     {
-        push @ret, [$y+1, $x];
+        $add_coord->($y+1,$x);
     }
 
     return \@ret;
                 return;
             }
 
-            if (all { $self->get_cell(Games::Nurikabe::Solver::Coords->new({y => $_->[0], x => $_->[1] } ))->status() eq $NK_BLACK }
+            if (all { $self->get_cell($_)->status() eq $NK_BLACK }
                 (@{$self->_calc_vicinity($coords)})
             )
             {
                 # We got an unknown cell that's entirely surrounded by blacks -
                 # let's do our thing.
-                $self->_mark_as_black(
-                    Games::Nurikabe::Solver::Coords->new({y => $coords->[0], x => $coords->[1] } )
-                );
+                $self->_mark_as_black($coords);
                 $self->_add_move(
                     {
                         reason => "surrounded_by_blacks",
 
             foreach my $shape (@$shapes_list)
             {
-                $self->_adj_whites_handle_shape(
-                    Games::Nurikabe::Solver::Coords->new(
-                        {
-                        y => $c->[0], x => $c->[1],
-                        }
-                    ), $cell, $shape);
+                $self->_adj_whites_handle_shape($c, $cell, $shape);
             }
         }
     );
 
             if ($cell->status() eq $NK_UNKNOWN && ! $cell->_reachable())
             {
-                $self->_mark_as_black(
-                    Games::Nurikabe::Solver::Coords->new(
-                        { y => $coords->[0], x => $coords->[1] }
-                    )
-                );
+                $self->_mark_as_black($coords);
             }
         },
     );
                 {
                     if (defined($cell->_island_reachable->[$idx]))
                     {
-                        push @{$island_reachable_coords[$idx]}, 
-                            Games::Nurikabe::Solver::Coords->new(
-                                {
-                                    y => $coords->[0],
-                                    x => $coords->[1],
-                                }
-                            )
-                            ;
+                        push @{$island_reachable_coords[$idx]}, $coords;
                     }
                 }
             }
 
     $self->_cells_loop(
         sub {
-            my ($cell_pair, $cell) = @_;
-
-            my $cell_coords = Games::Nurikabe::Solver::Coords->new(
-                {
-                    y => $cell_pair->[0], x => $cell_pair->[1]
-                }
-            );
+            my ($cell_coords, $cell) = @_;
 
             if (($cell->status() eq $NK_BLACK) && (! $cell->already_processed))
             {

File nurikabe-solver/Games-Nurikabe-Solver/t/calc-vicinity.t

View file
             $string_representation
         );
 
+    my $test_calc_vicinity = sub {
+        local $Test::Builder::Level = $Test::Builder::Level + 1;
+        my ($coords, $vicinity, $blurb) = @_;
+
+        eq_or_diff(
+            [ map { $_->_to_pair() } @{$board->_calc_vicinity(
+                    Games::Nurikabe::Solver::Coords->new($coords)
+                ) }
+            ],
+            $vicinity,
+            $blurb
+        );
+    };
+
     # TEST
-    eq_or_diff(
-        $board->_calc_vicinity([1,1]),
+    $test_calc_vicinity->(
+        {y => 1, x => 1},
         [[0,1],[1,0],[1,2],[2,1]],
         "Simple calc vicinity - 1,1",
     );
 
     # TEST
-    eq_or_diff(
-        $board->_calc_vicinity([4,3]),
+    $test_calc_vicinity->(
+        {y => 4, x => 3},
         [[3,3],[4,2],[4,4]],
         "calc vicinity bottom - 4,3",
     );
 
     # TEST
-    eq_or_diff(
-        $board->_calc_vicinity([0,0]),
+    $test_calc_vicinity->(
+        { y => 0, x => 0 },
         [[0,1],[1,0]],
         "calc vicinity corner - 0,0",
     );
 
     # TEST
-    eq_or_diff(
-        $board->_calc_vicinity([0,2]),
+    $test_calc_vicinity->(
+        { y => 0, x => 2},
         [[0,1],[0,3],[1,2]],
         "calc vicinity up - 0,2",
     );