上一个主题

撰写 Python 程式来解数独了

本页

程式详解: sudoku套件

这是使用物件导向程式设计来求解数独的程式库

class sudoku.sudoku.Box(idx, p)[源代码]

Box

get_group_number(num, pos=, []notInLineX=None, notInLineY=None)[源代码]

如果在一个区块群组中所有尚未被给填上的代码, num, 其可能填上此num 的Point 物件有同样的x 轴或y 轴座标时, 我们称为Group Numer。

class sudoku.sudoku.Chain(numList, posList)[源代码]

一个链结(chain)是由同个群组中2个以上的空白点组合起来,在这些点上的可被填上的代码总数刚好等于这些点数,所以在同个群组中的其他空白点就不可能被填上这个链结上的所有代码。

class sudoku.sudoku.GroupNumber(b, num, p, direction, idx)[源代码]

在区块群组中的 Group Number

class sudoku.sudoku.LineX(idx, p)[源代码]

x 轴线群组

class sudoku.sudoku.LineY(idx, p)[源代码]

y 轴线群组

class sudoku.sudoku.Matrix(file='')[源代码]

数独游戏的整体物件

allow(x, y, v)[源代码]

检查座标为(x, y) 的点(Point)物件能否填入数字 v

can_see(p0, method='u', num=0)[源代码]

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)[源代码]

get all postion

print_rec()[源代码]

列印所有的求解过程

read(file)[源代码]

读取数独游戏的定义档

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

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='')[源代码]

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)[源代码]

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)[源代码]

数字物件

can_see_by_group_number(p1)[源代码]

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)[源代码]

储存所有非空白的点到 p 这个列表

class sudoku.sudoku.Point(x, y)[源代码]

在数独表中的一个点

can_see(p1)[源代码]

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)[源代码]

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)[源代码]

求解法物件

exception sudoku.sudoku.SudokuDone(x, y, v)[源代码]

一个例外处理,当数独游戏中每个点都被填满时产生

exception sudoku.sudoku.SudokuError(x, y, v, t)[源代码]

一个例外处理,当一个点无法被设定或减掉某个数字(v)时而产生, 其中type 为s 表示为设定动作, r 表示为减掉动作

exception sudoku.sudoku.SudokuStop[源代码]

一个例外处理, 当求解纪录达到recLimit 时(除错时用)

exception sudoku.sudoku.SudokuWhenPosSet(x, y, v)[源代码]

一个例外处理, 当一个座标列表, [(x1, y1), ...], checkPos 有设定时, 这些点中如有被设定时, 就会引发此事件的处理

sudoku.sudoku.check_inobvious_number(m, first=1, only=False)[源代码]

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)[源代码]

检查每一个x 轴或y 轴线群组,是否只剩一个点能够填上某个数字

sudoku.sudoku.check_obvious_number(m, first=1, only=False)[源代码]

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)[源代码]

比较所有的结果列表,比较每个列表从原本最后一笔纪录之后的行动纪录,如果有相同者,则表示每一种状况都会导致这个结果,那此行动就应该是符合逻辑的,可以照做无误

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

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)[源代码]

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)[源代码]

假如在一群组(线或区块)中只剩一个空白点时

sudoku.sudoku.fill_only_one_possible(m, first=1, only=False)[源代码]

检查每一个空白点,看是否在可能的数字列表中只剩一个。

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)[源代码]

Guess Method

sudoku.sudoku.reduce_by_emulate_possible_in_one_position(m, first=1, only=False)[源代码]

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)[源代码]

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)[源代码]

以 Group Number 来消减其他点的可能数

sudoku.sudoku.reduce_by_two_possible_in_one_position(m, first=1, only=False)[源代码]

当一个点(p1)只有两个能数时,我们可以先假设这个点其中一数(设为first),然后将此点设为另外一数(设为second)来模拟f求解,看这求解过程中,first这个数落在哪些点上,那对所落的点与p1有所交集(两个点都能看见)的空白点而言,都不可能存在填入first这个数的可能,而可以让这些点将这个可能数消除。

sudoku.sudoku.reg_method()[源代码]

注册所有求解法为一个个物件并储存成一个列表送回给呼叫者

sudoku.sudoku.set_obvious_method_for_pos(m, method1, p1, v)[源代码]

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)[源代码]

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)[源代码]

Try Error Method, only fill the first possible postion

sudoku.sudoku.update_chain(m, first=1, only=False)[源代码]

= 0)。”检查与更新Matrix 物件(m)的链结状况与资讯,并取得链节总数(= 0)。

sudoku.sudoku.update_group_number(m, num)[源代码]

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)[源代码]

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)[源代码]

如果常数WRITEN_POSSIBLE_LIMITS 设定为1..9 时,则表示要像人一样将每个点的可能数