Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@x3ro
Created June 20, 2011 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save x3ro/1036778 to your computer and use it in GitHub Desktop.
Save x3ro/1036778 to your computer and use it in GitHub Desktop.
/**
* The Distribution type assumes works as follows:
* For any x Water Cells (._1) there are y Fishes (._2) and z Sharks (._3)
*/
type Distribution = (Int, Int, Int)
/**
* This object holds the Stream factory which
* is used to fill the Ocean with a given distribution.
* Needs the Ocean object to query an empty cell, but does
* not add the actual instances. That means that the Cell needs
* to be "spawned" in the ocean after every "take" operation
* out of the Stream, otherwise there could be collisions.
*/
object SpawnStream {
def apply(ocean:Ocean, d:Distribution):Stream[Option[Cell]] = {
next(0, ocean, d)
}
def next(i:Int, ocean:Ocean, d:Distribution):Stream[Option[Cell]] = {
val mod = i % (d._1 + d._2 + d._3);
val cell = i match {
case x if mod < d._1 => None
case x if mod < d._1 + d._2 => Some(new Cell(ocean.randomFreeCell) with Fish)
case _ => Some(new Cell(ocean.randomFreeCell) with Shark)
}
cell #:: next(i+1, ocean, d)
}
def main(args:Array[String]) {
SpawnStream(Ocean(10), (1,3,10)) take(10) foreach println _
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment