diff --git a/game-of-life/src/main/java/fr/ippon/life/Grid.java b/game-of-life/src/main/java/fr/ippon/life/Grid.java index 92ed44436563dc2fde13fcc9f9f00cb14c983465..1314e587da88ddda83418b4e099baf0e58058c0a 100644 --- a/game-of-life/src/main/java/fr/ippon/life/Grid.java +++ b/game-of-life/src/main/java/fr/ippon/life/Grid.java @@ -8,10 +8,12 @@ import java.util.stream.Stream; public class Grid { + private final Set<Cell> aliveCells; private final Set<Cell> deadCells; private final Box border; Grid(Set<Cell> aliveCells) { + this.aliveCells = aliveCells; this.deadCells = buildDeadCells(aliveCells); border = new Box(aliveCells); } @@ -25,6 +27,10 @@ public class Grid { .collect(Collectors.toUnmodifiableSet()); } + public boolean isAlive(Cell cell) { + return aliveCells.contains(cell); + } + public Collection<Cell> deadCells() { return deadCells; } diff --git a/game-of-life/src/main/java/fr/ippon/life/StringRenderer.java b/game-of-life/src/main/java/fr/ippon/life/StringRenderer.java index 03a602d2d405ac0a2166063fc9bd3e76cda1de8c..09d6eb6ea26ad599d164ca6f6bcbc449c5121e68 100644 --- a/game-of-life/src/main/java/fr/ippon/life/StringRenderer.java +++ b/game-of-life/src/main/java/fr/ippon/life/StringRenderer.java @@ -33,16 +33,12 @@ public class StringRenderer implements Renderer<String> { private IntFunction<String> cellRepresentation(Integer row) { return column -> { - if (aliveCell(row, column)) { + if (grid.isAlive(new Cell(row, column))) { return "o"; } return "x"; }; } - - private boolean aliveCell(Integer row, int column) { - return !grid.deadCells().contains(new Cell(row, column)); - } } } diff --git a/game-of-life/src/test/java/fr/ippon/life/StringRendererUnitTest.java b/game-of-life/src/test/java/fr/ippon/life/StringRendererUnitTest.java index 5afdb183b19accff6ae519d75ee5f3a8092065ad..a071db7a6cdf5afc32dc382d43efdb81c03f1ece 100644 --- a/game-of-life/src/test/java/fr/ippon/life/StringRendererUnitTest.java +++ b/game-of-life/src/test/java/fr/ippon/life/StringRendererUnitTest.java @@ -22,6 +22,11 @@ class StringRendererUnitTest { assertThat(renderer.run(grid().alive(1, 1).alive(1, 3).build())).isEqualTo(join("xxxxx", "xoxox", "xxxxx")); } + @Test + void shouldDisplayGridWithTwoDistantAliveCell() { + assertThat(renderer.run(grid().alive(1, 1).alive(1, 7).build())).isEqualTo(join("xxxxxxxxx", "xoxxxxxox", "xxxxxxxxx")); + } + private static String join(String... parts) { return Arrays.stream(parts).collect(Collectors.joining(System.lineSeparator(), "", System.lineSeparator())); }