Created
February 19, 2012 18:59
-
-
Save machisuji/1865190 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Pool[T](values: Iterator[T], width: Int, height: Int) { | |
class Cell[T](value: T, neighbours: => Seq[Cell[T]]) { | |
lazy val Seq(top, right, bottom, left) = neighbours | |
} | |
val cells = for (x <- 0 until width) yield ( | |
for (y <- 0 until height) yield | |
new Cell(values.next, Seq(cellAt(x, y - 1), cellAt(x + 1, y), cellAt(x, y + 1), cellAt(x - 1, y)))) | |
def cellAt(x: Int, y: Int): Cell[T] = cells(translate(x, width))(translate(y, height)) | |
def translate(c: Int, max: Int) = c match { | |
case n if n == max => 0 | |
case -1 => max - 1 | |
case n => n | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment