Skip to content
Snippets Groups Projects
Commit 81aff3af authored by Colin DAMON's avatar Colin DAMON
Browse files

Fix large grid displays

parent ee451b73
No related branches found
No related tags found
No related merge requests found
This commit is part of merge request !82. Comments created here will be created in the context of that merge request.
......@@ -8,21 +8,26 @@ 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);
}
private Set<Cell> buildDeadCells(Set<Cell> aliveCells) {
return aliveCells
.stream()
.map(Box::new)
.flatMap(Box::cells)
.filter(cell -> !aliveCells.contains(cell))
.collect(Collectors.toUnmodifiableSet());
return aliveCells.stream()
.map(Box::new)
.flatMap(Box::cells)
.filter(cell -> !aliveCells.contains(cell))
.collect(Collectors.toUnmodifiableSet());
}
public boolean isAlive(Cell cell) {
return aliveCells.contains(cell);
}
public Collection<Cell> deadCells() {
......@@ -60,11 +65,15 @@ public class Grid {
}
private Stream<Cell> cells() {
return IntStream.range(leftColumn(), rightColumn()).mapToObj(Integer::valueOf).flatMap(column -> cellsColumn(column));
return IntStream.range(leftColumn(), rightColumn())
.mapToObj(Integer::valueOf)
.flatMap(column -> cellsColumn(column));
}
private Stream<Cell> cellsColumn(Integer column) {
return IntStream.range(topRow(), bottomRow()).mapToObj(Integer::valueOf).map(row -> new Cell(row, column));
return IntStream.range(topRow(), bottomRow())
.mapToObj(Integer::valueOf)
.map(row -> new Cell(row, column));
}
public int topRow() {
......@@ -95,17 +104,29 @@ public class Grid {
}
private static Point topLeft(Set<Cell> cells) {
int row = cells.stream().mapToInt(Cell::row).min().orElse(0);
int row = cells.stream()
.mapToInt(Cell::row)
.min()
.orElse(0);
int column = cells.stream().mapToInt(Cell::column).min().orElse(0);
int column = cells.stream()
.mapToInt(Cell::column)
.min()
.orElse(0);
return new Point(row - 1, column - 1);
}
private static Point bottomRight(Set<Cell> cells) {
int row = cells.stream().mapToInt(Cell::row).max().orElse(0);
int column = cells.stream().mapToInt(Cell::column).max().orElse(0);
int row = cells.stream()
.mapToInt(Cell::row)
.max()
.orElse(0);
int column = cells.stream()
.mapToInt(Cell::column)
.max()
.orElse(0);
return new Point(row + 1, column + 1);
}
......
......@@ -21,28 +21,31 @@ public class StringRenderer implements Renderer<String> {
private String run() {
return IntStream
.range(grid.topRow(), grid.bottomRow())
.mapToObj(Integer::valueOf)
.map(row -> rowRepresentation(grid, row))
.collect(Collectors.joining(System.lineSeparator(), "", System.lineSeparator()));
.range(grid.topRow(), grid.bottomRow())
.mapToObj(Integer::valueOf)
.map(row -> rowRepresentation(grid, row))
.collect(
Collectors.joining(System.lineSeparator(), "",
System.lineSeparator()));
}
private String rowRepresentation(Grid grid, Integer row) {
return IntStream.range(grid.leftColumn(), grid.rightColumn()).mapToObj(cellRepresentation(row)).collect(Collectors.joining());
private String rowRepresentation(Grid grid,
Integer row) {
return IntStream
.range(grid.leftColumn(), grid.rightColumn())
.mapToObj(cellRepresentation(row))
.collect(Collectors.joining());
}
private IntFunction<String> cellRepresentation(Integer row) {
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));
}
}
}
......@@ -14,16 +14,30 @@ class StringRendererUnitTest {
@Test
void shouldDisplayGridWithOneAliveCell() {
assertThat(renderer.run(grid().alive(1, 1).build())).isEqualTo(join("xxx", "xox", "xxx"));
assertThat(renderer.run(grid().alive(1, 1)
.build())).isEqualTo(join("xxx", "xox", "xxx"));
}
@Test
void shouldDisplayGridWithTwoAliveCell() {
assertThat(renderer.run(grid().alive(1, 1).alive(1, 3).build())).isEqualTo(join("xxxxx", "xoxox", "xxxxx"));
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()));
return Arrays.stream(parts)
.collect(Collectors.joining(System.lineSeparator(),
"", System.lineSeparator()));
}
private GridBuilder grid() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment