blogs.conchango.com

welcome to the conchango blogging site
Welcome to blogs.conchango.com Sign in | Join | Help
in Search

Fat man and his mixed bag of goodies

OO, Patterns and Sudoku Solver: Part 1

Primary focus of this blog entry is to demonstrate some of the power and flexibility offered by object orientated programming and design patterns. I will try to demonstrate these techniques and how to use them to solve complex problems like Sudoku.

I chose Sudoku rather than "Hello world" because it's fun, complex and seems like all the rage at the moment.

I have divided this entry in to multiple parts for the benefit of all.

Part 1 is for people who are only interested in acquiring a free Sudoku solver but don't necessarily give a damn about any of the programming techniques mentioned above.

Part 2 is for those who are interested in knowing the different techniques and logic used to solve the problem and some statistics and pros and cons of each method.

Part 3 will be for developers who are interested into the actual code walkthrough.

Well without any further babble I will describe the usage of the Sudoku solver.

The main screen of Sudoku Solver

 

Starting a new sudoku.

  1. Click on New menu item and it will pop up the following dialog box

 

  1. Choose the required grid size (9 X 9 is the standard size).
  2. Choose the solving strategy (quick solver possible vals is the fastest one).
  3. Tick the populate sample data check box if you want to populate a sample sudoku.
  4. Click OK button. 

Now you will have a sudoku on the main screen as shown before.

Click on any blank cell you want to put a value in and it will show you a drop down box with values as shown below

Select a value and it will be placed in that cell.

Solving sudoku

Once you have finished placing all the values click on the Solve menu item to solve the sudoku.

The tool will solve any sudoku with a valid solution.

Restarting a Sudoku.

If you want to start a new sudoku based on the same grid and solving strategy then click on Reset menu else start over a new sudoku based on new options by clicking on New menu item again.

More...

You can also use this tool as a sudoku generator by placing just a couple of random values and clicking solve button.

Any grid bigger than 25 X 25 will be pure academic and with no fun value to the sudoku fans. So I have restricted it to that size through UI but for the benefit of the geeks the underlying code is made capable of solving sudoku of any grid size and is only restricted by the memory and processing power.

You can download the zip file for this sudoku solver at the end of this post. Please note that you will need .NET 2 runtime on your machine in order to run this programme. If you dont have it then you can download it from Microsoft website here.

Published 23 April 2007 00:11 by Manoj.Oswal
Attachment(s): Sudoku.zip

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Philip Coupar said:

I look forward to seeing your code!  

I only ever got as far as a brute force solver, it was quicker to write that than actually solve one for real :-)

April 23, 2007 14:04
 

jamie.thomson said:

Manoj,

Very impressive mate. I'm especially impressed that you managed to write a solver algorithm.

You might wanna put a link in your blog to where people can download dotnet Fx from.

-Jamie

April 23, 2007 18:12
 

Manoj said:

Thanks for the tip Jamie, I have now added the link to dotnet Fx.

April 23, 2007 20:11
 

Manoj said:

Good point Anthony.

Thats exactly what I am going to cover in the next two parts. Brute force is only good till a certain grid size and then the performance starts degrading drastically thats where you need to inject domain knowledge in to the brute force solver to make it quicker. I will be posting the source in the next part. It will be also interesting to plug in your solver in to the source.

April 24, 2007 09:54
 

Anthony.Steele said:

I did a suduko solver a while back, it was a fun and compact program, I can send the source to the core if you like (it only solves regular suduko, i.e. 9*9 grids).

I  used a "brute force" solver - it's guaranteed to find the right answer if it exists, and do it in less than a second with minimal code. It will be interesting to see how the other techniques stack up. I wouldn't asume that they are better just because they do it the way a person would. Chess and other board games, for instance, are done in a far more mathematical way (minimax), with exhaustive searches.

(this is an edit to previous comment - can we delete that comment?)

April 24, 2007 11:07
 

Imran.Zaffar said:

Well Done, Manoj! I look forward to testing it out for real!!!

April 24, 2007 15:02
 

James.Pipe said:

Good job fat man, although you've just managed to take all the enjoyment out of Suduko for me. How am I supposed to take any pleasure in solving one myself now when I know I could just use your tool. Anyhow, a couple of points of feedback for you in case you're looking to refine this:

1) Currently within each 9x9 (or whatever the user has selected) grid, you can select the same value multiple times, so that the puzzle cannot be solved. It would be useful if the select lists updated depending on what had already been selected.

2) You cannot change a value once you have selected it, which means if the user makes a mistake in entering values to be solved, they have to reset the grid and start again.

3) This is too clever, you should remove it otherwise people will start giving you more work to do

April 26, 2007 21:47
 

Manoj said:

Thank JP

My main focus was on the actual solver and not on the UI so there are lots of things that can be done to make it user friendly. For your point no 1, there are hidden goodies in the code that stops you from doing it which are not exposed in the current UI. That is covered in the next post and will explain how that can be used to make the tool effectively a sudoku trainer. I have used those facilities myself in the building and debugging phase of this tool and I was very surprised myself to see cells being resolved in a way I never thought before.

April 27, 2007 09:59
 

jamie.thomson said:

Where's parts 2 & 3?

May 3, 2007 22:38
 

greendaale said:

Hi.

Thanks for a great tutorial/lecture about Sudoku programming. It would be very nice to get the code for the solver. My email is enigmakick at hotmail com.

Greetings!

October 7, 2007 10:49

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Personal Edition), by Telligent Systems