Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/org/unioulu/tol/sqatlab/gameoflife/Cell.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
27 changes: 27 additions & 0 deletions src/org/unioulu/tol/sqatlab/gameoflife/Grid.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
package org.unioulu.tol.sqatlab.gameoflife;

import java.util.HashSet;
import java.util.Set;



public class Grid {
private Set<Cell> cells = new HashSet<>();

public int getNumNeighbors(Cell cell) {
int numNeighbors = 0;
//Set<Cell> neighborhood = new HashSet<Cell>();
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);

}

}
86 changes: 84 additions & 2 deletions src/org/unioulu/tol/sqatlab/gameoflife/test/TestCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());

}
}
43 changes: 41 additions & 2 deletions src/org/unioulu/tol/sqatlab/gameoflife/test/TestGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}