- Details
- Category: Code Samples
- Hits: 4156

# Smart Sudoku Solver

### Smart Mode

It has 4 smart methods for finding individual numbers.

- It looks for cells that have only one possible value. That must then be the value for that cell.
- It looks in a row for the only place that can take a certain value. That value must then go in that cell. (this is repeated for columns, and squares)
- Looks for sets of two possibles in a row and removes those from being possible in the rest of the row (this is repeated for columns)
- Looks for sets of 3 possibles that match in a row, then will remove possibles along the same row. Will also match a set of 3, set of 3 and set of 2. ex. "156", "15", "156" would be a match and remove 1, 5, & 6 from the other possibles on that row. (repeated for columns)

As long as it has found some new numbers to fill in, then calls itself recursively and repeats the process.

This will solve almost all Sudoku puzzles. For those that do not complete, you must use brute force mode.

### Brute Force Mode

In brute force mode the program will look for the cell that has the least number of possibles.

It takes the first possible, tries to solve the puzzle. If it fails it will roll back and try the next possible number. It will repeat ths until it solves the puzzle or determines that the puzzle is unsolvable.

It currently has a limit of 500 recursions so that it runs quickly. It could be allowed to recurse every number of every cell which would solve every solvable Sudoku, but it would take quite a while. And remember that the purpose of this program is to use smart solving.

### Enter a puzzle

You are presented with a blank sudoku grid. You can enter your own puzzle, or click on one of the three buttons to quickly fill with a solvable puzzle. The first two puzzles come from *Will Shortz's Sudoku, February 2016*, puzzle number 196 and 197 (if you love Sudoku, I recommend this book).

The third puzzle is a very difficult one that must use brute force to solve it.

If you want to see the code's layout, click here.

If you want to see the source code, click here.