From 85cb4aa36b7ba3f49fdd038810cca20fde8b6d23 Mon Sep 17 00:00:00 2001 From: Lars Bonczek Date: Sun, 2 Nov 2025 13:42:02 +0100 Subject: [PATCH 1/4] 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/4] 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 -------------------------------------------------------------------------------- From c2a668543f14c2d56c04970a4b8e0fafe2bb2933 Mon Sep 17 00:00:00 2001 From: Lars Bonczek Date: Wed, 17 Dec 2025 00:20:53 +0100 Subject: [PATCH 3/4] Remove obsolete comment --- sudoku.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sudoku.js b/sudoku.js index 54d2819..7eb2a11 100644 --- a/sudoku.js +++ b/sudoku.js @@ -80,9 +80,7 @@ e.g., 0 -> 17, and 100 -> 81. - 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) + By default, the puzzles are unique, unless you set `unique` to false. */ // If `difficulty` is a string or undefined, convert it to a number or From 614fb262b5de45359e4e57538bbc459fee70b93c Mon Sep 17 00:00:00 2001 From: Lars Bonczek Date: Wed, 17 Dec 2025 00:21:28 +0100 Subject: [PATCH 4/4] Use strict comparison when comparing solutions --- sudoku.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sudoku.js b/sudoku.js index 7eb2a11..0ea6f67 100644 --- a/sudoku.js +++ b/sudoku.js @@ -167,7 +167,7 @@ // 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){ + if(reverseSolution === solution){ return board; } }