-
-
Save jbrains/788684 to your computer and use it in GitHub Desktop.
Revisions
-
jbrains revised this gist
Jan 20, 2011 . 6 changed files with 59 additions and 25 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ package rules; public class Cell { protected int numberOfLiveNeighbors; private CellState state; public Cell() { this.state = CellState.DEAD; } public Cell withLiveNeighbors(int numberOfLiveNeighbors) { this.numberOfLiveNeighbors = numberOfLiveNeighbors; return this; } public Cell thatIsAlive() { this.state = CellState.ALIVE; return this; } public boolean isAlive() { return (state == CellState.ALIVE); } @Override public String toString() { return "I'm " + state + " and I have " + numberOfLiveNeighbors + " live neighbors."; } } File renamed without changes.This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ package rules import spock.lang.Specification import spock.lang.Unroll class EvolveCellOneGeneration extends Specification { @Unroll("a dead cell with #liveNeighbors live neighbors dies") def "rules for cells to live or die"() { given: def inNextGeneration = new RulesForNextGeneration() def cell = initiallyAlive ? new Cell().thatIsAlive().withLiveNeighbors(liveNeighbors) : new Cell().withLiveNeighbors(liveNeighbors) expect: inNextGeneration.itTurnsOutThat(cell).comesAlive() == endsUpAlive where: liveNeighbors | initiallyAlive | endsUpAlive 0 | false | false 1 | false | false 2 | false | false 3 | false | true 4 | false | false 0 | true | false 1 | true | false 2 | true | true 3 | true | true 4 | true | false } } File renamed without changes.File renamed without changes.This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,25 +0,0 @@ -
patrickwilsonwelsh created this gist
Jan 20, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,88 @@ package rules; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; public class RulesForNextGenerationTest { private RulesForNextGeneration inNextGeneration; @Before public void setup() { inNextGeneration = new RulesForNextGeneration(); } @Test public void deadCellWith_0_LiveNeighbors_StaysDead() throws Exception { assertIsDeadNextTime(new Cell().withLiveNeighbors(0)); } @Test public void deadCellWith_1_LiveNeighbors_StaysDead() throws Exception { assertIsDeadNextTime(new Cell().withLiveNeighbors(1)); } @Test public void deadCellWith_2_LiveNeighbors_StaysDead() throws Exception { assertIsDeadNextTime(new Cell().withLiveNeighbors(2)); } @Test public void deadCellWith_3_LiveNeighbors_ComesAlive() throws Exception { assertIsAliveNextTime(new Cell().withLiveNeighbors(3)); } @Test public void deadCellWith_4_LiveNeighbors_staysdead() throws Exception { assertIsDeadNextTime(new Cell().withLiveNeighbors(4)); } @Test public void liveCellWith_0_LiveNeighbors_Dies() throws Exception { assertIsDeadNextTime(new Cell().thatIsAlive().withLiveNeighbors(0)); } @Test public void liveCellWith_1_LiveNeighbors_Dies() throws Exception { assertIsDeadNextTime(new Cell().thatIsAlive().withLiveNeighbors(1)); } @Test public void liveCellWith_2_LiveNeighbors_StaysAlive() throws Exception { assertIsAliveNextTime(new Cell().thatIsAlive().withLiveNeighbors(2)); } @Test public void liveCellWith_3_LiveNeighbors_StaysAlive() throws Exception { assertIsAliveNextTime(new Cell().thatIsAlive().withLiveNeighbors(3)); } @Test public void liveCellWith_4_LiveNeighbors_Dies() throws Exception { assertIsDeadNextTime(new Cell().thatIsAlive().withLiveNeighbors(4)); } private void assertIsAliveNextTime(Cell cell) { assertTrue(inNextGeneration.itTurnsOutThat(cell).comesAlive()); } private void assertIsDeadNextTime(Cell cell) { assertFalse(inNextGeneration.itTurnsOutThat(cell).comesAlive()); } // Test list // TODO a dead cell with 0 live neighbors stays dead. // TODO a dead cell with 1 live neighbors stays dead. // TODO a dead cell with 2 live neighbors stays dead. // TODO a dead cell with 3 live neighbors comes alive. // TODO a dead cell with 4 live neighbors stays dead. // TODO a live cell with 0 live neighbors dies. // TODO a live cell with 1 live neighbor dies. // TODO a live cell with 2 live neighbors stays alive. // TODO a live cell with 3 live neighbors stays alive. // TODO a live cell with 4 live neighbors dies. } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ package rules; public class RulesForNextGeneration { private Cell cell; public RulesForNextGeneration itTurnsOutThat(Cell cell) { this.cell = cell; return this; } public boolean comesAlive() { if (cell.isAlive() && cell.numberOfLiveNeighbors == 2) return true; return (cell.numberOfLiveNeighbors == 3); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ package rules; public class Cell { protected int numberOfLiveNeighbors; private CellState state; public Cell() { this.state = CellState.DEAD; } public Cell withLiveNeighbors(int numberOfLiveNeighbors) { this.numberOfLiveNeighbors = numberOfLiveNeighbors; return this; } public Cell thatIsAlive() { this.state = CellState.ALIVE; return this; } public boolean isAlive() { return (state == CellState.ALIVE); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ package rules; public enum CellState { DEAD, ALIVE }