diff --git a/src/org/unioulu/tol/sqatlab/gameoflife/Cell.java b/src/org/unioulu/tol/sqatlab/gameoflife/Cell.java index b66e9c8..bdefd31 100644 --- a/src/org/unioulu/tol/sqatlab/gameoflife/Cell.java +++ b/src/org/unioulu/tol/sqatlab/gameoflife/Cell.java @@ -1,5 +1,71 @@ package org.unioulu.tol.sqatlab.gameoflife; public class Cell { + String state; + int x; + int y; + + public Cell(String initialState) { + this.state = initialState; + } + + public Cell(int x, int y) { + this.x = x; + this.y = y; + } + public int getX() { + + return x; + } + + public int getY() { + + return y; + } + + + + public void nextIteration(int numOfLiveCells) { + if (state == "Alive" && numOfLiveCells < 2) + state = "Dead"; + else if (state == "Alive" && numOfLiveCells > 3) + state = "Dead"; + else if (state == "Dead" && numOfLiveCells > 3) + state = "Dead"; + else if (state == "Dead" && numOfLiveCells < 3) + state = "Dead"; + else + state = "Alive"; + + } + + public Object getState() { + return this.state; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + x; + result = prime * result + y; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Cell other = (Cell) obj; + if (x != other.x) + return false; + if (y != other.y) + return false; + return true; +} } diff --git a/src/org/unioulu/tol/sqatlab/gameoflife/Grid.java b/src/org/unioulu/tol/sqatlab/gameoflife/Grid.java index 19dbf0b..e8d017c 100644 --- a/src/org/unioulu/tol/sqatlab/gameoflife/Grid.java +++ b/src/org/unioulu/tol/sqatlab/gameoflife/Grid.java @@ -1,5 +1,32 @@ package org.unioulu.tol.sqatlab.gameoflife; +import java.util.HashSet; +import java.util.Set; + + + public class Grid { + private Set cells = new HashSet<>(); + + public int getNumNeighbors(Cell cell) { + int numNeighbors = 0; + //Set neighborhood = new HashSet(); + for (int dx = -1; dx <= 1; dx++){ + for (int dy = -1; dy <= 1; dy++){ + Cell neighbor = new Cell(cell.x + dx, cell.y + dy); + if (cells.contains(neighbor)){ + numNeighbors++; + } + } + + } + + return numNeighbors-1; + } + + public void addCell(Cell cell) { + cells.add(cell); + + } } diff --git a/src/org/unioulu/tol/sqatlab/gameoflife/test/TestCell.java b/src/org/unioulu/tol/sqatlab/gameoflife/test/TestCell.java index d8de09e..fa74d23 100644 --- a/src/org/unioulu/tol/sqatlab/gameoflife/test/TestCell.java +++ b/src/org/unioulu/tol/sqatlab/gameoflife/test/TestCell.java @@ -3,12 +3,94 @@ import static org.junit.Assert.*; import org.junit.Test; +import org.unioulu.tol.sqatlab.gameoflife.Cell; public class TestCell { @Test - public void test() { - fail("Not yet implemented"); + public void testLiveCellWithNoNeighborsShouldDie() { + //arrange + Cell cell = new Cell("Alive"); + // + cell.nextIteration(0); + //assert + assertEquals("Dead", cell.getState()); + + } + + @Test + public void testLiveCellWithOneNeighborsShouldDie() { + //arrange + Cell cell = new Cell("Alive"); + // + cell.nextIteration(1); + //assert + assertEquals("Dead", cell.getState()); } + @Test + public void testLiveCellWithTwoNeighborsShouldLive() { + //arrange + Cell cell = new Cell("Alive"); + // + cell.nextIteration(2); + //assert + assertEquals("Alive", cell.getState()); + + } + + @Test + public void testLiveCellWithThreeNeighborsShouldLive() { + //arrange + Cell cell = new Cell("Alive"); + // + cell.nextIteration(3); + //assert + assertEquals("Alive", cell.getState()); + + } + + @Test + public void testLiveCellWithMoreThreeNeighborsShouldDie() { + //arrange + Cell cell = new Cell("Alive"); + // + cell.nextIteration(4); + //assert + assertEquals("Dead", cell.getState()); + + } + + @Test + public void testDeadCellWithThreeNeighborsShouldLive() { + //arrange + Cell cell = new Cell("Dead"); + // + cell.nextIteration(3); + //assert + assertEquals("Alive", cell.getState()); + + } + + @Test + public void testDeadCellWithMoreThanThreeNeighborsShouldBeDead() { + //arrange + Cell cell = new Cell("Dead"); + // + cell.nextIteration(4); + //assert + assertEquals("Dead", cell.getState()); + + } + + @Test + public void testDeadCellWithLessThanThreeNeighborsShouldBeDead() { + //arrange + Cell cell = new Cell("Dead"); + // + cell.nextIteration(4); + //assert + assertEquals("Dead", cell.getState()); + + } } diff --git a/src/org/unioulu/tol/sqatlab/gameoflife/test/TestGrid.java b/src/org/unioulu/tol/sqatlab/gameoflife/test/TestGrid.java index 9614d55..cc1cd7d 100644 --- a/src/org/unioulu/tol/sqatlab/gameoflife/test/TestGrid.java +++ b/src/org/unioulu/tol/sqatlab/gameoflife/test/TestGrid.java @@ -2,12 +2,51 @@ import static org.junit.Assert.*; import org.junit.Test; +import org.unioulu.tol.sqatlab.gameoflife.Cell; +import org.unioulu.tol.sqatlab.gameoflife.Grid; public class TestGrid { @Test - public void test() { - fail("Not yet implemented"); + public void testSingleCellOnGridHasNoNeighbors() { + //arrange + Grid grid = new Grid(); + Cell cell = new Cell(0,0); + + grid.addCell(cell); + + int numNeighbors = grid.getNumNeighbors(cell); + + assertEquals(0, numNeighbors); } + @Test + public void testTwoNeighborCellsShouldHaveOneNeighbor() { + //arrange + Grid grid = new Grid(); + Cell cell1 = new Cell(0,0); + Cell cell2 = new Cell(0,1); + + grid.addCell(cell1); + grid.addCell(cell2); + + int numNeighbors = grid.getNumNeighbors(cell1); + + assertEquals(1, numNeighbors); + } + + @Test + public void testTwoNeighborCellsOnDifferentRows() { + //arrange + Grid grid = new Grid(); + Cell cell1 = new Cell(0,0); + Cell cell2 = new Cell(1,1); + + grid.addCell(cell1); + grid.addCell(cell2); + + int numNeighbors = grid.getNumNeighbors(cell1); + + assertEquals(1, numNeighbors); + } }