Created
August 23, 2010 20:32
-
-
Save hoheinzollern/546274 to your computer and use it in GitHub Desktop.
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
/** | |
* Builds a new sudoku instance given a difficulty level | |
* | |
* @param level describes the difficulty level of the generated game | |
* - 1 normal | |
* - 2 medium | |
* - 3 hard | |
*/ | |
def makeSudoku(level: Int): Sudoku = { | |
var board = new Array[Array[Int]](9, 9) | |
// Initialize board to correct values | |
for (i <- 0 to 8) { | |
for (j <- 0 to 8) { | |
board(i)(j) = ((j + i * 6 + i / 3) % 9 + 1) | |
} | |
} | |
// Shuffle values between rows | |
for (h <- 0 to 60) { | |
// FIXME: use correct methods | |
val i = (Math.random * 100).asInstanceOf[Int] % 3 | |
val j = (Math.random * 100).asInstanceOf[Int] % 3 | |
val k = (Math.random * 100).asInstanceOf[Int] % 3 | |
val choose = (Math.random * 100).asInstanceOf[Int] % 2 | |
if (choose == 0 && j != k) { | |
println ("i= " + i + " j= " + j + " k= " + k) | |
val swap = board(i*3 + j) | |
board(i*3 + j) = board(i*3 + k) | |
board(i*3 + k) = swap | |
} else { | |
println ("i= " + i + " j= " + j + " k= " + k) | |
for (l <- 0 to 8) { | |
val swap = board(l)(i*3 + j) | |
board(l)(i*3 + j) = board(l)(i*3 + k) | |
board(l)(i*3 + k) = swap | |
} | |
} | |
} | |
var sudoku = new Sudoku | |
sudoku.setBoard(board) | |
if (sudoku.checkConstraints == false) { | |
Dialog.showMessage(null, "Constraint verification failed", "Error", Dialog.Error) | |
} | |
return sudoku | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment