Skip to content

C++ source code and sample app to solve any Sudoku game

License

Notifications You must be signed in to change notification settings

jselbie/SudokuSolver

Repository files navigation

SudokuSolver is a C++ library and sample app that can solve any Suduko game
board that can be resolved with standard elimination and deduction logic. This
is code that I originally wrote in 2006 and 2007. I recently rediscovered the
code as a result of inspecting an old hard drive. For this GitHub release, I
cleaned up the code, added modern C++ 11 styles, and made it cross-compile on
both Windows and Unix.

There are some published Suduko puzzles on the Internet that claim to be
"worlds hardest Suduko" or labeled as "hard", but require the solver to use
guess work and backtracking - those are out of scope for this program.

The code is Standard C++ 11 code and should compile most anywhere where your
C++ compiler supports C++ 11. I've tested Windows, Linux, and Mac.

Example. Assume you have an input file that represents the initial state of a
Sudoku board with "." used to indicate an unsolved square. Such a file is
already provided with this source code.

    $> cat input.txt
    9.63.51..
    ..5....4.
    ...9....6
    ..1.24...
    .8.5.3.6.
    ...16.9..
    3....7...
    .1....8..
    ..24.16.3

To compile the code:
    $> g++ -std=c++11 cell.cpp sudokuboard.cpp main.cpp -o solver

To compile on Windows, simply start a new Visual Studio project add all the
*.cpp files to the project.

To run the code:
    $> ./solver input.txt

And the corresponding output will show the initial state of the board,
and a lot of log statements.  The final state of the board is also shown
if the board is solved.

9 ? 6 | 3 ? 5 | 1 ? ?
? ? 5 | ? ? ? | ? 4 ?
? ? ? | 9 ? ? | ? ? 6
---------------------
? ? 1 | ? 2 4 | ? ? ?
? 8 ? | 5 ? 3 | ? 6 ?
? ? ? | 1 6 ? | 9 ? ?
---------------------
3 ? ? | ? ? 7 | ? ? ?
? 1 ? | ? ? ? | 8 ? ?
? ? 2 | 4 ? 1 | 6 ? 3
---------------------

TripleSearch - 2 removed from cell at (r=7 c=5)
TripleSearch - 6 removed from cell at (r=7 c=5)
...

9 4 6 | 3 7 5 | 1 8 2
7 2 5 | 8 1 6 | 3 4 9
1 3 8 | 9 4 2 | 7 5 6
---------------------
6 9 1 | 7 2 4 | 5 3 8
4 8 7 | 5 9 3 | 2 6 1
2 5 3 | 1 6 8 | 9 7 4
---------------------
3 6 9 | 2 8 7 | 4 1 5
5 1 4 | 6 3 9 | 8 2 7
8 7 2 | 4 5 1 | 6 9 3
---------------------

Number of scans - 4
Board has been solved
Board is valid

About

C++ source code and sample app to solve any Sudoku game

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages