skip to main content
Skip header Section
Sudoku Programming with CMarch 2015
Publisher:
  • Apress
  • 901 Grayson Street Suite 204 Berkely, CA
  • United States
ISBN:978-1-4842-0996-7
Published:12 March 2015
Pages:
300
Skip Bibliometrics Section
Bibliometrics
Skip Abstract Section
Abstract

Sudoku Programming with C teaches you how to write computer programs to solve and generate Sudoku puzzles. This is a practical book that will provide you with everything you need to write your own books of Sudoku Classic and Samurai puzzles. But be warned: after reading it, you'll discover that the puzzles in your local paper are not so challenging after all!We like Sudokus because they test our capacity to recognize and interpret patterns. But how are the clues generated? Where do those quasi-symmetrical configurations come from? When the author explored the Web to find out, he discovered that there were many sites that explained how to solve Sudokus, but none that told him how create them. He also saw many sites and apps to play Sudoku, but, perhaps not surprising, no indication of how they worked. So, he had to develop his own applications in order to find out. And, from the very start, he decided that he would publish the code for anyone else to use and perhaps tinker with, but the author wrote it in such a way that also lets readers with limited knowledge of programming techniques understand it. In fact, you could decide to start generating thousands of puzzles almost immediately, and go through the explanations of algorithms and techniques later, a bit at a time. The author chose to write the application in plain old C because he wanted to make the code accessible to as many people as possible. In this book, you will find an explanation of all solving strategies, and the code to implement them. Writing the Solver application was more difficult than writing the Generator, because it required designing and implementing each strategy separately. However, the author wanted to include a solving program capable of listing the strategies necessary to solve any particular puzzle. He also wanted to check whether a puzzle was solvable analytically, without any guessing. This book includes the full listings of both the Generator and the Solver, and explanations of all C modules, with walk-throughs and examples. What youll learn How to model a Sudoku puzzle in C What are the strategies in the main program as well as in the utilities needed How to implement numerous strategies and techniques for Sudoku generating and solving How to solve puzzles How to generate Sudokus What are Samurai Sudokus and how to solve them Who this book is for This book is for readers with limited knowledge of programming techniques. The C code is significant and so it is best to have some prior knowledge of C.

Contributors

Recommendations

Reviews

Andrew Robert Huber

This book is for readers whose primary interest is Sudoku puzzles. A knowledge of programming and C is assumed, as the book is not intended to teach these, nor how to become a software developer. It simply explains the author's programs to solve and generate Sudoku puzzles. Two C programs, Solver and Generator, are described in the book. The first chapter introduces the two programs and the representation of a Sudoku puzzle used in the programs (not surprisingly, a multi-dimensional array). The second chapter describes and shows examples of the 15 strategies used by the Solver program to solve Sudoku puzzles. The strategies are grouped into five levels of increasing sophistication (or difficulty). As a last resort, a 16th strategy, brute force with backtracking, is used. The explanations and examples are very helpful when discussing the actual implementation of each of the strategies. Most of the rest of the book (chapters 3 through 14) is devoted to the Solver program. Chapter 3 describes the main module that accepts the puzzle to be solved, invokes the various strategies until the puzzle is solved, and outputs the result. The following ten chapters then describe the implementation of the various strategies, in order of complexity. The chapters and discussions include complete C listings for the modules. The sources are available on the companion website, though strangely this is not mentioned, nor the location given, until chapter 14. By that time, the interested reader will have likely already searched for and found the sources. The discussion and explanation of the C modules is relatively straightforward and generally clear. The modules are in a consistent format, and the style of the C programs is reasonable and conforms to ANSI standard C. Arrays are used extensively with little use of pointers. The emphasis is on clarity rather than efficiency, so there are no hard-to-figure-out optimizations or coding tricks. There is, however, a dearth of comments in the code, though the explanation in the book largely makes up for that. One thing lacking in the program listings in the text is line numbers. These would have made it much easier to refer to code in the discussion, as well as made it much easier for the reader to know exactly which part of the program was being referred to. Instead, references are to things like the for loop, the second if statement, and so on, which the reader has to search for. I cannot guarantee that the programs are bug free; however, as a practicing software developer who writes and reviews C code on a daily basis, I found no errors in the code. The Generator program for generating Sudoku puzzles is described in a single chapter (15). The algorithm used is neither subtle nor very efficient, but it does work and it is simple to understand. The program generates a completely filled in (or solved) Sudoku grid at random by brute force, trying all possible combinations and backtracking when necessary. A configured number of cells in symmetrical positions are then cleared, again choosing the cells at random. If the resulting puzzle has a unique solution, the removal continues until a unique solution cannot be found. In checking for a solution, brute force is used rather than applying the Solver program. Different puzzles are generated using different seeds in the standard C random number generator. Chapter 16 does some analysis of puzzles generated by the Generator, showing statistics like how many clues (solved cells) are in each puzzle and how many and which strategies are needed to solve the puzzles. Chapter 17 shows how to generate puzzles with the clues in specific patterns, such as a smiley face or a heart. Chapter 18 shows how to generate multi-grid Sudokus-puzzles where some of the cells of one Sudoku overlap the cells of one or more other Sudokus. An appendix describes how to set up and run the Solver and Generator programs using Eclipse in a Windows or Mac operating system (OS) environment. Strangely, given the programs are written in C, no makefile is supplied with the sources, though for Linux users compiling the programs is straightforward. A short list of acronyms and a short index conclude the book. Though its purpose is limited, the book fulfills that purpose very well. Any programmer interested in Sudoku puzzles will find it interesting. The programs could be used as a starting point for other similar puzzles (Kakuro, anyone__?__). It can also be useful to anyone looking for an example of a nontrivial program to review or analyze, or how certain techniques such as backtracking can be used in practice. Online Computing Reviews Service

Access critical reviews of Computing literature here

Become a reviewer for Computing Reviews.