Commit 157084ce authored by Colin DAMON's avatar Colin DAMON

Fix large grid displays

parent ee451b73
......@@ -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;
}
......
......@@ -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));
}
}
}
......@@ -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()));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment