Skip to content

Instantly share code, notes, and snippets.

@machisuji
Created February 19, 2012 18:59
Show Gist options
  • Save machisuji/1865190 to your computer and use it in GitHub Desktop.
Save machisuji/1865190 to your computer and use it in GitHub Desktop.
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