Created
March 29, 2011 08:38
-
-
Save bjartek/892006 to your computer and use it in GitHub Desktop.
Minesweeper solver 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
val input = "..*\n...\n*.."; | |
val grid = input.split("\n").map(_.toArray) | |
def bomb(coord: Tuple2[Int,Int]) = grid.lift(coord._1).flatMap(_.lift(coord._2)) match { | |
case Some('.') => 0; | |
case Some('*') => 1; | |
case None => 0 | |
} | |
def nearby(x:Int, y:Int) = for( rx <- x-1 to x+1; ry <- y-1 to y+1) yield (rx,ry) | |
def score(x:Int, y:Int) : String = if(bomb((x,y)) == 1) " " else nearby(x,y).map(bomb).sum.toString | |
val cells = grid.indices.map( x => grid(x).indices.map( y => score(x,y))) | |
cells.foreach(x => println(x.mkString)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment