Pro Yearly is on sale from $80 to $50! »

Game of Developer Life... Deconstructed

Game of Developer Life... Deconstructed

34be88398f623c109b61d23e8215bd23?s=128

Mariusz Gil

April 03, 2017
Tweet

Transcript

  1. Deconstructed Game of Life @MariuszGil

  2. None
  3. None
  4. Back to 1970

  5. None
  6. None
  7. None
  8. None
  9. Why game oF...

  10. What How Why When

  11. object oriented

  12. None
  13. None
  14. private function calculateNewCellState($cell, $cellPosition) { $aliveCount = 0; $currentNeighbour =

    0; $isFirstColumn = $cellPosition % $this->columns === 0; $isLastColumn = $cellPosition % $this->columns === $this->columns - 1; for ($i = 0; $i < 8; $i++) { $currentNeighbour = $cellPosition + $this->neighbourCoordinates[$i]; if ($currentNeighbour > 0 && ($currentNeighbour < $this->boardLength) && $this->currentGeneration[$currentNeighbour]->getState()) { if (($isFirstColumn && !in_array($i, [0, 3, 5])) || ($isLastColumn && !in_array($i, [2, 4, 7])) || !($isFirstColumn || $isLastColumn)) { $aliveCount++; } } } if ($cell->getState() && ($aliveCount < 2 || $aliveCount > 3)) { return false; } else if (!$cell->getState() && $aliveCount === 3) { return true; } else { return $cell->getState(); } }
  15. protected function setCellDead(int $posN, int $posM) { $this->grid ->getCell($posN, $posM)

    ->setDead(); } private function onePlay() { $cellsToUpdate = []; for ($n = 0; $n < $this->grid->getSizeN(); $n++) { for ($m = 0; $m < $this->grid->getSizeM(); $m++) { $neighboursCount = $this->grid->getCellNeighboursCount($n, $m); $cellsToUpdate[] = ['n' => $n, 'm' => $m, 'neighbours' => $neighboursCount]; } } foreach ($cellsToUpdate as $cellStruct) { $this->rules->applyForCell($this->grid->getCell($cellStruct['n'], $cellStruct['m']), $cellStruct['neighbours']); } }
  16. public function compute() { $computedGrid = []; foreach ($this->matrix as

    $row => $cellArr) { foreach ($cellArr as $col => $cell) { $ns = $this->checkLifeNeighbours($row, $col); $newCell = ($cell === null) ? $this->checkIfAwake($ns) : $this->setCellStatus($cell, $ns); $computedGrid[$row][$col] = $newCell; } } $this->matrix = $computedGrid; } private function setCellStatus(Cell $cell, int $lifeNeighbours) { $newCell = clone($cell); if ($cell->getIsAlive() && ($lifeNeighbours < 2 || $lifeNeighbours > 3)) { $newCell->setIsAlive(false); } elseif (!$cell->getIsAlive() && 3 == $lifeNeighbours) { $newCell->setIsAlive(true); } return $newCell; }
  17. TDD FTW!

  18. Objects identification

  19. Objects ResponsibilitieS

  20. Objects desigN

  21. cell Universe Game Rule Populator Renderer

  22. Patterns and principles object oriented

  23. Domain driven design object oriented Patterns and principles

  24. Functional programming object oriented Patterns and principles Domain driven design

  25. None
  26. apply(state, rules) =

  27. None
  28. None
  29. wrong AssumptionS

  30. Future changeS

  31. 2 3

  32. None
  33. 4 6

  34. None
  35. private function checkIfAwake($lifeNeighbours) { return (3 == $lifeNeighbours) ? new

    Cell(true) : null; } private function checkLifeNeighbours(int $row, int $col) { $life = 0; for ($rowMatrix = $row-1; $rowMatrix <= $row+1; $rowMatrix++) { for ($colMatrix = $col-1; $colMatrix <= $col+1; $colMatrix++) { if ($colMatrix == $col && $rowMatrix == $row) continue; if (isset($this->matrix[$rowMatrix][$colMatrix]) && $this->matrix[$rowMatrix][$colMatrix]->getIsAlive()) { $life++; } } } return $life; }
  36. None
  37. So what?

  38. stability / instability

  39. None
  40. Cancer cell cell Universe Game Rule Populator cli Renderer 2D

    Coordinates stronger cell Always alive cell 3D Coordinates Svg Renderer
  41. 19 95

  42. to remember

  43. questions? @MariuszGil

  44. None