Sudoku Showcase
Public Member Functions | Public Attributes | List of all members
sudoku.solver.Solver Class Reference

Solves Sudoku puzzles using a combination of constraint propagation and recursive backtracking. More...

Public Member Functions

def __init__ (self, Puzzle puzzle)
 
set[tuple[int]] solutions (self)
 (property) The unique solutions found by the solver. More...
 
None mergeSolutions (cls, "Solver" this, "Solver" other)
 (classmethod) Merge the solutions of the solver. More...
 
None printSolutions (self)
 Function to print the found solution console. More...
 
bool solve (self)
 Solve the puzzle. More...
 

Public Attributes

 puzzle
 

Detailed Description

Solves Sudoku puzzles using a combination of constraint propagation and recursive backtracking.

The solving process follows two stages:

The constraint propagation step acts as an optimizer to reduce the search space efficiently, but does not guarantee completeness or uniqueness of the solution on its own.

The backtracking algorithm selects the field with the fewest remaining candidates, clones the current puzzle state, applies one candidate, and then continues solving recursively with a new Solver instance.

If multiple valid solutions are detected during recursion, the solving process aborts to preserve uniqueness checks.

The Solver is primarily used for generating and validating uniquely solvable puzzles. While the implementation focuses on readability and understandability rather than maximum optimization, it still performs efficiently even on sparse puzzles.

Constructor & Destructor Documentation

◆ __init__()

def sudoku.solver.Solver.__init__ (   self,
Puzzle  puzzle 
)

Member Function Documentation

◆ mergeSolutions()

None sudoku.solver.Solver.mergeSolutions (   cls,
"Solver"  this,
"Solver"  other 
)

(classmethod) Merge the solutions of the solver.

NOTE: this will be the Solver with the updated solution.

Parameters
thisSolver - the Solver to merge the solution into
otherSolver - the Solver to merge from
Returns
None

◆ printSolutions()

None sudoku.solver.Solver.printSolutions (   self)

Function to print the found solution console.

Returns
None

◆ solutions()

set[tuple[int]] sudoku.solver.Solver.solutions (   self)

(property) The unique solutions found by the solver.

A solution is a serialized puzzle with no empty fields. To add a solution

See also
_addSolution() or mergeSolutions()
Returns
set[tuple[int | None]]

◆ solve()

bool sudoku.solver.Solver.solve (   self)

Solve the puzzle.

Tries to solve the Puzzle using chain of constraints and if stuck, brute force

Returns
bool - if unique solution

Member Data Documentation

◆ puzzle

sudoku.solver.Solver.puzzle

The documentation for this class was generated from the following file: