- 583 名前:デフォルトの名無しさん mailto:sage [2012/04/29(日) 01:27:13.41 ]
- せっかくなのですべてのセルが並行に動くように>581を修正
case class Cell(var alive:Int) extends Actor { var neighborhood = List[Cell]() def act() = { neighborhood.foreach(_ ! alive) var count = 0; var s = 0 loopWhile(count < neighborhood.size) { receive { case i:Int => s += i; count += 1 } } andThen { alive = if(s == 3 || s == 4 && alive == 1) 1 else 0 } } } case class Board(val lattice:List[List[Int]]) { val cells = lattice.map(_.map(x => Cell(x))) val m = lattice.size; val n = lattice(0).size for(i <- 0 until m; j <- 0 until n; k <- -1 to 1; l <- -1 to 1) cells(i)(j).neighborhood ::= cells((i+k+m)%m)((j+l+n)%n) def life() { cells.foreach(_.foreach(_.start)) } override def toString() = cells.map(_.map(_.alive.toString).reduce(_ + "," + _)).reduce(_ + "\n" + _) } val board = Board(List(List(0,1,1,1,0),List(0,1,0,0,0),List(0,0,1,0,0),List(0,0,0,0,0),List(0,0,0,0,0))) board.life print(board) evaluate/updateの分割が不要になったので少し短くなったか
|

|