Skip to content
Snippets Groups Projects

Resolve "Game of life part 2"

Merged Colin DAMON requested to merge 106-game-of-life-part-2 into master
7 files
+ 218
70
Compare changes
  • Side-by-side
  • Inline
Files
7
@@ -2,24 +2,16 @@ package fr.ippon.life;
import java.util.Set;
public class Cell {
public record Cell(int row, int column) {
private static final int LOW_POPULATION_THRESHOLD = 2;
private static final int HIGH_POPULATION_THRESHOLD = 3;
private static final int BORN_THRESHOLD = HIGH_POPULATION_THRESHOLD;
private final int row;
private final int column;
public Cell(int row, int column) {
this.row = row;
this.column = column;
}
boolean stayAlive(Set<Cell> aliveCells) {
long neighbourgCount = countNeigbours(aliveCells);
return neighbourgCount == LOW_POPULATION_THRESHOLD || neighbourgCount == HIGH_POPULATION_THRESHOLD;
return neighbourgCount == LOW_POPULATION_THRESHOLD
|| neighbourgCount == HIGH_POPULATION_THRESHOLD;
}
boolean born(Set<Cell> aliveCells) {
@@ -27,7 +19,9 @@ public class Cell {
}
private long countNeigbours(Set<Cell> aliveCells) {
return aliveCells.stream().filter(this::isNeighbour).count();
return aliveCells.stream()
.filter(this::isNeighbour)
.count();
}
private boolean isNeighbour(Cell other) {
@@ -35,38 +29,11 @@ public class Cell {
return false;
}
return delta(row, other.row) <= 1 && delta(column, other.column) <= 1;
return delta(row, other.row) <= 1
&& delta(column, other.column) <= 1;
}
private static int delta(int first, int second) {
return Math.abs(first - second);
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + column;
result = prime * result + row;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
Cell other = (Cell) obj;
if (column != other.column) return false;
if (row != other.row) return false;
return true;
}
}
Loading