上一個主題

撰寫 Python 程式來解數獨了

本頁

程式詳解: sudoku套件

這是使用物件導向程式設計來求解數獨的程式庫

class sudoku.sudoku.Box(idx, p)[source]

Box

get_group_number(num, pos=, []notInLineX=None, notInLineY=None)[source]

如果在一個區塊群組中所有尚未被給填上的代碼, num, 其可能填上此 num 的 Point 物件有同樣的 x 軸或 y 軸座標時, 我們稱為 Group Numer。

class sudoku.sudoku.Chain(numList, posList)[source]

一個鏈結(chain)是由同個群組中2個以上的空白點組合起來,在這些點上的可被填上的代碼總數剛好等於這些點數,所以在同個群組中的其他空白點就不可能被填上這個鏈結上的所有代碼。

class sudoku.sudoku.GroupNumber(b, num, p, direction, idx)[source]

在區塊群組中的 Group Number

class sudoku.sudoku.LineX(idx, p)[source]

x 軸線群組

class sudoku.sudoku.LineY(idx, p)[source]

y 軸線群組

class sudoku.sudoku.Matrix(file='')[source]

數獨遊戲的整體物件

allow(x, y, v)[source]

檢查座標為 (x, y) 的點(Point)物件能否填入數字 v

can_see(p0, method='u', num=0)[source]

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

get_all_pos(diff=, []method='a', num=0, chain=None, possibles=None)[source]

get all postion

print_rec()[source]

列印所有的求解過程

read(file)[source]

讀取數獨遊戲的定義檔

reduce(x, y, v, d='set', check=False, info='')[source]

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
setit(x, y, v, d='define', info='')[source]

set the position x, y to be the number v return: >=1 if set successfully, 0 if it can’t be set the number v

sort_unassigned_pos_by_possibles(possibles=0)[source]

Get unassign position’s possible number list, format is [p1, p2,...] and Sorted By the possible numbers possibles: 0 for all, >=2, mean get only the possible numbers for it

class sudoku.sudoku.Number(v)[source]

數字物件

can_see_by_group_number(p1)[source]

Check if the position, p1, can be seen of all this number’s group number” return: gn if can be seen by it, or None

setit(p1)[source]

儲存所有非空白的點到 p 這個列表

class sudoku.sudoku.Point(x, y)[source]

在數獨表中的一個點

can_see(p1)[source]

this position can see p1? the value can’t be 3 or 7 it means the same pos rtn: 0: can’t see p1

1: can see it in x line 2: can see it in y line 4: can see it in the box
can_see_those(posList)[source]

check this position can see which positions in the posList, a [(x, y),...] list

class sudoku.sudoku.SolveMethod(fun, idx, name='', level=0, obvious=True)[source]

求解法物件

exception sudoku.sudoku.SudokuDone(x, y, v)[source]

一個例外處理,當數獨遊戲中每個點都被填滿時產生

exception sudoku.sudoku.SudokuError(x, y, v, t)[source]

一個例外處理,當一個點無法被設定或減掉某個數字(v)時而產生, 其中 type 為 s 表示為設定動作, r 表示為減掉動作

exception sudoku.sudoku.SudokuStop[source]

一個例外處理, 當求解紀錄達到 recLimit 時 (除錯時用)

exception sudoku.sudoku.SudokuWhenPosSet(x, y, v)[source]

一個例外處理, 當一個座標列表, [(x1, y1), ...], checkPos 有設定時, 這些點中如有被設定時, 就會引發此事件的處理

sudoku.sudoku.check_inobvious_number(m, first=1, only=False)[source]

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

sudoku.sudoku.check_line_last_possible_for_number(m, first=1, only=False)[source]

檢查每一個 x 軸或 y 軸線群組,是否只剩一個點能夠填上某個數字

sudoku.sudoku.check_obvious_number(m, first=1, only=False)[source]

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

sudoku.sudoku.compare_result(m, emu, result)[source]

比較所有的結果列表,比較每個列表從原本最後一筆紀錄之後的行動紀錄,如果有相同者,則表示每一種狀況都會導致這個結果,那此行動就應該是符合邏輯的,可以照做無誤

sudoku.sudoku.emulator(m, x, y, v, targets=, []checkval=0)[source]

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

sudoku.sudoku.fill_last_position_by_setting(m, sets)[source]

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

sudoku.sudoku.fill_last_position_of_group(m, first=1, only=False)[source]

假如在一群組(線或區塊)中只剩一個空白點時

sudoku.sudoku.fill_only_one_possible(m, first=1, only=False)[source]

檢查每一個空白點,看是否在可能的數字列表中只剩一個。

Args:
m: Matrix Object first (int): the first number of checking only (bool): just check the first number or not
Returns:
in the tuple format (sets, reduces, method Index to restart using, first, only)
sudoku.sudoku.guess(m, idx=0, first=0, only=False)[source]

Guess Method

sudoku.sudoku.reduce_by_emulate_possible_in_one_position(m, first=1, only=False)[source]

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.

sudoku.sudoku.reduce_by_emulate_possible_number_in_group(m, first=1, only=False)[source]

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.

sudoku.sudoku.reduce_by_group_number(m, first=1, only=False)[source]

以 Group Number 來消減其他點的可能數

sudoku.sudoku.reduce_by_two_possible_in_one_position(m, first=1, only=False)[source]

當一個點(p1)只有兩個能數時,我們可以先假設這個點其中一數(設為first),然後將此點設為另外一數(設為second)來模擬f求解,看這求解過程中,first這個數落在哪些點上,那對所落的點與p1有所交集(兩個點都能看見)的空白點而言,都不可能存在填入first這個數的可能,而可以讓這些點將這個可能數消除。

sudoku.sudoku.reg_method()[source]

註冊所有求解法為一個個物件並儲存成一個列表送回給呼叫者

sudoku.sudoku.set_obvious_method_for_pos(m, method1, p1, v)[source]

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

sudoku.sudoku.solve(file, loop_limit=0, rec_limit=0, check=None, level_limit=0, emu_limits=2, use_try=True, use_emu=False)[source]

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

sudoku.sudoku.try_error(m=None, file='', depth=0)[source]

Try Error Method, only fill the first possible postion

sudoku.sudoku.update_chain(m, first=1, only=False)[source]

檢查與更新 Matrix 物件(m)的鏈結狀況與資訊,並取得鏈節總數(>= 0)。

sudoku.sudoku.update_group_number(m, num)[source]

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

sudoku.sudoku.update_indirect_group_number(m, num, amt=0, start=0, first=1, only=False)[source]

Update in-direct Group Number, formed by the assigned number and groupnumber already known, a recursive function

sudoku.sudoku.write_down_possible(m, first=1, only=False)[source]

如果常數 WRITEN_POSSIBLE_LIMITS 設定為 1..9 時,則表示要像人一樣將每個點的可能數 <= WRITEN_POSSIBLE_LIMITS 都寫在點上,讓人可以看出這些數的關聯而繼續求解