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;
@@ -8,21 +8,26 @@ import java.util.stream.Stream;
public class Grid {
public class Grid {
 
private final Set<Cell> aliveCells;
private final Set<Cell> deadCells;
private final Set<Cell> deadCells;
private final Box border;
private final Box border;
Grid(Set<Cell> aliveCells) {
Grid(Set<Cell> aliveCells) {
 
this.aliveCells = aliveCells;
this.deadCells = buildDeadCells(aliveCells);
this.deadCells = buildDeadCells(aliveCells);
border = new Box(aliveCells);
border = new Box(aliveCells);
}
}
private Set<Cell> buildDeadCells(Set<Cell> aliveCells) {
private Set<Cell> buildDeadCells(Set<Cell> aliveCells) {
return aliveCells
return aliveCells.stream()
.stream()
.map(Box::new)
.map(Box::new)
.flatMap(Box::cells)
.flatMap(Box::cells)
.filter(cell -> !aliveCells.contains(cell))
.filter(cell -> !aliveCells.contains(cell))
.collect(Collectors.toUnmodifiableSet());
.collect(Collectors.toUnmodifiableSet());
}
 
 
public boolean isAlive(Cell cell) {
 
return aliveCells.contains(cell);
}
}
public Collection<Cell> deadCells() {
public Collection<Cell> deadCells() {
@@ -60,11 +65,15 @@ public class Grid {
@@ -60,11 +65,15 @@ public class Grid {
}
}
private Stream<Cell> cells() {
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) {
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() {
public int topRow() {
@@ -95,17 +104,29 @@ public class Grid {
@@ -95,17 +104,29 @@ public class Grid {
}
}
private static Point topLeft(Set<Cell> cells) {
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);
return new Point(row - 1, column - 1);
}
}
private static Point bottomRight(Set<Cell> cells) {
private static Point bottomRight(Set<Cell> cells) {
int row = cells.stream().mapToInt(Cell::row).max().orElse(0);
int row = cells.stream()
.mapToInt(Cell::row)
int column = cells.stream().mapToInt(Cell::column).max().orElse(0);
.max()
 
.orElse(0);
 
 
int column = cells.stream()
 
.mapToInt(Cell::column)
 
.max()
 
.orElse(0);
return new Point(row + 1, column + 1);
return new Point(row + 1, column + 1);
}
}
Loading