Skip to content

Instantly share code, notes, and snippets.

@dsasse07
Last active February 17, 2021 13:26
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 dsasse07/c80c87add2973d10f9f6f30715179181 to your computer and use it in GitHub Desktop.
Save dsasse07/c80c87add2973d10f9f6f30715179181 to your computer and use it in GitHub Desktop.
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