Commits

Anonymous committed afc30a5

Refactoring: extracted some queue methods.

  • Participants
  • Parent commits 75fe9b4

Comments (0)

Files changed (1)

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

     idx
     known_cells
     order
+    _queue
     ));
 
 =head1 SYNOPSIS
     return $self->_sort_coords(\@ret);
 }
 
+sub _init_queue
+{
+    my $island = shift;
+
+    $island->_queue([map { [0,$_] } @{$island->known_cells()}]);
+
+    return;
+}
+
+sub _dequeue
+{
+    my $self = shift;
+
+    return shift(@{$self->_queue()});
+}
+
+sub _enqueue
+{
+    my $self = shift;
+    my $item = shift;
+
+    push @{$self->_queue()}, $item;
+}
+
 =head2 $island->mark_reachable_brfs_scan({board => $board})
 
 Mark the reachable unknown cells using a Breadth-First-Search scan.
 
     my $board = $args->{'board'};
 
-    my @queue = (map { [0,$_] } @{$island->known_cells()});
-
     my $dist_limit = $island->order() - @{$island->known_cells()};
 
+    $island->_init_queue();
+
     QUEUE_LOOP:
-    while (@queue)
+    while (my $item = $island->_dequeue())
     {
-        my $item = shift(@queue);
-        
         my ($dist, $c) = @$item;
 
         if ($dist == $dist_limit)
                     return;
                 }
 
-                push @queue, $cell->set_island_reachable(
+                $island->_enqueue($cell->set_island_reachable(
                     $island->idx(),
                     $dist+1,
                     $to_check
-                );
+                ));
             },
         );
     }