SIGN UP MEMBER LOGIN:    
ARTICLE

Sudoku generating and solving using C# in PocketPC SDK

Posted by Alex Groysman Articles | Games Programming C# November 20, 2005
The article is about a program writen in C# to create a simple game application - sudoku. The game can generate several diffculty level sudokus with one solution, solve previously entered puzzles etc.
Reader Level:
Download Files:
 

The sudoku game which sources can be downloaded above is a simple C# application created for pocketPC to show the use of Knuth's dancing links algorithm implementation to solve any sudoku puzzle.

The main idea is to transform sudoku grid into a bitwise matrix as it is described in Knuth's lessons to use a simple algotirhm to find combination of lines in the matrix that create one line filled with 1's without repeating.

The interface is simple and created using a button array.

There are 2 main ideas implmented in the game :

  1. Is a solver itself. (solver.cs - implementation of a solver class ).
  2. How to generate a valid uniq solution sudoku puzzle.

In order to do so, I have used a few tricks to increase generation speed :

  1. First I fill first 27 places (main diagonal - top left qubicle, middle qubicle and lower right qubicle with a random set of numbers 1..9, since there are no intersections it is quiet easy to do).
  2. I use solver to find a first possible solution for a created grid.
  3. I start removing numbers until I get a predefined number of clues left on grid while solution is unique.

If removing a number I get a non unique solution ( or solution can not be found) the number is put back and so on.

Login to add your contents and source code to this article
share this article :
post comment
 

My solver class searches for all possible solutions using a total coverage matrix.

Each combination of lines that gives a line of 1's ( one time per cell) is a solution.

I limit search to look for maximum 2 solutions. If only one found - we have unique solution, if 2 - a soluiton is not unique.

Posted by Alex Groysman Jan 25, 2006

Cuando eliminas un número o varios, ¿ Cómo determinas que la solución se única?

Gracias.


When you eliminate a number or several, How you determine that the unique solution?

Thanks

Posted by Patricio Vargas Jan 22, 2006
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor