Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- case class Point(x: Int, y: Int, alive: Boolean)
- case class Universe(points: Vector[Point], size: Int) {
- def apply(x: Int, y: Int) = points((x % size) * (y % size))
- }
- def aliveNeighbors(u: Universe)(px: Int, py: Int): Int = {
- val xs = for {
- x <- (py - 1) to (py + 1)
- y <- (px - 1) to (px + 1) if x != px && y != py
- } yield {
- if (u(x, y).alive) 1
- else 0
- }
- xs.sum
- }
- def life(u: Universe): Universe = {
- val aliveNeighborsU = aliveNeighbors(u)(_, _)
- val liveLimits = 2 to 3
- val updatedPoints = u.points map {
- case p @ Point(x, y, false) if aliveNeighborsU(x, y) == 3 => p.copy(alive = true)
- case p @ Point(x, y, true) if !liveLimits.contains(aliveNeighborsU(x, y)) => p.copy(alive = false)
- }
- u.copy(points = updatedPoints)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement