這是使用物件導向程式設計來求解數獨的程式庫
一個鏈結(chain)是由同個群組中2個以上的空白點組合起來,在這些點上的可被填上的代碼總數剛好等於這些點數,所以在同個群組中的其他空白點就不可能被填上這個鏈結上的所有代碼。
數獨遊戲的整體物件
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
數字物件
在數獨表中的一個點
一個例外處理,當一個點無法被設定或減掉某個數字(v)時而產生, 其中 type 為 s 表示為設定動作, r 表示為減掉動作
一個例外處理, 當一個座標列表, [(x1, y1), ...], checkPos 有設定時, 這些點中如有被設定時, 就會引發此事件的處理
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
檢查每一個 x 軸或 y 軸線群組,是否只剩一個點能夠填上某個數字
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
比較所有的結果列表,比較每個列表從原本最後一筆紀錄之後的行動紀錄,如果有相同者,則表示每一種狀況都會導致這個結果,那此行動就應該是符合邏輯的,可以照做無誤
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
檢查每一個空白點,看是否在可能的數字列表中只剩一個。
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.
當一個點(p1)只有兩個能數時,我們可以先假設這個點其中一數(設為first),然後將此點設為另外一數(設為second)來模擬f求解,看這求解過程中,first這個數落在哪些點上,那對所落的點與p1有所交集(兩個點都能看見)的空白點而言,都不可能存在填入first這個數的可能,而可以讓這些點將這個可能數消除。
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
檢查與更新 Matrix 物件(m)的鏈結狀況與資訊,並取得鏈節總數(>= 0)。
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