Previous topic

Start Programming to Solve a Sudoku

This Page

Package: Sudoku - the OOP method

This is the oop method to solve the Sudoku.

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

Box

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

if the unassigned num in this box which it’s all possible positions have the same direction, we call it as a GroupNumber

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

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

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

Group Number in Box

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

Line of X

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

Line of Y

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

A Table of a Sudoku

allow(x, y, v)[source]

Checking if the position x, y, can be set the value 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]

Print all the steps of solving process

read(file)[source]

Read Sudoku’s Define from file

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]

Number Object

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]

save assigned position in the p list

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

A Position in a Sudoku’s table

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]

Method Object

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

An exception When the table has been filled 81 positions

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

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

exception sudoku.sudoku.SudokuStop[source]

An exception When the the record number >= recLimit

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

An exception When the position, checkPos, has been set, and program want to setit

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]

Check every line that only have one position for un-assigned number

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]

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

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]

If the un-assigned positions in a group(line or box) are only one left

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

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.

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]

Reduce the possible number in a posiition by GroupNumber

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

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

sudoku.sudoku.reg_method()[source]

register all method as an object and save them into a list to return

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]

Update the chain of line return: >=0 means the chain number’s amount in the matrix, m

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]

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