Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------------------------------------------------------------------------
Expand Down
21 changes: 15 additions & 6 deletions sudoku.js
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This note can be deleted now since you've implemented this functionality 😄

guaranteed to have unique solutions)
TODO: Implement puzzle uniqueness
*/

// If `difficulty` is a string or undefined, convert it to a number or
Expand All @@ -98,7 +96,9 @@
MIN_GIVENS);

// Default unique to true
unique = unique || true;
if(typeof unique === "undefined"){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this!

unique = true;
}

// Get a set of squares and all possible candidates for each square
var blank_board = "";
Expand Down Expand Up @@ -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){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use strict comparison here (===)

return board;
}
}
}
}
Expand Down