# HG changeset patch # User shlomif@b384bcd7-cfd4-0310-aca0-d78b80f7b91b # Date 1291935270 0 # Node ID da09bb85e8621d005b4d515a47f271acb23d5854 # Parent 7f175f6a9f7be9dd8470b5e92a7da5efec164ac2 Convert _cells_loop and _calc_vicinity to ::Coords. diff --git a/nurikabe-solver/Games-Nurikabe-Solver/lib/Games/Nurikabe/Solver/Board.pm b/nurikabe-solver/Games-Nurikabe-Solver/lib/Games/Nurikabe/Solver/Board.pm --- a/nurikabe-solver/Games-Nurikabe-Solver/lib/Games/Nurikabe/Solver/Board.pm +++ b/nurikabe-solver/Games-Nurikabe-Solver/lib/Games/Nurikabe/Solver/Board.pm @@ -397,7 +397,10 @@ my \$x = 0; foreach my \$cell (@\$row) { - \$callback->([\$y,\$x], \$cell); + \$callback->( + Games::Nurikabe::Solver::Coords->new({y => \$y, x => \$x,}), + \$cell, + ); } continue { @@ -441,29 +444,38 @@ 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; @@ -494,15 +506,13 @@ 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", @@ -603,12 +613,7 @@ 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); } } ); @@ -647,11 +652,7 @@ 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); } }, ); @@ -704,14 +705,7 @@ { 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; } } } @@ -769,13 +763,7 @@ \$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)) { diff --git a/nurikabe-solver/Games-Nurikabe-Solver/t/calc-vicinity.t b/nurikabe-solver/Games-Nurikabe-Solver/t/calc-vicinity.t --- a/nurikabe-solver/Games-Nurikabe-Solver/t/calc-vicinity.t +++ b/nurikabe-solver/Games-Nurikabe-Solver/t/calc-vicinity.t @@ -24,30 +24,44 @@ \$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", );