Skip to content

Instantly share code, notes, and snippets.

@bjartek
Created March 29, 2011 08:38
Show Gist options
  • Save bjartek/892006 to your computer and use it in GitHub Desktop.
Save bjartek/892006 to your computer and use it in GitHub Desktop.
Minesweeper solver in scala
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