This is the oop method to solve the Sudoku.
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
A Table of a Sudoku
get the possition list which can see the position, p method: “u”: un-assigned positions, “a”: all, “s”: assigned positions num: if method=”u”, the position must have be possible to be filled the number
reduce the position(x, y)’s possible numbers from v Return:
int, as following
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
Number Object
A Position in a Sudoku’s table
An exception When the table has been filled 81 positions
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
An exception When the position, checkPos, has been set, and program want to setit
Check every number which has been assigned and known as group-number and its effect’s boxes’ does not have assigned that number” Only: False, check all numbers
True, check the first number only
first: the first number to be checked
Check every line that only have one position for un-assigned number
Check every number which has been assigned and its effect’s boxes’ does not have assigned that number Only: False, check all numbers
True, check the first number only
first: the first number to be checked
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
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
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
If the un-assigned positions in a group(line or box) are only one left
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.
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.
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.
Reduce the possible number in a posiition by GroupNumber
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
register all method as an object and save them into a list to return
Check is there an more obvious method for the position, p1 than method1 Obvious methods include fillLastPostionOfGroup=0 and checkObviousNumber=1 return: True: set, False: not set
Solve a sudoku which define in a file! loopLimit: the limit for the method loops, 0: no limits recLimit: when the records >= recLimit, it will stop, 0: no limits
Try Error Method, only fill the first possible postion
Update the chain of line return: >=0 means the chain number’s amount in the matrix, m
Update the group number, num, in a box, and store those group number in m.n.group list return: >=0 means the group number’s amount in the matrix, m