diff --git a/README.md b/README.md index ef28ce7..71c7bcc 100644 --- a/README.md +++ b/README.md @@ -83,9 +83,6 @@ false, e.g., sudoku.generate("easy", false) ``` -Note: **Puzzle uniqueness is not yet implemented**, so puzzles are *not* -guaranteed to have unique solutions. - Solve a Sudoku puzzle -------------------------------------------------------------------------------- diff --git a/sudoku.js b/sudoku.js index 65039da..54d2819 100644 --- a/sudoku.js +++ b/sudoku.js @@ -80,11 +80,9 @@ e.g., 0 -> 17, and 100 -> 81. - By default, the puzzles are unique, uless you set `unique` to false. + By default, the puzzles are unique, unless you set `unique` to false. (Note: Puzzle uniqueness is not yet implemented, so puzzles are *not* guaranteed to have unique solutions) - - TODO: Implement puzzle uniqueness */ // If `difficulty` is a string or undefined, convert it to a number or @@ -98,7 +96,9 @@ MIN_GIVENS); // Default unique to true - unique = unique || true; + if(typeof unique === "undefined"){ + unique = true; + } // Get a set of squares and all possible candidates for each square var blank_board = ""; @@ -161,8 +161,17 @@ // Double check board is solvable // TODO: Make a standalone board checker. Solve is expensive. - if(sudoku.solve(board)){ - return board; + const solution = sudoku.solve(board); + if(solution){ + if(!unique){ + return board; + } + // Check if "backwards" solution is equal to regular solution. + // If it is, the sudoku has a unique solution. + const reverseSolution = sudoku.solve(board, true); + if(reverseSolution == solution){ + return board; + } } } }