EEWeb Pulse - Volume 103

Page 24

PULSE

S u d a Logical Test Sudoku is a perfect logical test with some fun involved. For those who have never played Sudoku, it is a game starting with a grid containing 36 elements. The elements are arranged in rows and columns in a 9 by 9 grid style. The board generally comes with squares filled in based on difficulty. The number of elements shown does not directly correlate with the difficulty which is an important distinction. The order of the elements makes the difference. Shown below is an example of a Sudoku board.

The order they are to be filled in should match with every other row, column and 3x3 box. With this in mind, there are a slew of possibilities the numbers can be hidden to create different difficulties for the user. With a certain amount of logical reasoning, most puzzles can be solved without guessing. This got me thinking. I am studying computer engineering and from my experience this has been a degree with elements of both electrical and software engineering. Logic is present throughout all aspects of these engineering topics. With this in mind, building a Sudoku program in the Java coding language is a great test in improving both logical and engineering skills. This is not an article about building a Sudoku program from scratch and the possibilities that are available to do so. Like with any computer program, there are multitudes of different ways to structure and code a Sudoku program.

Figure 1. Example Sudoku Board

This appears to be around a medium difficulty Sudoku puzzle. The goal is to fill in this board so every cell has just one number. The numbers you choose are required to be chosen out of the range of 1 through 9. Notice how 3x3 boxes have been bolded as to distinguish a constraint. There should be no duplicates in the following fashion: • Each row should have the numbers 1–9 • Each column should have the numbers 1–9 • Each 3x3 box should have the numbers 1–9

26 24

Visit: eeweb.com

Originally, it seemed the simplest way to structure the programming for this project is to use FOR loops and IF statements. With some tinkering and multiple attempts later, a Sudoku program was materializing. This was possible because you could randomly generate a number. Then recursively through FOR loops you could eliminate from which constraints to eliminate numbers. For example, if a 3 was generated, you could save the three in an array, and you could then only generate 1–2 and 4–9. Doing this created a random board without duplicates.

Following the generation of the board came hiding the numbers. As I have mentioned before, just hiding more numbers does not determine difficulty. Some easy Sudoku puzzles have more hidden numbers than harder difficulty puzzles. So, how do you hide numbers in a way that changes difficulty? Essentially, when answering this question you have to ask yourself, “How do you want users answering your puzzle?” It then boils down to figuring out a similar version of a Sudoku solver. The key to discovering a solution lies in understanding how the game works scientifically. In order to create challenging puzzles in Sudoku, algorithms based on Knuth’s Algorithm X to solve the Exact Cover problem need to be developed. This solution is termed as the Dancing Links technique, which is fairly useful in software engineering. Here begins the problem. Exact Cover, Knuth’s Algorithm, and the Dancing Links Technique. We have what is technically called the “Exact Cover” problem. On Wikipedia’s terms, the exact cover problem is as such; “given a collection S of subsets of a set X, an exact cover is a subcollection S* of S such that each element in X is contained in exactly one subset in S*. One says that each element in X is covered by exactly one subset in S*. An exact cover is a kind of cover” (Wikipedia 2013). In relation to Sudoku, the exact cover problem is associated with the restrictions that make Sudoku the game it is. Each element represents the individual cells. The S* references the constraints of the game Sudoku. The collection


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.