This is the oop method to solve the Sudoku.
A 3x3 Box Object
Parameters: |
|
---|
Note
This will replace the original GroupBase’s init()
if the unassigned num in this box which it’s all possible positions have the same direction, we call it as a GroupNumber
Parameters: |
|
---|---|
Returns: | GroupNumber, none or a GroupNumber Object |
A chain of two and above positions which are not in the same box but in the same line and can form a chain, means the possible number in this chain positions only can be filled in these positions
Parameters: |
|
---|
The Base Class of a Line or Box
The init function of a GroupBase object
Parameters: |
|
---|
check the group can be filled the number(v)?
Parameters: | v – int, the number to be checked |
---|---|
Returns: | True or False |
get the un-assigned position in this group, and possible numbers only in [count] positions
Note
The output format is a tuple list, the tuple has two value, one is number, another is s position list
Parameters: | count – int, how many positions are un-assigned to get |
---|---|
Returns: | list, [(num,[p1, p2...]),...] |
get position list in this group
Parameters: |
|
---|---|
Returns: | list: a List of Position |
Group Number in Box
Parameters: |
|
---|
Line of X
The init function of a GroupBase object
Parameters: |
|
---|
Line of Y
The init function of a GroupBase object
Parameters: |
|
---|
A Table of a Sudoku
Parameters: | file – file, the define file of a sudoku |
---|
Properties:
rec: list of (x, y, v, t, d), the record of all the solving steps
filled: int, the amount of assigned points
done: bool, if solved or not
error: bool, if there is an error occurs
lineX: LineX list
lineY: LineY list
b: Box List
p: a two dimensions of list of Point
n: Number list
chain: Chain list
Checking if the position x, y, can be set the value v
Parameters: |
|
---|---|
Returns: | True or False |
get the position list which can see the position, p
Parameters: |
|
---|---|
Returns: | Point list |
get all positions, to call the :meth:GroupBase.get_all_pos function to get it
Parameters: |
|
---|---|
Returns: | Point list |
Read Sudoku’s Define from file
Parameters: | file – file, the sudoku define file, can give it the absolute path name, if only the file name, it would find file in the data path. |
---|
reduce the position(x, y)’s possible numbers from v
Parameters: |
|
---|---|
Returns: | int, as following code |
2 -- if set a number,
1 -- if just set number
0 -- if is not in the possible set, if check is True, it will raise an SudokuError exception
set the position x, y to be the number v
Parameters: |
|
---|---|
Returns: | int, >=1 if set successfully, 0 if it can’t be set the number v |
Number Object
Note
You can imagine that an object of this class is equal to a country, every country has its own id from 1-9.
Parameters: | v – int, the id of the Number, from 1 to 9 |
---|
A Position in a Sudoku’s table
Note
We can imagine that a Point object is an house
This is the init of Point Object
Parameters: |
|
---|
Method Object
Parameters: |
|
---|
To execute a method to solve a sudoku
Parameters: |
|
---|---|
Returns: | tuple, (sets, reduces, method index, first, only), as following |
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
To Store running condition
methodLoopIdx: int, the index of the methods loops
methodIdx: int, the method idx which run now
checkPos: Point list, set this variable, if you want to make an exception when this position has been set
writeDownAlready: bool, if the every un-assigned positions have been writen down their possible number?
emulatePossibles: int, to set the possibles when using emulate method, for both positions and numbers
tryStack: list of (m, x, y, idx)
m: Matrix object, the Matrix copy before try to set (x, y) to be the idx of possible numbers
x: int, the position x of trying
y: int, the position y of trying
idx: int, the index of the possible numbers, starting from 0
tryIdx: int, the depth index of trying
tryUse: bool, if using Guess method or not
emuUse: bool, if using emulator methods or not
Scope: int, the difficult level of this sudoku
Level: int, the level Limit, 0 means no limit
Original: Matrix, the init Matrix
Result: Matrix, the now Matrix
printSteop: bool, if print the steps of solving
nowPath: str, record now path
An exception When the table has been filled 81 positions
Parameters: |
|
---|
An exception when x, y can’t be set or reduce to or form the number v t: is the type: ‘s’ means set, ‘r’ means reduce
Parameters: |
|
---|
An exception When the position, checkPos, has been set, and program want to setit
Parameters: |
|
---|
Check every number which has been assigned and known as group-number and its effect’s boxes’ does not have assigned that number”
Parameters: |
|
---|---|
Returns: | tuple, (sets, reduces, method index, first, only), as following |
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
Check every line that only have one position for un-assigned number
Parameters: | m – Matrix object |
---|---|
Returns: | tuple, (sets, reduces, method index, first, only), as following |
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
Check every number which has been assigned and its effect’s boxes’ does not have assigned that number
Parameters: |
|
---|---|
Returns: | tuple, (sets, reduces, method index, first, only), as following |
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
compare the result list, to check if there are same result in every step after the last record of original m.rec if same for all emulate result, it means that it must be true, so can do by it
Parameters: |
|
---|---|
Returns: | tuple, (sets, reduces), sets: the amount of setting, reduces: the amount of reducing |
emulate the x, y to be set v, then start to use some basic methods to try to solve it will stop when and return:
1: one of the targets have been set the checkval
2: isDone
-1: error is True
0: all basic methods have been tested, and can't solve
and the result matrix
Parameters: |
|
---|---|
Returns: | tuple, (rtn, matrix, idx) |
rtn: 1: one of the targets have been set the checkval
2: isDone
-1: error is True
0: all basic methods have been tested, and can't solve
matrix: Matrix, the reulst of emulating
idx: int, if the rtn == 1, this will indicate the index of the target that has been assigned the number, checkval
When setting a number, may cause 1-3 groups left only one possible position check if a group have only position left, just set it.
Parameters: |
|
---|---|
Returns: | tuple, (sets, reduces, method index, first, only), as following |
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
If the un-assigned positions in a group(line or box) are only one left
Parameters: | m – Matrix Object |
---|---|
Returns: | tuple, (sets, reduces, method index, first, only), as following |
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
Check every unassigned position, if it’s possible numbers left one only WRITEN_POSSIBLE_LIMIT: True, Check position’s writen is True or note False, don’t check.
Parameters: |
|
---|---|
Returns: | tuple, (sets, reduces, method index, first, only), as following |
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
get the possible positions(pos) numbers chain
Parameters: |
|
---|---|
Returns: | Chain list |
Guess every possible number in a position by from the min possibles of positions
Parameters: |
|
---|---|
Returns: | tuple, (sets, reduces, method index, first, only), as following |
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
when a position(p1) has 2 or more possible numbers, we can emulate every possible number and get its result:
1. if it causes an error, we can reduce that number,
2. if it can solve the sudoku, we can set this number,
3. if all possible number can's get condition 1 or 2, we can compare their rec, if they have the same records, we can do it.
Parameters: | m – Matrix object |
---|---|
Returns: | tuple, (sets, reduces, method index, first, only), as following |
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
when a group(lineX, lineY, Box) has 2 or more position have the same possible number, we can emulate every position to set the number and get its result:
1. if it causes an error, we can reduce the position's possible number from that number,
2. if it can solve the sudoku, we can set this number in the position,
3. if all possible position can's get condition 1 or 2, we can compare their rec, if they have the same records, we can do it.
Parameters: | m – Matrix object |
---|---|
Returns: | tuple, (sets, reduces, method index, first, only), as following |
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
Reduce the possible number in a posiition by GroupNumber
Parameters: | m – Matrix object |
---|---|
Returns: | tuple, (sets, reduces, method index, first, only), as following |
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
when a position(p1) has two possible numbers only, we can assume if the position is one number(first) then try to emulate to set the position with the other number(second), then see the first number will be filled in a position(p2) which the position can see it if so, we can reduce all these positions which can see p1 and p2 at the same time from the first number
Parameters: | m – Matrix object |
---|---|
Returns: | tuple, (sets, reduces, method index, first, only), as following |
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
register all method as an object and save them into a list to return
Returns: | SolveMethod list |
---|
Check is there an more obvious method for the position, p1 than method1 Obvious methods include fillLastPositionOfGroup=0 and checkObviousNumber=1
Parameters: |
|
---|---|
Returns: | True: set, False: not set |
Solve a sudoku which define in a file!
Parameters: |
|
---|---|
Raises: |
|
Returns: | Matrix object |
Try Error Method, only fill the first possible position
Parameters: |
|
---|---|
Returns: | bool, True if the sudoku has been solved |
Update the chain of line return: >=0 means the chain number’s amount in the matrix, m
Parameters: | m – Matrix object |
---|---|
Returns: | tuple, (sets, reduces, method index, first, only), as following |
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
Update the group number, num, in a box, and store those group number in m.n.group list
Parameters: |
|
---|---|
Returns: | tuple, (sets, reduces, method index, first, only), as following |
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
Update in-direct Group Number, formed by the assigned number and Group Number already known, a recursive function
Parameters: |
|
---|---|
Returns: | tuple, (sets, reduces, method index, first, only), as following |
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
Write down the possible numbers in every un-assigned position if WRITEN_POSSIBLE_LIMITS has set to 1..9, it will only write down the possibles which <= that limits
Parameters: | m – Matrix object |
---|---|
Returns: | tuple, (sets, reduces, method index, first, only), as following |
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not