Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sudoku initialize functions
const newSolvedBoard = _ => {
startTime = new Date
// Create an unaffiliated clone of a fresh board
const newBoard = BLANK_BOARD.map(row => row.slice() )
// Populate the board using backtracking algorithm
fillPuzzle(newBoard)
return newBoard
}
function newStartingBoard (holes) {
// Reset global iteration counter to 0 and Try to generate a new game.
// If counter reaches its maximum limit in the fillPuzzle function, current attemp will abort
// To prevent the abort from crashing the script, the error is caught and used to re-run
// this function
try {
counter = 0
let solvedBoard = newSolvedBoard()
// Clone the populated board and poke holes in it.
// Stored the removed values for clues
let [removedVals, startingBoard] = pokeHoles( solvedBoard.map ( row => row.slice() ), holes)
return [removedVals, startingBoard, solvedBoard]
} catch (error)
return newStartingBoard(holes)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment