PHP Flood Fill

Flood Filler is a small program in PHP demonstrating the Flood Fill algorithm in a simple way.

My implementation of the same algorithm for a game map generator in C# and ASP.NET

My implementation of the same algorithm for a game map generator in C# and ASP.NET

Here’s the code:

  1. /*
  2.  * @title:  Flood Fill Class
  3.  * See below class for example input.
  4.  */
  5. class FloodFiller {
  6.  
  7.     private $x, $y, $fill, $searchNext, $map;
  8.  
  9.     public function Scan( $map, $point ) {
  10.         // We create the list of traversable squares(fill)
  11.         // and a to-search queue(searchNext[])
  12.         // where we insert our starting point.
  13.         $this->map          = $map;
  14.         $this->fill         = array();
  15.         $this->searchNext   = array();
  16.         $this->searchNext[] = array('x' => $point[ 'x' ], 'y' => $point[ 'y' ]);
  17.  
  18.         // As long as there are items in the queue
  19.         // keep filling!
  20.         while ( !empty( $this->searchNext ) ) {
  21.  
  22.             // Get the next square item and erase it from the list  
  23.             $next = array_pop( $this->searchNext );
  24.             $this->x = $next[ 'x' ];
  25.             $this->y = $next[ 'y' ];
  26.  
  27.             // Check square. If it's traversable we add
  28.             // the square to our fill list and we turn the
  29.             // square untraversable to prevent future checking.
  30.             if ( $this->map[ $this->x ][ $this->y ] == 1 ) {
  31.                 $this->map[ $this->x ][ $this->y ] = 0;
  32.                 $this->fill[] = array( 'x' => $this->x, 'y' => $this->y );
  33.                 $this->CheckDirections();
  34.  
  35.             }
  36.         }
  37.         return $this->fill;
  38.     }
  39.     private function CheckSquare( $checkX, $checkY ) {
  40.         // if we can fill this square we add it to our queue
  41.         if ( $this->map[ $checkX ][ $checkY ] == 1 ) {
  42.             $this->searchNext[] = array( 'x' => $checkX, 'y' => $checkY );    
  43.         }
  44.     }
  45.     private function CheckDirections() {
  46.         // Perform a check of all adjacent squares
  47.         $this->CheckSquare( $this->x, $this->y - 1 );
  48.         $this->CheckSquare( $this->x, $this->y + 1 );
  49.         $this->CheckSquare( $this->x - 1, $this->y );
  50.         $this->CheckSquare( $this->x + 1, $this->y );
  51.     }
  52. }
  53.  
  54. // Example Input:
  55. // 6×6 map
  56. // 1: valley
  57. // 0: mountain/sea
  58. // Starting Point: 3.3
  59. $map = array(
  60.                //0  1  2  3  4  5
  61.     array( /*0*/ 1, 0, 1, 0, 1, 0 ),
  62.     array( /*1*/ 0, 1, 1, 0, 1, 0 ),
  63.     array( /*2*/ 1, 1, 1, 1, 0, 0 ),
  64.     array( /*3*/ 1, 1, 0, 1, 1, 1 ),
  65.     array( /*4*/ 0, 0, 1, 1, 1, 0 ),
  66.     array( /*5*/ 1, 0, 1, 0, 0, 0 )
  67.     );
  68.  
  69. // Starting Point: 3.3
  70. $point = array ( 'x' => 3, 'y' => 3 );
  71.  
  72. // new class instance
  73. $floodFiller = New FloodFiller();
  74.  
  75. // Print the final list of filled coordinates
  76. print_r( $floodFiller->Scan( $map, $point ) );

Leave a Reply