From 85cb4aa36b7ba3f49fdd038810cca20fde8b6d23 Mon Sep 17 00:00:00 2001 From: Lars Bonczek Date: Sun, 2 Nov 2025 13:42:02 +0100 Subject: [PATCH 1/2] Implement puzzle uniqueness --- sudoku.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) 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; + } } } } From abe627c09aa62f42abab088c39c1856acdd8c80b Mon Sep 17 00:00:00 2001 From: Lars Bonczek Date: Sun, 2 Nov 2025 13:58:52 +0100 Subject: [PATCH 2/2] Update README.md --- README.md | 3 --- 1 file changed, 3 deletions(-) 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 --------------------------------------------------------------------------------