package edu.wright.hendrix11.conway.logic;

/**
 * @author Joe Hendrix
 */
public class Cell {
    private boolean alive = false;
    private Cell northernCell;
    private Cell southernCell;
    private Cell easternCell;
    private Cell westernCell;

    public Cell() {
    }

    public Cell(boolean alive) {
        this.alive = alive;
    }

    public void setEasternCell(Cell easternCell) {
        easternCell.westernCell = this;
        this.easternCell = easternCell;
    }

    public void setNorthernCell(Cell northernCell) {
        northernCell.southernCell = this;
        this.northernCell = northernCell;
    }

    public void setSouthernCell(Cell southernCell) {
        southernCell.northernCell = this;
        this.southernCell = southernCell;
    }

    public void setWesternCell(Cell westernCell) {
        westernCell.easternCell = this;
        this.westernCell = westernCell;
    }

    public Cell getEasternCell() {
        return easternCell;
    }

    public Cell getNorthernCell() {
        return northernCell;
    }

    public Cell getSouthernCell() {
        return southernCell;
    }

    public Cell getWesternCell() {
        return westernCell;
    }
    
    public Cell getNorthEasternCell() {
        Cell northEasternCell = null;
        
        if(northernCell != null) {
            northEasternCell = northernCell.easternCell;
        } else if(easternCell != null) {
            northEasternCell = easternCell.northernCell;
        }
        
        return northEasternCell;
    }
    
    public Cell getNorthWesternCell() {
        Cell northWesternCell = null;
        
        if(northernCell != null) {
            northWesternCell = northernCell.westernCell;
        } else if(westernCell != null) {
            northWesternCell = westernCell.northernCell;
        }
        
        return northWesternCell;
    }
    
    public Cell getSouthEasternCell() {
        Cell southEasternCell = null;
        
        if(southernCell != null) {
            southEasternCell = southernCell.easternCell;
        } else if(easternCell != null) {
            southEasternCell = easternCell.southernCell;
        }
        
        return southEasternCell;
    }
    
    public Cell getSouthWesternCell() {
        Cell southWesternCell = null;
        
        if(southernCell != null) {
            southWesternCell = southernCell.westernCell;
        } else if(westernCell != null) {
            southWesternCell = westernCell.southernCell;
        }
        
        return southWesternCell;
    }

    public void toggle() {
        alive = !alive;
    }

    public boolean isAlive() {
        return alive;
    }

    public int getNumberLivingNeighbors() {
        int count = 0;
        
        count += countCardinalLivingNeighbors();
        count += countDiagonalLivingNeighbors();
        
        return count;
    }
    
    private int countDiagonalLivingNeighbors() {
        int count = 0;
        
        if(getNorthEasternCell() != null && getNorthEasternCell().isAlive()) {
            count++;
        }

        if(getNorthWesternCell() != null && getNorthWesternCell().isAlive()) {
            count++;
        }

        if(getSouthEasternCell() != null && getSouthEasternCell().isAlive()) {
            count++;
        }

        if(getSouthWesternCell() != null && getSouthWesternCell().isAlive()) {
            count++;
        }
        
        return count;
    }
    
    private int countCardinalLivingNeighbors() {
        int count = 0;

        if(northernCell != null && northernCell.isAlive()) {
            count++;
        }

        if(southernCell != null && southernCell.isAlive()) {
            count++;
        }

        if(easternCell != null && easternCell.isAlive()) {
            count++;
        }

        if(westernCell != null && westernCell.isAlive()) {
            count++;
        }

        return count;
    }

    @Override
    public int hashCode() {
        return alive ? 1 : 0;
    }

    @Override
    public boolean equals(Object o) {
        return this == o;
    }
}

@@ -331,32 +331,562 @@
 e = alive;%0A    %7D
+%0A    %0A    public List%3CCell%3E getNeighbors() %7B%0A        List%3CCell%3E neighbors = new ArrayList%3C%3E();%0A        %0A        neighbors.add(northernCell);%0A        neighbors.add(southernCell);%0A        neighbors.add(easternCell);%0A        neighbors.add(westernCell);%0A        %0A        neighbors.add(getNorthEasternCell());%0A        neighbors.add(getNorthWesternCell());%0A        neighbors.add(getSouthEasternCell());%0A        neighbors.add(getSouthWesternCell());%0A        %0A        assert neighbors.size() == 8;%0A        %0A        return neighbors;%0A    %7D
 %0A%0A    public voi
@@ -3285,175 +3285,31 @@
     
-count += countCardinalLivingNeighbors();%0A        count += countDiagonalLivingNeighbors();%0A        %0A        return count;%0A    %7D%0A    %0A    private int countDiagonalLiving
+for(Cell neighbor : get
 Neig
@@ -3307,32 +3307,33 @@
 : getNeighbors()
+)
  %7B%0A        int c
@@ -3331,444 +3331,61 @@
     
-int count = 0;%0A        %0A        if(getNorthEasternCell() != null && getNorthEasternCell().isAlive()) %7B%0A            count++;%0A        %7D%0A%0A        if(getNorthWesternCell() != null && getNorthWesternCell().isAlive()) %7B%0A            count++;%0A        %7D%0A%0A        if(getSouthEasternCell() != null && getSouthEasternCell().isAlive()) %7B%0A            count++;%0A        %7D%0A%0A        if(getSouthWesternCell() != null && getSouthWesternCell().isAlive()) %7B%0A
+    if(neighbor != null && neighbor.isAlive()) %7B%0A    
     
@@ -3401,38 +3401,32 @@
 ount++;%0A    
-    %7D%0A
         
 %0A        ret
@@ -3413,39 +3413,22 @@
         
+%7D
 %0A    
-    return count;%0A
     %7D%0A  
@@ -3433,437 +3433,52 @@
     
-%0A
     
-private int countCardinalLivingNeighbors() %7B%0A        int count = 0;%0A%0A        if(northernCell != null && northernCell.isAlive()) %7B%0A            count++;%0A        %7D%0A%0A        if(southernCell != null && southernCell.isAlive()) %7B%0A            count++;%0A        %7D%0A%0A        if(easternCell != null && easternCell.isAlive()) %7B%0A            count++;%0A        %7D%0A%0A        if(westernCell != null && westernCell.isAlive()) %7B%0A            count++
+%0A        assert count %3E= 0 && count %3C= 8
 ;%0A  
@@ -3479,26 +3479,24 @@
  8;%0A        
-%7D%0A
 %0A        ret

Update Cell.java