Introduction

The program loads, solves, generates and saves Sudoku puzzles of sizes up to 30x30 (this restriction is due to the use of 32 bit integers - the java default size - for flags). After solving, the original puzzle may be restored.

A 9x9 Sudoku puzzle is a 9x9 array of cells, which is further divided as a 3x3 array of blocks of 3x3 cells. In a 9x9 Sudoku puzzle the digits 1 to 9 are the symbols and each symbol must occur exactly once in each row, each column and each sub-block. Some of the cells are already given, and the problem is to find the unique solution satisfying the constraints.

Sudoku puzzles can be of any size - another common size is 16x16 divided into 4x4 sub-blocks. However unless the size is a perfect square, the sub-block rule cannot apply and the puzzle is less attractive.

Normally a 9x9 Sudoku puzzle uses sub-blocks, and in addition the blanks are arranged so the puzzle has some symmetry. This can be half-turn symmetry or both vertical and horizontal symmetry.

File Format

The file format is as follows.

Line 1 --- contains the puzzle size. A negative size indicates that sub-blocks are not to be used (in the case of perfect square sizes) A size of 1 or greater than 30 is not permitted. So the only permitted sizes are -25, -16, -9, -4 and 2 to 30. Line 2 --- the list of symbols used in the puzzle prefixed by the symbol used in the file to denote a blank Remaining lines contain the puzzle. For example the file
4
_1234
1_3_
23_1
3__2
_1_3
would denote the 4x4 puzzle
1?3?
23?1
3??2
?1?3
where I have replaced blanks by questionmarks. Note that the final line must be terminated by an end-of-line.

Loading and Saving files

When loading a file, the program verifies that no number occurs more than once in any row or column (or sub-block, if applicable), and set the flags indicated under the "Solution Technique" section. See the README file for information on Fedora 4 compatablity.

Preferences

The user-defined preferences (used for generating puzzles) are:-
size
sub-blocks
If sub-blocks are to be used (only valid when size is a perfect square)
Symbol set
Symmetry
Symmetry can be none, half-turn, quarter-turn or vert/horiz (vertical and horizontal).
Symmetry affects the difficulty level of puzzles generated to some degree. For 9x9 puzzles mostly trivial, easy puzzles come from quarter-turn symmetry, but for 25x25 puzzles quarter-turn symmetry often generates hard, diabolical puzzles.

Note:- The difficulty part of the Preferences Dialog has been commented out, as the time taken to generate is highly variable, and there is no code in the generation section to use the difficulty preference setting.

Note also that when the preferences are being edited, if the "Enter" key is pressed when the size field is being edited, the symbols will automatically be set to the required number of symbols from the string "1234567890ABCDEFGHiJKLMNoP".

Difficulty

The difficulty of a puzzle is determined by the level of rule (described below) in solving the puzzle. The reason for these choices are as follows.

Solution Explanation

Note that after the difficulty button has been pressed, its label changes to Explain. Pressing this brings up a frame showing how the puzzle was solved.

Solving

When solving, the program assumes a single solution exists, and displays the first solution found. If no solution exists, it will display a partial solution (as much of a solution as it could find).

Solution Technique

The solution method is recursive, using an array of flags which indicate the symbols valid in each cell. Each cell has a flag (a 0 bit) indicating which symbols are possible. A 1 bit indicates that the corresponding symbol already occurs in the same row or column (or sub-block), or is not allowable for any of the additional rules below.

Validating

When a puzzle is loaded, the puzzle is automatically validated to set the flags for each cell - this also helps to check that no symbol has been placed twice in any row, column or block.

Generating Puzzles

A default layout with all symbols is generated - for example in a 9x9 puzzle the lines 123456789, 234567891, 345678912, etc would be laid out in the suitable order to satisfy the block condition.
The symbol set is then permuted randomly, then rows and columns are permuted randomly so the generated solved puzzle is random.
Then symbols are removed (keeping vertical and horizontal symmetry) until the puzzle has more than one solution when the last set of removed symbols is restored.
If only half-turn or no symmetry is required, more symbols are removed until no further removal is possible.

Notes

Although to determine the difficulty of a puzzle the puzzle must actually be solved, the solution will not be displayed until the solve button is pushed. Generating puzzles can take a long time, especially for hard or diabolical puzzles with symmetry. See also the README file