Click here to Skip to main content
Click here to Skip to main content

Sudoku

, 21 Jan 2008
Rate this:
Please Sign up or sign in to vote.
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.

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

License

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

Share

About the Author

rajesh amara

United States United States
No Biography provided

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 21 Jan 2008
Article Copyright 2008 by rajesh amara
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid