Skip to content

Instantly share code, notes, and snippets.

@megawac
Last active July 27, 2016 16:37
Show Gist options
  • Save megawac/3c82089929d404bbf72834d5471c7435 to your computer and use it in GitHub Desktop.
Save megawac/3c82089929d404bbf72834d5471c7435 to your computer and use it in GitHub Desktop.
rough sudo code for emkay
function checkSquare(matrix, i, j) {
return columnValid(matrix, i) && rowValid(matrix, j)
}
function solvePuzzle(boardMatrix) {
(i, j) = selectFirstUnfilled(boardMatrix)
matrix = boardMatrix.clone()
matrix[i, j] = red
if (checkSquare(matrix, i, j)) {
try {
return solvePuzzle(matrix)
} catch() {
// do nothing, just continue the and try to solve with blue
}
}
matrix[i, j] = blue
if (checkSquare(matrix, i, j)) {
return solvePuzzle(matrix)
} else {
throw 'Board unsolvable'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment