package edu.wright.hendrix11.conway.logic;

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

/**
 * @author Joe Hendrix
 */
public class Grid {
    private Map<Coordinate, Cell> aliveCells = new HashMap<>();

    public Grid() {
    }

    public Grid(Map<Coordinate, Cell> aliveCells) {
        this.aliveCells = aliveCells;
    }

    public Grid(List<Coordinate> liveCellCoordinates) {
        for(Coordinate coordinate : liveCellCoordinates) {
            aliveCells.put(coordinate, new Cell());
        }
    }

    public void tick() {
        Map<Coordinate, Cell> deadAdjacentCells = new HashMap<>();

        for(Map.Entry<Coordinate, Cell> entry : aliveCells.entrySet()) {
            for(Coordinate neighbor : entry.getKey().getNeighbors()) {
                if(aliveCells.get(neighbor) != null) {
                    entry.getValue().incrementAliveNeighbors();
                } else {
                    if(deadAdjacentCells.get(neighbor) != null) {
                        deadAdjacentCells.get(neighbor).incrementAliveNeighbors();
                    } else {
                        Cell deadCell = new Cell(false);
                        deadCell.incrementAliveNeighbors();
                        deadAdjacentCells.put(neighbor, deadCell);
                    }
                }
            }
        }

        for(Map.Entry<Coordinate, Cell> entry : aliveCells.entrySet()) {
            Cell aliveCell = entry.getValue();

            aliveCell.tick();

            if(!aliveCell.isAlive()) {
                // remove alive cell
            }
        }

        for(Map.Entry<Coordinate, Cell> entry : deadAdjacentCells.entrySet()) {
            Cell deadCell = entry.getValue();

            deadCell.tick();

            if(deadCell.isAlive()) {
                // add newly alive cell
            }
        }
    }
}

@@ -548,12 +548,29 @@
 lic 
-void
+Map%3CCoordinate, Cell%3E
  tic
@@ -637,24 +637,86 @@
 HashMap%3C%3E();
+%0A        Map%3CCoordinate, Cell%3E changedCells = new HashMap%3C%3E();
 %0A%0A        fo
@@ -1531,32 +1531,79 @@
 ntry.getValue();
+%0A            Coordinate coord = entry.getKey();
 %0A%0A            al
@@ -1674,18 +1674,75 @@
         
-//
+changedCells.put(coord,aliveCell);%0A                // TODO:
  remove 
@@ -1902,16 +1902,63 @@
 Value();
+%0A            Coordinate coord = entry.getKey();
 %0A%0A      
@@ -2038,31 +2038,41 @@
     
-// add newly alive c
+changedCells.put(coord,deadC
 ell
+);
 %0A   
@@ -2088,20 +2088,58 @@
 %7D%0A        %7D%0A
+        %0A        return changedCells;%0A
     %7D%0A%7D%0A

Update Grid.java