Click here to Skip to main content
15,895,746 members
Articles / Programming Languages / C#

Sudoku

Rate me:
Please Sign up or sign in to vote.
3.67/5 (6 votes)
21 Jan 2008CPOL3 min read 49.4K   2.3K   24   6
Play Sudoku (9X9) and solve using the Backtracking algorithm

Sample Snapshot

Sudoku2.JPG

Snapshot Which Shows the Possible Values

PossibleValues.JPG

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.

C#
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.

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

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGrid Setup Pin
jeremycrouse27-Mar-12 6:41
jeremycrouse27-Mar-12 6:41 
AnswerRe: Grid Setup Pin
rajesh amara4-Apr-12 6:46
rajesh amara4-Apr-12 6:46 
GeneralWow! Pin
Member 54018432-Jun-10 15:14
Member 54018432-Jun-10 15:14 
GeneralRe: Wow! Pin
rajesh amara21-Jul-10 18:29
rajesh amara21-Jul-10 18:29 
Generalproject code Pin
Johnpdavidson@hotmail.co.uk23-May-10 23:16
Johnpdavidson@hotmail.co.uk23-May-10 23:16 
GeneralRe: project code [modified] Pin
rajesh amara21-Jul-10 18:17
rajesh amara21-Jul-10 18:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.