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.
- Trivial means only phase 0 used.
- Easy means only phases 0 and 1 used.
- Moderate means phases 0,1 and 2 used.
- Hard means phases 0 through 3 are used.
- Diabolical means that in at least one case a choice had to be made
(and possibly retracted and another choice made)
The reason for these choices are as follows.
-
Trivial - look at numbers fitting only one cell.
-
Easy - look at cells in a (row) where a number fits.
-
Moderate - need to look at two cells.
-
Hard - the rules here don't say which numbers go in cells,
but only eliminate numbers from cells.
-
Diabolical - having to make a random choice may cause backtracking.
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.
- Phase 0 of solution. Check each cell and determine if there is a
cell which permits only one symbol. This can occur as illustrated
in the following diagram of a 3x3 puzzle. A _ indicates a blank.
__3
2__
___
Of the three symbols 1,2,3, simply concentrating on the top left
cell indicates that neither 2 can occur (it is already in the
first column), nor a 3 can occur (it is already in the first row).
The flags for the top left cell indicate that only the symbol 1 can
be placed there.
If such a cell is found, progress to the recursive step (below).
- Phase 1 of the solution. In this phase we consider symbols. We examine
rows, then columns, then blocks for the following type of situation.
___
_1_
__1
Looking at the first row, we can see that the symbol 1 cannot occur
in either of the second or third cells (as it already occurs in
columns 2 and 3). Hence it must occur in cell 1.
If this situation is found, we proceed to the recursive stage.
- Phase 2 of the solution. In this phase we now need to look at pairs
of cells. For simplicity we look at a small example which is best
shown in a puzzle where sub-blocks apply. We look for two cells
each of which can contain only the came two symbols.
____
___2
__21
__1_
In the first row, the flags for the third or fourth cells will indicate
that symbols 3 and 4 (only) can occur. In this case this is because
the symbols 1,2 already occur in the third and fourth columns.
So cells 3 and 4 both can only contain the two symbols 3,4. This means
we can adjust the flags for cells 1 and 2 to exclude symbols 3 and 4.
If we adjust any flag we can go back and re-start phase 0
- Phase 3 of the solution. This parallels phase 1, in that we look
at the numbers that can occur in entire rows (ditto columns, then blocks.
If in a row we discover that some two symbols can ooccur in only 2 cells,
(even if on first sight the flags may indicate other symbols can occur also
in those cells), the symbols must occur in those cells. So we adjust the flags
for the row to permit only those two symbols in the two places and bar the
symbols from the other places. If we adjust any flag we can go back and
re-start phase 0.
- Phase3b of the solution
In this phase we use particularly the relation between
blocks and rows/columns. Consider the puzzle
____
__3_
__12
____
In the bottom right corner subblock it is clear that 3 can only go in the bottom
right cell. This means that 3 cannot go in the bottom row of the lower left cell.
(This illustrates the interplay between rows and columns. Although in this small
example phase 0 would apply, in larger puzzles a number may only fit in a particular
row of a subblock, and so be eliminated from that row in other subblocks.)
Note.Phases 2 and 3 can be extended to considering 3 cells, 4 cells and so on but
with probably diminishing return.
- Recursive step.
Choose the first cell found with the least number of possible symbols. Many times
(indeed every time, except for "Diabolical" puzzles) there will be a cell with only
one symbol.
For the chosen cell, for every possible symbol in that cell
place the symbol in the cell
copy the array of flags
adjust the flags to ban the symbol in that cell's row, column, block.
Call solve recursively with the current puzzle state and the new flags.
(Initially solve is called with the original puzzle and the original flags
set when the puzzle is validated.)
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