Created
January 13, 2013 13:50
-
-
Save julien-lafont/4524203 to your computer and use it in GitHub Desktop.
Code Retreat 2012 : Game of Life #scala
This file contains hidden or 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
object Monde { | |
def apply() : Monde = { | |
val cells = (1 until 100).toList.map(i => Cellule(i, i+1, 9)) | |
Monde(cells) | |
} | |
def fusion(possibles: List[Cellule]) : List[Cellule] = { | |
possibles./:[List[Cellule]](Nil){ (acc, cell) => | |
acc.partition{ c => c.x == cell.x && c.y == cell.y } match { | |
case (Nil, _) => cell :: acc | |
case (elem :: _, restant) => cell.copy(coeff = elem.coeff + cell.coeff) :: restant | |
} | |
} | |
} | |
def garderSurvivants(fusion: List[Cellule]) = | |
fusion./:[List[Cellule]](Nil)((acc, c) => | |
if(c.coeff == 3 || c.coeff == 11 || c.coeff == 12) | |
c.copy(coeff=9) :: acc | |
else acc) | |
} | |
case class Monde(cellules: List[Cellule]) { | |
lazy val cellulesVoisines = cellules.flatMap(c => c :: c.voisins.toList) | |
def next = Monde(Monde.garderSurvivants(Monde.fusion(cellulesVoisines))) | |
} | |
case class Cellule(x: Int, y: Int, coeff:Int = 1) { | |
private def offsetOf(n: Int) = Array(-1, 0, 1) map (_ + n) | |
lazy val voisins = for { | |
xn <- offsetOf(x) | |
yn <- offsetOf(y) if (xn,yn) != (x,y) | |
} yield Cellule(xn, yn) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment