Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Game of Developer Life... Deconstructed

Game of Developer Life... Deconstructed

Mariusz Gil

April 03, 2017
Tweet

More Decks by Mariusz Gil

Other Decks in Programming

Transcript

  1. Deconstructed
    Game of Life
    @MariuszGil

    View Slide

  2. View Slide

  3. View Slide

  4. Back to 1970

    View Slide

  5. View Slide

  6. View Slide

  7. View Slide

  8. View Slide

  9. Why game oF...

    View Slide

  10. What How
    Why When

    View Slide

  11. object oriented

    View Slide

  12. View Slide

  13. View Slide

  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();
    }
    }

    View Slide

  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']);
    }
    }

    View Slide

  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;
    }

    View Slide

  17. TDD FTW!

    View Slide

  18. Objects identification

    View Slide

  19. Objects ResponsibilitieS

    View Slide

  20. Objects desigN

    View Slide

  21. cell
    Universe Game
    Rule
    Populator
    Renderer

    View Slide

  22. Patterns and principles
    object oriented

    View Slide

  23. Domain driven design
    object oriented
    Patterns and principles

    View Slide

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

    View Slide

  25. View Slide

  26. apply(state, rules) =

    View Slide

  27. View Slide

  28. View Slide

  29. wrong AssumptionS

    View Slide

  30. Future changeS

    View Slide

  31. 2 3

    View Slide

  32. View Slide

  33. 4 6

    View Slide

  34. View Slide

  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;
    }

    View Slide

  36. View Slide

  37. So what?

    View Slide

  38. stability / instability

    View Slide

  39. View Slide

  40. Cancer cell
    cell
    Universe Game
    Rule Populator
    cli Renderer
    2D Coordinates
    stronger cell
    Always alive cell
    3D Coordinates Svg Renderer

    View Slide

  41. 19 95

    View Slide

  42. to remember

    View Slide

  43. questions?
    @MariuszGil

    View Slide

  44. View Slide