package edu.wright.hendrix11.conway.logic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Joe Hendrix
 */
public class Grid {
    private List<Cell> aliveCells = new ArrayList<>();

    public void addLivingCell(Cell cell) {
        assert cell.isAlive();
        aliveCells.add(cell);
    }

    public void removeDeadCell(Cell cell) {
        assert !cell.isAlive();
        aliveCells.remove(cell);
    }

    public void tick() {
        List<Cell> toggleTheseCells = new ArrayList<>();

        for(Cell cell : aliveCells) {
            int numberOfLivingNeighbors = cell.getNumberLivingNeighbors();

            if(numberOfLivingNeighbors < 2 || numberOfLivingNeighbors > 3) {
                toggleTheseCells.add(cell);
            }
        }

        for(Cell cell : toggleTheseCells) {
            cell.toggle();

            if(cell.isAlive()) {
                aliveCells.add(cell);
            } else {
                assert !cell.isAlive();
                aliveCells.remove(cell);
            }
        }

    }

}

@@ -566,37 +566,44 @@
     
-for(Cell cell : aliv
+checkLivingCells(toggleThes
 eCells)
- %7B
+;%0A
 %0A   
@@ -611,221 +611,109 @@
     
-    int numberOfLivingNeighbors = cell.getNumberLivingNeighbors(
+toggleCells(toggleTheseCells
 );%0A
-%0A
     
+%7D%0A
     
+%0A
     
-if(numberOfLivingNeighbors %3C 2 %7C%7C numberOfLivingNeighbors %3E 3) %7B%0A                toggleTheseCells.add(cell);%0A            %7D%0A        %7D%0A
+private void toggleCells(List%3CCell%3E toggleTheseCells) %7B
 %0A   
@@ -974,22 +974,356 @@
 %7D%0A        %7D%0A
+    %7D%0A    %0A    private void checkLivingCells(List%3CCell%3E toggleTheseCells) %7B%0A        for(Cell cell : aliveCells) %7B%0A            int numberOfLivingNeighbors = cell.getNumberLivingNeighbors();%0A%0A            if(numberOfLivingNeighbors %3C 2 %7C%7C numberOfLivingNeighbors %3E 3) %7B%0A                toggleTheseCells.add(cell);%0A            %7D%0A        %7D
 %0A    %7D%0A%0A%7D%0A

Update Grid.java