1. Shlomi Fish
  2. nurikabe

Commits

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

Extracted the add_offset() method.

  • Participants
  • Parent commits 1093d43
  • Branches default

Comments (0)

Files changed (4)

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

View file
  • Ignore whitespace
     return $self;
 }
 
+=head2 my $offset_coords = $self->add_offset($coords, $offset)
+
+Returns the offset coords based on $coords (an [Y,X] coordinates
+in the board) and $offset, which is a [Y,X] offset.
+
+=cut
+
+sub add_offset
+{
+    my ($self, $coords, $offset) = @_;
+
+    return [$coords->[0] + $offset->[0], $coords->[1] + $offset->[1]];
+}
+
 =head1 AUTHOR
 
 Shlomi Fish, C<< <shlomif at cpan.org> >>

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

View file
  • Ignore whitespace
 
 use List::MoreUtils qw(all);
 
-use base 'Class::Accessor';
+use base 'Games::Nurikabe::Solver::Base';
 
 use Games::Nurikabe::Solver::Cell qw($NK_UNKNOWN $NK_WHITE $NK_BLACK);
 use Games::Nurikabe::Solver::Island;
     return $ret;
 }
 
-sub new
+sub _init
 {
-    my $class = shift;
+    my $self = shift;
+    my $args = shift;
 
-    my $self = $class->SUPER::new(@_);
+    $self->_width($args->{width});
+    $self->_height($args->{height});
 
     $self->_clear_verdict_marked_cells();
 
     $self->_moves([]);
 
-    return $self;
+    return 0;
 }
 
 =head2 $class->load_from_string($string)
     }
     my ($width, $height) = ($1, $2);
 
-    my $self = $class->new();
-
-    $self->_width($width);
-    $self->_height($height);
+    my $self = $class->new({width => $width, height => $height});
 
     my @cells;
     my @islands;
     my $offset = $shape->{'offset'};
     my $blacks_offsets = $shape->{'blacks'};
 
-    # Other X and Other Y
-    my $other_coords =
-    [
-        $c->[0] + $offset->[0],
-        $c->[1] + $offset->[1]
-    ];
+    # Other [X,Y] 
+    my $other_coords = $self->add_offset($c, $offset);
     
     if (! $self->_is_in_bounds(@$other_coords))
     {
         # Bingo.
         foreach my $b_off (@$blacks_offsets)
         {
-            my $b_coords =
-            [
-                $c->[0] + $b_off->[0],
-                $c->[1] + $b_off->[1]
-            ];
-            $self->_mark_as_black(@$b_coords);
+            $self->_mark_as_black(@{$self->add_offset($c, $b_off)});
         }
 
         $self->_add_move(

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

View file
  • Ignore whitespace
 use strict;
 use warnings;
 
-use base 'Class::Accessor';
+use base 'Games::Nurikabe::Solver::Base';
 
 =head1 NAME
 
     my $nurikabe = Games::Nurikabe::Island->new(
         {
             idx => $index,
-            known_cells => $index,
+            known_cells => [@coords],
+            order => 3,
         }
     );
 
 
 =cut
 
-sub new
+sub _init
 {
-    my ($class, $args) = @_;
-
-    my $self = $class->SUPER::new($args);
+    my ($self, $args) = @_;
 
     $self->idx($args->{idx});
     $self->known_cells($self->_sort_coords($args->{known_cells}));
+    $self->order($args->{order});
 
-    return $self;
+    return 0;
 }
 
 
     {
         foreach my $offset ([-1,0],[0,-1],[0,1],[1,0])
         {
-            my $to_check = [$cell->[0]+$offset->[0], $cell->[1]+$offset->[1]];
+            my $to_check = $self->add_offset($cell, $offset);
             my $s = join(",",@$to_check);
+
             if (!exists($exclude_coords{$s}))
             {
                 push @ret, $to_check;
 
 =cut
 
-1; # End of Games::Nurikabe::Solver
+1; # End of Games::Nurikabe::Solver::Island

File nurikabe-solver/Games-Nurikabe-Solver/t/island-surround.t

View file
  • Ignore whitespace
     my $board =
         Games::Nurikabe::Solver::Board->new(
             {
-                _width => $bi->width(),
-                _height => $bi->height(),
+                width => $bi->width(),
+                height => $bi->height(),
             }
         );