Thursday, July 7, 2011

Sudoku Solver with GUI written in python (Part 1 of 4)

Sudoku Solver is a small graphical app for solving any given Sudoku puzzle, almost instantaneously. It also shows the steps required to solve the same.

(Have a look at one of the toughest Sudoku puzzles here. Sudoku Solver can solve it too, and it is the toughest one I have found till date!)

This is how Sudoku Solver looks like -

For what audience is this post intended?

Although I have primarily written this post for sharing my python code, this can be useful for you if -

You want to learn how to solve a Sudoku puzzle by hand.

You want to yourself make a Sudoku solving program/application.

You want to see some large, fairly complicated, working python code.

You don't care how the Sudoku Solver works, you just want to use it to solve some of your Sudoku puzzles.

OK, I am interested. Give me some background of Sudoku.

Sudoku is a logic based placement puzzle. A Sudoku puzzle consists of a 9 x 9 grid cells which are divided into 9 rows, columns and subgrids (as shown above). The task is to place the numbers from 1 to 9 in the empty cells in such a way that, in every row, column and 3 × 3 subgrid, each number appears only once.

How to solve a Sudoku puzzle?

Although the rules of Sudoku are quite simple, solving it can sometimes become a tough task.
I have posted this separately with lots of pictorial examples here - How To Solve A Sudoku puzzle?

So what can this program do? Show me some examples.

It can solve any Sudoku puzzle, almost instantaneously.

Example -

Take any Sudoku puzzle you like, from anywhere - newspapers, websites, etc. (For this example, I have taken the one from here.) Fill up the Sudoku Solver accordingly (How do I use the Sudoku Solver?).

Click on Solve. The puzzle will be solved instantly. Note that it shows the steps required to solve the puzzle in the side pane.

(Note the time taken to solve the puzzle - 0.05 seconds!)

What's different in this?

Mostly on the web, you will find either of these -

Lots of websites having tonnes of Sudoku puzzles to solve and their final answers (but they don't show the steps required to solve these puzzles).

Some Sudoku solving apps/applets/programs, but they can solve only the simpler ones.

With my app, you can -

Solve any Sudoku puzzle.

Also see the steps required to solve the same.

OK, so where is the source code?

The project is hosted on GitHub (version 1.2 onwards).
If you simply wish to download the source code, see the next section.

I started programming this in mid-May, 2011. It took around 10 days for me to complete it. A month later I decided to document it (which took much more time). Finally, I decided to host this as a project on on PyPI. It was fun! Now it is hosted on GitHub (version 1.2 onwards).