How to Program Sudoku

Programming Sudoku is a challenge for computer programmers. Could a little advice help you in creating your next masterpiece?

Several programmers have contacted me in the last few years seeking advice on how to program Sudoku. I always advise them to read the book Programming Sudoku by Wei-Meng Lee. I have read the book and compiled the code. Here is my review and opinion of this book.

Programming Sudoku Book

This book teaches you how to create a Sudoku program using Visual Basic 2005 which is included in Microsoft Visual Studio Professional 2005.

Note: Today I would recommend using Microsoft Visual Studio 2008 Professional instead.

Even if you are not a Visual Basic pro, you can most likely follow the advice in this book and create a basic Sudoku program. You could also easily port the code given to C# or other languages.

The book begins by explaining the basic rules of Sudoku and terminology to establish a firm foundation.

You build on this foundation by creating your first Sudoku project. You are given, step by step, everything you need to type into your Visual Basic editor to create your user interface.

If you are not familiar with Visual Basic 2005, the book explains in sufficient detail what you are attempting to do, where to find what you need, and what to type into each control or code function.

The nice thing about the book Programming Sudoku is, you are first given a detailed explanation of each algorithm that you are about to enter in your editor. This helps you better understand the logic and the functionality of the code. Each code function is given as needed.

You enter the code just as you would expect – function by function. Just read the book and follow step by step the instructions that are given. A complete code listing is found in the Appendix of the book.

I was able to enter and compile the program as given with only one minor issue that surfaced. Due to a minor version change, I was required to add a partial line of code to satisfied the compiler. I don’t remember exactly where that line of code resides.

Programming Sudoku

My Opinion Of The Program

The program found in the book Programming Sudoku is basic. It uses the CRME (Column Row Minigrid Elimination) technique to solve for naked singles. Additional algorithms include looking for what the author calls Lone Rangers, Twins and Triplets.

You will not find any algorithm harder than triplets. To solve puzzles that need advanced techniques it uses a brute force algorithm. This algorithm is basically a trial and error method of solving Sudoku.

There is a lot of room for improvement in this program. One glaring error is the program does not generate a Sudoku puzzle with a single solution for all difficulty levels. A valid Sudoku puzzle must have a single answer.

The program does not create advanced Sudoku puzzles. By that I mean, you will not find Quads, X-Wing, XY-Wing, and Swordfish patterns. Nor will you find any solving by colors techniques.

People who solve advanced Sudoku puzzles often want the ability to pencil in their candidates for cells either manually or automatically. This program does not provide that functionality.

The puzzle does solve a cell each time you click the Hint button. Some puzzle solvers will prefer that the software gives a verbal hint rather than solving a cell. You can also solve the complete puzzle when you click the Solve Puzzle button.

I don’t see any great need to have each move tabulated in the Activities window. This functionality slows the program noticeably.

The overall size of the puzzle window is too small for my liking. The lines separating the mini-grids are hard to see with my aging baby boomer eyes. I would increase the puzzle size to fill the computer screen window.

I still recommend buying the book Programming Sudoku despite the faults I find in the program. This book will help you understand issues that Sudoku programmers will face.