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

        checkLivingCells(toggleTheseCells);

        toggleCells(toggleTheseCells);
    }
    
    private void toggleCells(List<Cell> toggleTheseCells) {
        for(Cell cell : toggleTheseCells) {
            cell.toggle();

            if(cell.isAlive()) {
                aliveCells.add(cell);
            } else {
                assert !cell.isAlive();
                aliveCells.remove(cell);
            }
        }
    }
    
    private void checkLivingCells(List<Cell> toggleTheseCells) {
        for(Cell cell : aliveCells) {
            int numberOfLivingNeighbors = cell.getNumberLivingNeighbors();

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

}

@@ -1007,30 +1007,36 @@
 e void check
-Living
+AdjacentDead
 Cells(List%3CC
@@ -1072,42 +1072,1205 @@
     
-for(Cell cell : aliveCells) %7B%0A    
+Set%3CCell%3E newbornCells = new HashSet%3C%3E();%0A        %0A        for(Cell cell : aliveCells) %7B%0A            if(cell.getNorthernCell() != null && !cell.getNorthernCell().isAlive()) %7B%0A                if(checkDeadCell(cell.getNorthernCell())) %7B%0A                    newbornCells.add(cell.getNorthernCell());%0A                %7D%0A            %7D%0A            %0A            if(cell.getSouthernCell() != null && !cell.getSouthernCell().isAlive()) %7B%0A                if(checkDeadCell(cell.getSouthernCell())) %7B%0A                    newbornCells.add(cell.getSouthernCell());%0A                %7D%0A            %7D%0A            %0A            if(cell.getEasternCell() != null && !cell.getEasternCell().isAlive()) %7B%0A                if(checkDeadCell(cell.getEasternCell())) %7B%0A                    newbornCells.add(cell.getEasternCell());%0A                %7D%0A            %7D%0A            %0A            if(cell.getWesternCell() != null && !cell.getWesternCell().isAlive()) %7B%0A                if(checkDeadCell(cell.getWesternCell())) %7B%0A                    newbornCells.add(cell.getWesternCell());%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A    %0A    private boolean checkDeadCell(Cell deadCell) %7B%0A        assert !deadCell.isAlive();%0A        %0A
     
@@ -2303,17 +2303,21 @@
 hbors = 
-c
+deadC
 ell.getN
@@ -2336,24 +2336,32 @@
 eighbors();%0A
+        
 %0A           
@@ -2361,73 +2361,622 @@
     
-    if(numberOfLivingNeighbors %3C 2 %7C%7C numberOfLivingNeighbors %3E 3
+assert numberOfLivingNeighbors %3E= 0 && numberOfLivingNeighbors %3C= 8;%0A        return numberOfLivingNeighbors == 3;%0A    %7D%0A    %0A    private boolean checkLivingCell(Cell livingCell) %7B%0A        assert livingCell.isAlive();%0A        %0A        int numberOfLivingNeighbors = livingCell.getNumberLivingNeighbors();%0A        %0A        assert numberOfLivingNeighbors %3E= 0 && numberOfLivingNeighbors %3C= 8;%0A        return numberOfLivingNeighbors %3C 2 %7C%7C numberOfLivingNeighbors %3E 3;%0A    %7D%0A    %0A    private void checkLivingCells(List%3CCell%3E toggleTheseCells) %7B%0A        for(Cell cell : aliveCells) %7B%0A            if(checkLivingCell(cell)
 ) %7B%0A

Update Grid.java