SUDOKU NOTES - 20MAR06

TO WORD WRAP THIS TEXT:

Click Edit on this screen's above toolbar and then click on Word Wrap.

Introduction

This Sudoku PC program was written for the Author's own entertainment and is not supplied as essential part of the EPE PIC Sudoko design of July 2006, but it is hoped that you may also find it to be of interest. It is supplied "as is" without any guarantees.

This text is in two main parts, notes regarding the PC generation and playing of Sudoku puzzles, and a description of the PC program which solves most published Sudoku puzzles with which it has been used by the Author. It is not claimed that it will solve all Sudoku puzzles.

The generation of Sudoku puzzles aspect was written prior to designing the published EPE PIC Sudoku unit to prove the technique required. The software for the PIC unit is based on this PC program, although there are differences.

Puzzle Loading

There are two ways to load this program with a puzzle, either via the internal generator, or by inputting them from a text file which has been created by the user.

Each time the program is run it defaults to the internal puzzle generator, which gets it data through a random number generator. There are then eight patterns of blank spaces, any one of which it selects at random when the Random Blanks button is clicked to reveal a tick mark. When there is no tick shown, the same blanks pattern is always used, but the numbers themselves are always selected at random.

The resulting pattern of numbers and blanks is then displayed in the lefthand set of 81 boxes. The answers to that puzzle can be showed in the righthand set of 81 boxes by clicking the Show Answers button. They can be hidden again by clicking Hide Extras.

Puzzle Playing

To play a selected puzzle, click on the blank box to which you want to add a number to the pattern, then use your keyboard to enter the number you want. Repeat the process until all blanks are filled, according to the normal simple rules as shown on screen when the Rules button is clicked. Any chosen number can be changed in the same way if you wish. Keying a "0" entirely clears that box of a value. Only numbers you have chosen (in black) can be changed. Those coloured red can never be changed.

At any time during the game you may click Check Results to show which of your choices are correct (providing answers have been included with the same text file). Correct choices are shown with a green background. Red indicates an incorrect choice. Remaining blanks are highlighted in blue. Click Clear Colours to remove the coloured highlights.

If all boxes are filled correctly, you will be told so when clicking Check Results.

A new game may be started at any time by clicking Set New Game, or clicking Random Blanks. Clicking the latter always alternates between the tick being on or off.

You may always click the Key Press ? button to reveal a tick mark, when all subsequent data entries to the main 81 boxes are automatically checked for correctness (again, providing answers have been supplied). Click the button again to clear the tick and checking facility. It is an alternating cycle.

Option Choices

Clicking Show Choices causes the righthand set of 81 boxes to show the options which are currently available to be used in any main box. You may also key in your own notes into any of the righthand boxes, indicating to yourself which are the options you think might apply. There is a limit of five consecutive digits which can be entered into the RHS boxes. These notes are over written when the Show Answers, Show Choices, Show Box Nos or Hide Extras buttons are clicked. 

The Show Box Nos button probably has little practical benefit, but shows in the RHS boxes the numbered order of the LHS boxes, from 1 to 81, top left to bottom right.

There are two main choices of background colour you may choose to use for the boxes, light blue or white. Click Box Colour to alternate between them. You may prefer light blue when working on a puzzle. White may be chosen should you wish to do a "screen dump" of the whole screen, pasting it into the Windows Paint facility prior to printing out the image to paper via a printer.

Screen Dump

To use the screen dump facility, jointly press the keyboard Alt and PRNT SCRN keys to place the current image onto Windows' Clipboard. Then click the Windows Start button, select Programs, Accessories and then Paint. Once in Paint, click Edit and then Paste to paste the image into Paint's screen. The image may be saved to disk via the File/Save As option. The image may be edited in Paint to extract the area which you want, then click Image/Clear Image and then Edit/Paste to repaste the selected image on screen. Choose Paint's Help button for info on how to extract an area (via the dotted box in its toolbars).

External Puzzles

Externally generated puzzles can be loaded and worked on via the DIR button. This reveals a Directory screen through which puzzles files can be loaded. There is a selection of puzzles provided with this program, those which the Author used when developing it. Click the Directory screen's Notes button for more info on using it.

You may key in your own puzzles, say from the details published in newspapers etc, and then load that file into the program. Examine one of the existing files for the layout. Basically the format is for the first nine lines to hold the puzzle, with a space (" ") indicating each blank. Each line must be nine digits long. If you already have the answers, they can be entered as the next nine lines, again nine digits per line. If you don't have the answers, leave these lines blank (don't enter any data).

To enter puzzle detail, either do so via Windows Notepad through the Windows normal selection option, or go into Sudoku's Directory screen via DIR. At the bottom of the Directory screen is an oblong box through which you can create a blank file. Type in the name you wish to use for this file, then click the screen's Make File button. This creates a file of that name, confirms that it's been created (or telling you that name already exists - it cannot overwrite an existing file). Then it opens the new file in Notepad so that you can enter your data. Do so. Save the file in the usual Notepad way when finished.

