-
-
Save GabrielJones646/e4bd52c40cdb74146633 to your computer and use it in GitHub Desktop.
This file contains 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 ScalaJSExample extends js.JSApp { | |
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 = Page.canvas | |
val renderer = Page.renderer | |
val (w, h) = (sandbox.height.toDouble, sandbox.height.toDouble) | |
var current=(handleString(toad2,true)) | |
def main(): 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) | |
}, 100 // make this smaller to speed up | |
) | |
} | |
} |
This file contains 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