コード例 #1
0
ファイル: World.php プロジェクト: jakubkratina/gameOfLife
 /**
  * Iterate through each Organism in the World, created as a 2D array.
  * Check surroundings of each Organism and do some operations.
  *
  * @return $this
  */
 public function live()
 {
     if ($this->table) {
         $this->table();
     }
     for ($i = 0; $i < $this->iterations; $i++) {
         $this->operations = [];
         for ($x = 0; $x < $this->cells; $x++) {
             for ($y = 0; $y < $this->cells; $y++) {
                 // Calculate operation
                 $operation = !isset($this->life[$x][$y]) ? Organism::canBeBirthable($x, $y, $this->life) : $this->life[$x][$y]->whatShouldIDo($this->life);
                 if ($operation) {
                     $this->operations[] = array_merge($operation, ['x' => $x, 'y' => $y]);
                 }
             }
         }
         // After iteration is done, process all operations.
         $this->process();
         // Add current iteration life to output for simulation
         $this->addOrganismsToSimulation($i);
         if ($this->table) {
             $this->table($i);
         }
     }
     return $this;
 }