Then click DIR on the Sudoku screen again and select that file to load it into Sudoku.

Each file when loaded is automatically checked for whether or not answers are provide. It they are they are checked to see that they conform to the standard Sudoku rules. If they don't, and you will be told this, re-examine the file through the Edit Puzzle button and amend any errors (duplicated/missing numbers). Resave it and reload.

The file name of the selected program is displayed near the bottom right of the screen. If it is too long to be fully visible, left-click on it to display a Message box with the full details,

Automatic Puzzle Solving

The other main aspect of the Sudoku PC program is that of attempting to automatically solve a Sudoku Puzzle. The puzzle may be that automatically generated internally by the program, or an external one selected via the DIR button.

With the puzzle you wish to have solved displayed on screen, click the Solve It button. The program uses logic to fill in the blanks on your behalf. It is not claimed that all Sudoku puzzles can be solved in this way, but most of the ones with which it has been used by the Author have been solved automatically.

It is worth noting, though, that it has been found that some more difficult puzzles have more than one perfectly legitimate solution, all of which conform to the basic Sudoku rules, but which do not necessarily match the published answers.

This is particularly so with puzzles in which at some stage there is a choice of numbers which could be placed in a box and there is no logical indication of which is the correct choice of two numbers, or of the correct route which should be taken following that choice. With some Sudokus encountered, several points have occurred at which such a choice must be made, and then later on another choice of options is presented at which further decisions must be made, and sometimes yet more decision points further on.

With one puzzle there were at least four levels of options at which a choice was needed. In-depth analysis by the Author failed to reveal any way in which the choices could be logically made. In all respects the grid usually could still be filled completely, but the results failed to correspond with the published answers, although such answers had been reached by the program in its various permutations of the options.

It is worth commenting, though, that most Sudoku puzzles tried could be solved in one pass of the program, without any dual choices being encountered.

Permutations

The program has been written so that when choices are presented, it goes into a cycle in which all permutations of dual choices are performed, with the results put out to a file on the hard drive for later examination by the user.

During this process, each set of options is examined to see if it would contravene the logical rules of Sudoku. If they do they are ignored. There are two conditions which are recorded to the file, those which result in the full grid being filled, and those in which the result is an equal number blanks remaining compared to the number of unused further options which are possible.

The file in which these results are recorded is shown by clicking the Show Records button, which loads the file into Notepad for viewing. Sometimes the file may be too long for Notepad, in which case the choice of opening it through Wordpad is presented. If so, accept the offering. The file shown is always the last one created when permutations are encountered. The file name is given at the top of the first page.

Only where there is a choice of one or the other of two values is the choice recorded and processed. Choices having multiple options greater than two are ignored as these will ultimately reduce as the program progresses. The program assesses how many permutations there are to process all the dual choices available. If there are more than 16 you are asked if you want to continue or abort at this point. Processing too many permutations would taken too long for slower PCs, although with one of the Author's faster PCs several complete permutations of choices occur per second.

With one puzzle there were first 128 permutations to begin with, later resulting in a choice of 2048 permutations offered. Another puzzle resulted in 524,288 permutations being offered (2 to the power of 19). As it turned out, there was only one result of that vast series which was worth pursuing further by manual examination - and then it was the second choice of the dual pair pointed to which was correct!

On another occasion, the indication of filled and unused options was equal at a Perm value of 47, yet analysis of the published answer revealed that a Perm value of 43 was the correct route, but resulting in further choices having to be made along that route.

Grid Copies

Where permutations are encountered you are asked if you would like to make a copy of the grid as it now stands before the permutations. This is created as a separate file with an extension number relevant to the base file name and which can be loaded later to try out and examine the results of suggested possibilities of choice. Normally, the same recording file name is used on each occasion, overwriting the data from a previous run. If you want to keep a particular file's details make a copy of that file and rename it as you wish.

Having selected a choice from those permutations listed, enter that permutation number into the Perm Value text box and press <Enter> on the keyboard. This reloads the file and applies just that permutation to the choices. The binary value of the permutation's decimal number is used, in which each bit applies to a particular choice pair, 0 selecting the lefthand value, 1 selecting the right. During the processing of multiple options, the Perm value is incremented by one for each permutation, to the full quantity of the permutations available (always 2 to the power of the number of pairs found).

Step Mode

When using the auto-solve facility, a step-mode can be entered so that each stage of solving can be examined. Having loaded a required puzzle file, click Start Step Mode, to reveal a Step button. The program pauses each time a process results in a box being filled, highlighting the background colour of that box. Having examined the result, click Step again to move on to the next stage.

So, have fun with this program. It kept the Author engrossed over about three month's writing it. Excellent value and entertainment!

John Becker 20 March 2006

ADDING YOUR OWN NOTES

These notes are displayed via an "Active" text editor and you may add your own notes to them, saving them to disk in the usual way.
