Su Doku 2

The 2 in the title was added after an update to the original !Sudoku program. !Sudoku2 now creates Sudoku puzzles as well as solving them.

This program was written after I read an article in Archive and tried to solve the example puzzle. I found that it was frustratingly easy to make a mistake which wasn't noticed until 3 or 4 more numbers were filled in. Which numbers are wrong?
So I wrote this program so that I could make corrections and continue with the puzzle.

You can actually do a puzzle on screen (I haven't added any error checking - maybe that would be a useful addition) or just input the given numbers in your puzzle and click the solve button.

To input numbers just click on the relevant tile and type a number. To save you inputing a grid of numbers each time you want the same puzzle solved click on the 'Save' icon. This saves the grid as a file, the default filename is '1'. If you want to see the files click on the iconbar menu option 'Show files'. They to be found inside the application in a directory named 'Grids', you can rename the files you see. There is a limit of 100 files that can be saved in the Grids directory.

To load a file, click on the 'Load' icon and select the file you want from the directory of saved grids. Click on 'Solve' to see most of your puzzles solved in less than a second.

Not all grids are solved first time, try clicking on 'Solve' again. When an impasse is reached and no further progress is made this is because application of the rules of Sudoku are insufficient and intelligent guesses have to be made. To aid this process, click with the menu button on the unsolved cells, the possible numbers for this cell will be shown. Obviously it is better to make your guess from a cell with just 2 possibilities so look carefully. You may have to repeat this process. The worst case I have come across is the puzzle in the October Archive gleaned from the Guardian. I found 3 solutions to the Telegraph of 24th September, there may be more. I got no thanks for pointing this out nor did I win the prize!

Of course, I may not know all the tricks to solve a Sudoku puzzle, if you find a puzzle that can't be solved by the program but can be solved by you (without guessing), please send me the puzzle and your solution. I will try to implement your method and give credit.

If the program can solve a puzzle you can't then a study of the report produced after the last click on the solve button may help (File 'Report' within the application directory), to read it click the 'View report' option on the iconbar menu. As well as the row, column and number involved a short explanation of how the program solved this cell is given. The remarks are :
1. Only 1 in row : The possibilities in a row have been searched and only this cell contained that number
2. Only 1 in column : Same as above for a column
3. Only 1 in mini-grid : Same as above but for mini-grid
4. Row clearance : When a cell is solved, the program clears the possibility for that number from other cells in the row, when one cell is reduced to only one possibility it is solved.
5. Column clearance : Same as above for a column
6. Mini-grid clearance : Same as above for mini-grid
7. Pair nm in mini-grid : Two pairs of the same numbers (n and m) found in mini-grid, leading to clearance of those numbers from other cells in the mini-grid.
8. Pair nm in row : same as above for a row
9. Pair nm in column : same as above for a column
10. Triples nmo in mini-grid :3 triples or 2 triples and a pair with same numbers in a mini-grid leading to clearance in the mini-grid
11. Full row in a mini-grid : When a row is filled in a mini-grid this has implications for the possibilities of other numbers in rows in other mini-grids
12. Full column in a mini-grid : Same as above for a column
13. n can't be in this mini-column : Number n has to be in this column in another mini-grid
14. n can't be in this mini-row : Number n has to be in this row in another mini-grid
15. Must be in this mini-row : 3 numbers only to go in this row eliminates possibilities for other numbers
16. Must be in this mini-column : same as above for a column
The report is only a guide as it only gives the last operation to reduce the possibilities to 1 for that cell, a more crucial deletion may have occurred earlier.
17. n has to be in other row : n occurs as a possibility in only one mini-grid along a line, hence the other rows in this mini-grid can't contain n.
18. n has to be in other column : similar to above.

HELP!
=====
Methods 8, 9 and 10 above were found in the excellent book by Carol Vorderman, if you are a Sudoku addict and want to learn more I can recommend you buy it. Strangely though it doesn't mention those annoying puzzles which can only be solved by guessing, the implication being that all puzzles in the book can be solved by logic alone. This is were I needed some help. Puzzle CV200 couldn't be solved by the program or by me. Jeanne Scyra managed to find a logical way to solve this puzzle and the method has now been added to the program. Jeanne also found one that had several solutions which meant that guesswork was involved. Jeanne has also pointed out how to solve yet another puzzle (CV180), leading to 2 more methods. If you find any other grids that the program can't manage please send details of how you cracked it. I can then attempt to add that method to the program's repertoire.

Printing Grids
==============
An option to 'Save as Draw' appears on the iconbar menu. If you use this without making any selections first (the 'Select Grids' option on the menu) then a drawfile containing 6 blank(*) grids is produced ready for printing. You can use this after making a mess of a puzzle to start again or to keep your book of puzzles in pristine condition.

(*) Well not entirely blank, you will see tiny numbers 1 to 9 in each cell which can be crossed out when you are eliminating possible numbers to go in that cell. This maker a neater and hopefully more efficient method when you are solving difficult puzzles.

Up to 6 puzzles can be selected for printing using the 'Select Grids' option on the iconbar menu.

Have fun and remember that if you win any magazine prizes with the help of this program it would be nice to share your winnings with the author. Any ideas for more bells and whistles will be considered.

Creating Grids
==============
An option on the iconbar menu 'Create grid' should be selected. The Sudoku window will be cleared and you will see numbers going into the cells until it is full. The grid clears again before a test solve is made, if all is well around 26-30 numbers appear. This is the puzzle for you to solve. It is better first to save this puzzle before attempting to solve it in case of mistakes later. I'm afraid that as numbers are chosen almost at random (although you will notice a mirror image pattern of placement) the level of difficulty can range from easy to super difficult. They all should be solvable by logic alone with no need for guessing. If all was not well on testing another puzzle is generated until one solvable by the program is produced, this will not happen very often.

Derek Baron
derek.baron@tiscali.co.uk

No charge is made for the program but copyright is retained by the author.
Requests for additions, reporting of 'features' will be acknowledged and hopefully dealt with.

Thanks to Jeremy Nicoll for pointing out some unwanted features in the first version.

A very big thank you to Jeanne Scyra for pointing out the obvious method of solving grid CV200, which was the last one to give difficulty. This has led to 2 more report messages (15 and 16). Further correspondence has led to the solving of CV180 with 2 more messages added(*), thanks Jeanne.

(*) actually these messages don't appear in the report for CV180 as although it led to a reduction of possible numbers, no cell was reduced to 1 possibility at that stage.

How the program works
=====================

2 arrays are used, 1 for solved cells and the other for possible numbers in each cell. The solved array is zeroed and the possible array is first filled with all the numbers 1 to 9 for each cell, treated as ASCII characters.
On clicking 'Solve' the program examines each cell in turn, if a number has been input or loaded then this is put in the solved array and the possible array for that cell is zeroed (If there are 2 or more numbers these are ignored). The possible arrays in that row and column are then cleared of that number as is the relevant block of 3x3 cells. If a cell is reduced to just 1 possible number that is put in the solved array and by iteration the process of clearing is repeated.
The program then checks that of all possible numbers in a row or column only 1 cell has a particular number as a possibility then that cell is solved and the clearing process initiated, a similar process is carried out for each 3x3 block. More checks are then carried out if a full mini-line is solved or the possibilities for a particular number are only found in one mini-line within a mini-grid.
The whole process is then repeated up to twice more or until each cell is solved.
As mentioned above sometimes more passes are needed, clicking the 'Solve' icon gives 3 passes each time. This still leaves some puzzles unsolved as they would be if pencil and paper had been used. Try saving and making guesses.
