Skip to content

Instantly share code, notes, and snippets.

@ArcticLight
Created December 14, 2016 05:01
Show Gist options
  • Save ArcticLight/eeab5b19fd01e544d877233cbc94306a to your computer and use it in GitHub Desktop.
Save ArcticLight/eeab5b19fd01e544d877233cbc94306a to your computer and use it in GitHub Desktop.
A really dumb implementation of Minesweeper
package me.arcticlight.minesweeper
import scala.collection.mutable
import scala.io.StdIn
/**
* Created by bogos on 12/13/2016.
*/
object Minesweeper {
def main(args: Array[String]): Unit = {
val board = mutable.IndexedSeq.fill(8,8){0}
for(i <- Seq.range(0,9)) {
val a = Math.abs(scala.util.Random.nextInt()) % 8
val b = Math.abs(scala.util.Random.nextInt()) % 8
board(a)(b) = -1
}
var failed = false
while(!failed) {
println(showBoard(board))
val (x,y) = userInput()
if(board(x)(y) == -1) {
println("You lose!")
println(showBoard(board.map(x => x.map(y=> if(y == 0) 1 else y))))
return
}
board(x)(y) = 1
}
}
def showBoard(board: Seq[Seq[Int]]): String = {
board.zipWithIndex.map({case (z,x) =>
z.zipWithIndex.map({case (v,y) =>
if(v <= 0) "X"
else calcNeighbors(board, x, y)
}).mkString("["," ","]")
}).mkString("\n")
}
def calcNeighbors(board: Seq[Seq[Int]], x: Int, y: Int): String = {
val neighbors = Seq((-1,-1),(0,-1),(1,-1),(-1,0),(1,0),(-1,1),(0,1),(1,1))
neighbors.map({
case (i,j) => board.lift(x+i).flatMap(z=>z.lift(y+j))
}).map({
case Some(i) if i == -1 => 1
case _ => 0
}
).sum.toString
}
def userInput(): (Int, Int) = {
print("Selection: ")
val input = StdIn.readLine()
val split = input.split(",")
(Integer.parseInt(split(0).trim()), Integer.parseInt(split(1).trim()))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment