Skip to content
Snippets Groups Projects

Resolve "Fix large grid display"

Merged Colin DAMON requested to merge 110-fix-large-grid-display into master
3 files
+ 66
28
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -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);
}
Loading