Last active
April 15, 2016 03:36
-
-
Save fancellu/9131923 to your computer and use it in GitHub Desktop.
Conway's Game of Life in 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
import org.scalajs.dom | |
// Paste here http://www.scala-js-fiddle.com/ | |
// Ctrl-enter to recompile | |
@JSExport | |
object ScalaJSExample{ | |
type Coord = Pair[Int, Int] | |
type CoordSet = Set[Coord] | |
def handleString(str: String, identity:Boolean=false): String = { | |
val lines=str.stripMargin.trim.split("\n") | |
val coords: CoordSet = for ( | |
(line, x) <- lines.zipWithIndex toSet; | |
(ch, y) <- line.zipWithIndex if (ch == '*') | |
) yield (x, y) | |
def nextGen(coords: CoordSet): CoordSet = { | |
def getCube(c: Coord): CoordSet = { | |
for (dx <- Set(-1, 0, 1);dy <- Set(-1, 0, 1)) | |
yield (c._1 + dx, c._2 + dy) | |
} | |
// coords flatMap getCube gets us our search set, i.e. area around existing points | |
coords flatMap getCube filter | |
{coord=>val numNeigh = getCube(coord)-coord count coords; | |
(numNeigh == 3 || numNeigh == 2 && coords(coord))} toSet | |
} | |
def toString(coords: CoordSet): String ={ | |
val sx = lines.size | |
val sy = lines.map(_.size).max | |
Seq.tabulate(sx, sy)((x,y) => if (coords((x, y))) "*" else ".").map(_.mkString).mkString("\n") | |
} | |
toString(if (identity) coords else nextGen(coords)) | |
} | |
val toad2=""".......... | |
|...*....... | |
|.*..*...... | |
|.*.**...... | |
|..*........ | |
|....*...... | |
|........... | |
""" | |
val sandbox = dom.document | |
.getElementById("canvas") | |
.asInstanceOf[dom.HTMLCanvasElement] | |
val renderer = sandbox.getContext("2d") | |
.asInstanceOf[dom.CanvasRenderingContext2D] | |
val (w, h) = (sandbox.height.toDouble, sandbox.height.toDouble) | |
var current=(handleString(toad2,true)) | |
@JSExport | |
def main(args: Array[String]): Unit = { | |
dom.setInterval(() => { | |
renderer.fillStyle = s"white" | |
// renderer.fillRect(1, 1, 20, 20) | |
renderer.clearRect(0,0,w,h) | |
for ((line,idx)<-current.split("\n").zipWithIndex){ | |
renderer.fillText(line.map(_+" ").mkString,200,200+idx*20) | |
} | |
current=handleString(current) | |
}, 1000 // make this smaller to speed up | |
) | |
} | |
} | |
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
package playpen | |
// Check with this http://projects.abelson.info/life/ | |
object GameOfLife extends App { | |
type Coord = Pair[Int, Int] | |
type CoordSet = Set[Coord] | |
def handleString(str: String, identity:Boolean=false): String = { | |
val lines=str.stripMargin.trim.split("\n") | |
val coords: CoordSet = for ( | |
(line, x) <- lines.zipWithIndex toSet; | |
(ch, y) <- line.zipWithIndex if (ch == '*') | |
) yield (x, y) | |
def nextGen(coords: CoordSet): CoordSet = { | |
def getCube(c: Coord): CoordSet = { | |
for (dx <- Set(-1, 0, 1);dy <- Set(-1, 0, 1)) | |
yield (c._1 + dx, c._2 + dy) | |
} | |
// coords flatMap getCube gets us our search set, i.e. area around existing points | |
coords flatMap getCube filter | |
{coord=>val numNeigh = getCube(coord)-coord count coords; | |
(numNeigh == 3 || numNeigh == 2 && coords(coord))} toSet | |
} | |
def toString(coords: CoordSet): String ={ | |
val sx = lines.size | |
val sy = lines.map(_.size).max | |
Seq.tabulate(sx, sy)((x,y) => if (coords((x, y))) "*" else ".").map(_.mkString).mkString("\n") | |
} | |
toString(if (identity) coords else nextGen(coords)) | |
} | |
val toad="""...... | |
|...*.. | |
|.*..*. | |
|.*..*. | |
|..*... | |
|...... | |
""" | |
val block=""".... | |
|.**. | |
|.**. | |
|....""" | |
val toad2="""...... | |
|...*.. | |
|.*..*. | |
|.*.**. | |
|..*... | |
|....*. | |
""" | |
val glider=""".*... | |
|..*.. | |
|***.. | |
|.....""" | |
val start=toad2 | |
var current=start | |
println(handleString(current,true)) | |
println | |
for (loop<-0 to 11){ | |
current=handleString(current) | |
println(loop+1) | |
println(current) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment