Coding: Sudoku Code

Previous page Next page

SourGumdrop source code notes

SGP

The puzzle answers written using the symbols 1-9 and using positive values for givens and negatives for the others. Also contains the algorithm usage when the puzzle was solved using the Simplest First strategy.

SGG

Global variables.

SGHint

Handles the hint mechanism: the candidates that form a pattern - say X-wing - are "causes" and candidates that can be removed because of the pattern are "effects". Both cause and effect are stored as lists of cell_index, candidate_index pairs.

SGHistory

The history mechanism - an object stack used for recording the states of the grid while it is being worked on and for the backtracking mechanism used by the solver that uses guessing to solve puzzles that cannot be solved with the built-in algorithms. Phew!

SGCell

The data for a cell. We store the solution, the current guess, the state of each candidate (ie deleted or still available), and for convenience, the cell, row, column and box indexes (numbering from 0).

SGSudoku

An object containing a list of 81 SGCell's. This class contains all the algorithms that operate on the sudoku data.

SGWG

Here we keep our copies of all the widget variables: lists of objects that control every item in the gui - eg there are 810 (81x10) candidate buttons and we use my_little_buttons[little_button_index] to access the widget for button little_button_index.





SGHandleStatus

This class manages the status of objects. Consider a candidate button shown on the grid. It may be normal or disabled, coloured by the user, coloured as a hint cause, coloured as a hint effect, need to be redrawn, etc. Usually it can have several of these attributes at once. Writing in C I might code these states as bit patterns. For Python I assigned each possible state a different prime number, and for any object with state variable STATE, to set a state I multiply STATE by the appropriate prime number, to unset it I divide STATE.

SGWidget

This class contains the main event loop and many of the functions that handle the user interface. There is complete separation between the Sudoku variables (in SGSudoku) and the variables shown by the gui. After operations - say the application of a hint - the function sync_sg_and_sgw(self, new_sg, old_sg) is used to compare the current state of the Sudoku data (new_sg) with the previous state (old_sg). All differences found alter the status of the corresponding widget variables and each changed object is also given a "redraw" status. After sync_sg_and_sgw, refresh_widgets redraws all altered widgets accordingly and deletes their redraw status. Obviously, as widgets can have multiple states simultaneously the drawing routines need to order their operations so that the most important states are displayed - eg if a button has been coloured by the user but is also coloured by the hint mechanism, the hint colouring is shown. Once the hint has been acted upon the user colouring can be shown again.

Small classes: SGSCell, SGWidgetData, SelectPuzzle, ShowDifficulty, ConfigureSG, SGIO, EnterPuzzle.

Last updated: 2010-01-20    Sitemap