12,816,654 members (33,047 online)
alternative version

#### Stats

32.1K views
22 bookmarked
Posted 14 Jan 2008

# Sudoku

, 21 Jan 2008 CPOL
 Rate this:
Play Sudoku (9X9) and solve using the Backtracking algorithm

## 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:

1. 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.
2. 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.
3. Once the board is ready, you can start solving the puzzle or press F9 to solve it automatically.
4. At any time, you can easily save the current game. This will be saved into an XML file.
5. 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.
6. 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.
7. 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:

1. One can easily enter the values
2. One can save and load the game
3. Solve the game automatically
4. 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:

1. `SudokuSolver`
2. `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())
{
// Algorithm couldn't find the solution
}
else
{
// Algorithm successfully solved the puzzle
}
```

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

## Share

 United States
No Biography provided

## You may also be interested in...

 First Prev Next
 Grid Setup jeremycrouse27-Mar-12 7:41 jeremycrouse 27-Mar-12 7:41
 Re: Grid Setup rajesh amara4-Apr-12 7:46 rajesh amara 4-Apr-12 7:46
 Wow! Member 54018432-Jun-10 16:14 Member 5401843 2-Jun-10 16:14
 Re: Wow! rajesh amara21-Jul-10 19:29 rajesh amara 21-Jul-10 19:29
 project code Johnpdavidson@hotmail.co.uk24-May-10 0:16 Johnpdavidson@hotmail.co.uk 24-May-10 0:16
 Re: project code [modified] rajesh amara21-Jul-10 19:17 rajesh amara 21-Jul-10 19:17
 Last Visit: 31-Dec-99 19:00     Last Update: 23-Mar-17 19:33 Refresh 1