Skip to content

Commit 32a048e

Browse files
Heiko SeebergerHeiko Seeberger
authored andcommitted
Game of life: Implementation for generations
1 parent 57d058b commit 32a048e

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/main/scala/Generation.scala

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
case class Generation(aliveCells: Set[Cell] = Set.empty) {
22

33
def next: Generation =
4-
???
4+
new Generation(stayingAlive ++ born)
5+
6+
def stayingAlive: Set[Cell] =
7+
aliveCells.filter(canStayAlive)
8+
9+
def born: Set[Cell] =
10+
aliveCells.flatMap(deadNeighbors).filter(canBeBorn)
11+
12+
def canStayAlive(cell: Cell): Boolean =
13+
Set(2, 3).contains(aliveNeighbors(cell).size)
14+
15+
def canBeBorn(cell: Cell): Boolean =
16+
aliveNeighbors(cell).size == 3
17+
18+
def aliveNeighbors(cell: Cell): Set[Cell] =
19+
cell.neighbors.intersect(aliveCells)
20+
21+
def deadNeighbors(cell: Cell): Set[Cell] =
22+
cell.neighbors.diff(aliveCells)
523
}

0 commit comments

Comments
 (0)