Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Logical Sudoku Solver

0.00/5 (No votes)
8 Nov 2007 1  
Utilize logical rules and scanning to solve soduku puzzle, no guessing

Introduction

I played sudoku for a while, then I just want to write something that solves the puzzle like I do. Following is an example I used with debugmode = false, if anybody doesn't like this part, please let me know, I will be happy to remove it from the screen and from my code.

7  |  2|9  
 3 | 4 |   
2  |1  |  8
-----------
  5|  7|   
42 |   | 35
   |3  |8  
-----------
5  |  4|  9
   | 8 | 5 
  1|2  |  7

=======================================

7  |  2|9  
 3 | 4 |   
2  |1  |  8
-----------
3 5|4 7|   
42 |   | 35
   |3  |8  
-----------
5  |  4|  9
   | 8 | 5 
  1|2  |  7

=======================================

7  |  2|9  
 3 | 4 |   
2  |1  |  8
-----------
385|4 7|   
42 |   | 35
   |3  |8  
-----------
5  |  4|  9
   | 8 | 5 
  1|2  |  7

=======================================

7  |  2|9  
 3 | 4 |   
2  |17 |  8
-----------
385|4 7|   
42 |   | 35
   |3  |8  
-----------
5  |  4| 89
   | 8 | 5 
8 1|2  |  7

=======================================

7  |  2|91 
 3 | 4 |   
2  |17 |  8
-----------
385|4 7|   
42 |   | 35
   |3  |8 4
-----------
5  |  4| 89
   | 8 | 5 
8 1|2  |  7

=======================================

7 4|  2|91 
13 | 4 |   
2  |17 |  8
-----------
385|4 7|   
42 |   | 35
 1 |3  |8 4
-----------
5  |  4| 89
   | 8 | 5 
8 1|2  |  7

=======================================

7 4|8 2|91 
138| 4 |   
2  |17 |  8
-----------
385|4 7|   
42 |   | 35
 1 |3  |8 4
-----------
5  |  4| 89
   | 8 | 5 
8 1|2  |  7

=======================================

7 4|8 2|91 
138|54 |   
2  |17 |  8
-----------
385|4 7|   
42 |  8| 35
 1 |3  |8 4
-----------
5  |  4| 89
   | 8 | 5 
8 1|2  |  7

=======================================

754|8 2|91 
138|549|   
2  |17 |5 8
-----------
385|4 7|   
42 |  8| 35
 1 |3  |8 4
-----------
5  |  4| 89
   | 81| 5 
8 1|2  |  7

=======================================

754|862|913
138|549|   
2  |173|548
-----------
385|4 7|  1
42 |  8| 35
 1 |3  |8 4
-----------
5  | 34|189
   | 81| 5 
8 1|2  | 67

=======================================

754|862|913 
138|549|  6 
2  |173|548
-----------
385|4 7|6 1 
42 | 18| 35
 1 |3 6|8 4
-----------
5 2| 34|189
  3| 81| 52
8 1|2 5|367

=======================================

754|862|913
138|549|276
2  |173|548
-----------
385|4 7|6 1
426|918|735
917|356|8 4
-----------
5 2| 34|189
6 3| 81|452
841|295|367

=======================================

754|862|913
138|549|276
269|173|548
-----------
385|427|691
426|918|735
917|356|824
-----------
572|634|189
693|781|452
841|295|367

Finished!!!

Using the code

program.cs contains  
            Sudoku sudoku = new Sudoku();
            //sudoku.debugmode = true;

            //sudoku.writeLog = false;

            sudoku.Test();
You can make debugmode = true to display those rules involved, also can write them to log if the console screen is not convenient to read it.

Points of Interest

Nothing fancy, it would be better if I apply one rule at a time instead of all of them. Top1465 sudoku questions involve 'guessing', which, IMHO, ruins the fun of solving sudoku logically.

History

1.0 Created

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here