Skip to content

Instantly share code, notes, and snippets.

@ColdSauce
Last active March 13, 2017 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ColdSauce/332f89c84f06cceca20c4a473536457a to your computer and use it in GitHub Desktop.
Save ColdSauce/332f89c84f06cceca20c4a473536457a to your computer and use it in GitHub Desktop.
code golf
object Bubble {
val r = scala.util.Random
def getNewBoardIteration(board: Vector[Vector[Boolean]]): Vector[Vector[Boolean]] = {
val allFalse = board.zipWithIndex.map(row =>
row._1.zipWithIndex.filter(col =>
if(((row._2 % 2) + col._2) % 2 == 0)
!col._1
else
false
)
).zipWithIndex.filter(x => x._1.length > 0)
val firstIndex = r.nextInt(allFalse.length)
val secondIndex = r.nextInt(allFalse(firstIndex)._1.length)
val index = (allFalse(firstIndex)._2, allFalse(firstIndex)._1(secondIndex)._2)
board.updated(index._1, board(index._1).updated(index._2, true))
}
def printBoard(board: Vector[Vector[Boolean]]) = {
board.zipWithIndex.map(row => {
row._1.zipWithIndex.map(col => {
val current = col._1
if(current) {
print("X")
} else {
if(((row._2 % 2) + col._2) % 2 == 0) {
print("O")
} else {
print("_")
}
}
})
println("")
})
}
def main(args: Array[String]): Unit = {
val first = args(0).toInt
val second = args(1).toInt
val emptyVectors = Vector.fill(second, first)(false)
(0 to (first * second) / 2 - (((first * second) - 1) % 2)).foldLeft[Vector[Vector[Boolean]]] (emptyVectors) ((acc, curr) => {
val newBoard = getNewBoardIteration(acc)
Thread.sleep(1000)
System.out.print("\033[H\033[2J");
System.out.flush();
printBoard(newBoard)
newBoard
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment