Last active
March 13, 2017 01:27
-
-
Save ColdSauce/332f89c84f06cceca20c4a473536457a to your computer and use it in GitHub Desktop.
code golf
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 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