Last active
February 17, 2021 13:26
-
-
Save dsasse07/c80c87add2973d10f9f6f30715179181 to your computer and use it in GitHub Desktop.
Sudoku initialize functions
This file contains hidden or 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
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