Sample Snapshot
Snapshot Which Shows the Possible Values
Introduction
This demo is a representation of 9 X 9 Sudoku Game. Users can enter the values very easily. Backtracking algorithm is used to complete the puzzle automatically. The source code for the algorithm is included. This is mainly a 9 X 9 game. But it can be easily extended to other formats.
The following is a brief description which shows various steps in the game:
- One can easily move around the board using the arrow keys or mouse. Once you are on a particular cell, you can enter the appropriate value. The accepted values are from 1 to 9.
- Once you enter the values, you have to make the board ready. This basically means the values you entered are locked down and cannot be changed.
- Once the board is ready, you can start solving the puzzle or press F9 to solve it automatically.
- At any time, you can easily save the current game. This will be saved into an XML file.
- You can load an existing game by dragging and dropping the game XML file. Please refer to the sample XML files to see the format of the XML.
- At any point of time, you can display the possible values in the cell by pressing F4. You can always select a value from the possible values by clicking the possible number using the left mouse.
- Press F11 to create a New puzzle where you can start solving the puzzle.
Background
I was always looking for a Sudoku program where I can enter the values easily. I couldn't find any program, hence I created one such program. I also included the source code which shows the algorithm I used. I primarily used the backtracking algorithm.
The following are the main features of the program:
- One can easily enter the values
- One can save and load the game
- Solve the game automatically
- Display all the possible values
I also included some puzzle XML files under the puzzles folder. In order to load an XML, just drag and drop the XML file onto the game. I basically wrote the whole program in 3 hours. So don't expect much. I didn't have time to optimize the code yet. For the Algorithm part, there are only 2 classes:
SudokuSolver
Cell
SudokuSolver
internally uses Cell
class. Sudoku board is primarily divided into grids. Each grid is further divided into cells. All the IDs run from left to right.
The following Id's representation shows a Sudoku board with 9 grids:
0 1 2
3 4 5
6 7 8
The below representation shows a Sudoku board with 3 Grids and each grid consisting of 9 cells...
0 1 2
0 1 2 0 1 2 0 1 2
3 4 5 3 4 5 3 4 5
6 7 8 6 7 8 6 7 8
... etc.
The above representation is for the UI on which you enter the values. The XML file is saved in the above format.
Using the Code
In order to enter the cell values, use the SetCellValue(...)
method.
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
m_Solver.SetCellValue(i, j, cellValue);
}
}
But for entering the values for the SudokoSolver
, the Cell
Ids range from 0 to 9.
Below is a representation of the SudokoSolver
with 9 X 9 Sudoku puzzle:
0 1 2 3 4 5 6 7 8
1 . . . . . . .
2 . . . . . . .
3
4
5
6
7
8
So the above representation will be transposed as below for (row, col):
(0,0) (0,1) (0,2) (0,3) ....... (0,7) (0,8)
(1,0) (1,1) (1,2) (1,3) ....... (1,7) (1,8)
(2,0) (2,1) (2,2) (2,3) ....... (2,7) (2,8)
..............................................
..............................................
..............................................
(7,0) (7,1) (7,2) (7,3) ....... (7,7) (7,8)
(8,0) (8,1) (8,2) (8,3) ....... (8,7) (8,8)
Solve the puzzle by calling the SolveSudoku()
method. This method either returns true
which means it was able to solve the puzzle automatically. If it returns false
, it means a failure.
if (!m_Solver.SolveSudoku())
{
}
else
{
}
After the puzzle is solved, you can retrieve the values as follows:
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
int cellValue = m_Solver.GetCellValue(i, j);
}
}
History
- R 0.1 (14th January, 2008)
- R 0.2 (21st January, 2008)
- R 0.3 (13th April, 2010)
- Fixed the bug which allows the user to enter the numbers using the numpad