spacer
home / programming / javascript / gr / column16 / 1 To page 1To page 2current page
[previous]

Programmer / Writer
Aquent
US-WA-Redmond

Justtechjobs.com Post A Job | Post A Resume
Developer News
Cisco Lawsuit: A Test for the GPL?
Shifts for Enterprise Linux, Green Networks in '09
Gifts for All in Linux 2.6.28

JavaScript Sudoku Puzzle Solver

Strategies

There are a number of distinct strategies for eliminating symbols from cells and the purpose of the iterate() code is to cycle through them one by one. Each strategy is defined by a function that takes the puzzle object as an argument and returns a numeric value to indicate the result of executing the strategy; a value of –1 indicates an error of some kind, a value of 0 indicates that nothing was done and a positive value indicates that modifications were made to the solution. The strategy functions are stored in an array as follows…

 

SudokuPuzzle.prototype.strategies = [

function(puzzle)

{

  ...

},

function(puzzle)

{

  ...

},

etc

];

The advantage of storing them like this is that strategies may be added or removed without needing to modify the iterate() function that calls them. Have a look at the following line of code from the iterate() function…

  var nReturn = this.strategies[this.nIteration++ % this.strategies.length](this);

There’s a lot going on in this one line of code. The remainder of this.nIteration over the number of strategies is used to index into the array of strategy functions. This function is then called, passing the SudokuPuzzle object as the argument. Finally, the value of this.nIteration is post-incremented so that for the next iteration, a different strategy will be used.

Let’s have a look at the different strategies…

Strategy 1: Eliminate Dead Certs

This is the simplest and most obvious strategy and it derives directly from the rules of the puzzle; if any cell contains a definite symbol, then all other cells in the same row or column or group must not display that symbol. In other words, that symbol is eliminated from the solution sets of all the other cells in the same row/column/group set.

Strategy 2: Isolate individuals on each row/col/group

This strategy is a little more complex than the previous one. The idea is if a token appears only once in any row, column or group, then any other tokens that might appear in the same solution cell can be eliminated. Consider the following example…

34

1

134

2

46

5

2345

12

6

14

4

13

1

4

3

15

2

6

26

5

2

14

3

1

2456

3

245

456

1

2

256

126

125

3

56

4

This grid shows an intermediate state of the puzzle. Numbers in bold show the original starting position, numbers in red indicate potential symbols for a cell. Looking at the first cell in the second row (shown in italics), the symbol “5” appears only once in the top left hand group (also in the same row). It follows therefore that “5” can be placed in that cell and since every row/column/group contains a complete set of symbols, then that cell must contain the symbol “5”

Strategy 3: Look for Patterns

This strategy is quite subtle and like the previous one it relies on certain properties that emerge from the rules of Sudoku. This time, cells with incomplete solutions are matched against all the other cells in the same row, column and grid, counting the number of cells with exactly the same incomplete solution. In the example grid below, in the fourth column, there are two cells with the value “14” (marked in italics)…

34

1

134

2

46

5

2345

12

6

14

4

13

1

4

3

15

2

6

26

5

2

14

3

1

2456

3

245

456

1

2

256

126

125

3

56

4

Now, if the number of cells is the same as the number of symbols in the string, those symbols must be distributed only amongst those counted cells. In the example grid, there are exactly two cells containing the value “14” (shown in italics ) so either the first one will have the “1” and the second will have the “4” or the other way around. What's certain is that both the “1” and the “4” cannot exist in any other cell in that column. Two other cells in that column - on the third and fifth rows (underlined) - contain those symbols so they can be filtered to become “5” and “56”.

Strategy 4: Look for row or column limited symbols

In this strategy, each token in each group is analysed to see whether inside a group, a token is limited to a single column or row. Consider the example below…

 

34

1

134

2

46

5

2345

12

6

14

4

13

1

4

3

15

2

6

26

5

2

14

3

1

2456

3

245

456

1

2

256

126

125

3

56

4

In the bottom left group the symbol “4” appears only on one row (underlined). This implies that wherever the “4” goes in that group, it will be on that row and the logical conclusion is that it will not in any other group on that row. Using this strategy in the example grid above, the fourth column in that row containing the symbols “456” can be changed to “56”.

Demo

To see the SudokuPuzzle solver in action click here

Source Code

Copy the following code and save as SudokuPuzzle.html

Conclusion

In this article I've created a JavaScript Sudoku puzzle solver program. The program allows a user to enter any size of puzzle and starting position and the solver will search for the solution. Four separate solver strategies were shown and when used in combination, solved all of the Sudoku puzzles I was able to test.

Disclaimer

While I have endeavoured to make this code as browser compatible as possible, it's only been tested it with Internet Explorer (6.0) and Netscape (7.1) as these browsers are used by a large proportion of users.


About the Author

Guyon Roche is a freelance web developer in London, Great Britain. He specializes in Windows platforms with an interest in bridging the gaps between different technologies, visit www.silver-daggers.co.uk for more details. He can be reached via e-mail at guyonroche@silver-daggers.co.uk

home / programming / javascript / gr / column16 / 1 To page 1To page 2current page
[previous]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

webref The latest from WebReference.com Browse >
An Introduction to 3D · Email Marketing Terms to Know · Search Engines 101: Paid Vs. Natural Search
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Mastering SSH: Connecting, Executing Remote Commands and Using Authorized Keys · Connecticut Town Lays Groundwork for Merged School, Municipal VoIP Network · Wi-Fi for your Car, Truck, or MPV

Created: March 27, 2003
Revised: October 21, 2005

URL: http://webreference.com/programming/javascript/gr/column16/1