Package: Sudoku - the OOP method

This is the oop method to solve the Sudoku.

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

A 3x3 Box Object

Parameters:
  • idx – int, the box’s id
  • p – the Point object list of this object

Note

This will replace the original GroupBase’s init()

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

Parameters:
  • num – int, find this num, it is a group number in this box
  • pos – list, the Point object list which to find a group number, if it is empty, the function will get all unassigned points to be a list
  • notInLineX – bool, to call get_all_pos to not include x-line’s unassigned point
  • notInLineY – bool, as above, but y-line
Returns:

GroupNumber, none or a GroupNumber Object

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

Parameters:
  • numList – int list, presents the numbers of this chain
  • posList – Point list, presents the positions that form the chain
class sudoku.sudoku.GroupBase(idx, p)[source]

The Base Class of a Line or Box

The init function of a GroupBase object

Parameters:
  • idx – int, the id of this object
  • p – the point object list in this object
allow(v)[source]

check the group can be filled the number(v)?

Parameters:v – int, the number to be checked
Returns:True or False
count_num_possible(count=1)[source]

get the un-assigned position in this group, and possible numbers only in [count] positions

Note

The output format is a tuple list, the tuple has two value, one is number, another is s position list

Parameters:count – int, how many positions are un-assigned to get
Returns:list, [(num,[p1, p2...]),...]
get_all_pos(diff=, []method='a', num=0, notInLineX=None, notInLineY=None, chain=None, possibles=None)[source]

get position list in this group

Parameters:
  • diff – list, exclude the positions in it
  • method – char, a: all, s:has assigned, u:not assigned
  • num – int, if method is u and set the number to 1-9, will get all possible pos which are possible to be assigned the num
  • notInLineX – bool, exclude the x line’s positions
  • notInLineY – bool, exclude the y line’s positions
  • chain – bool, if set, check it, if it is True, just get the chain positions, or get the un-chained positions
  • possibles – int, if method=”u” and possibles!=None, it will only get the possible set’s length = possibles
Returns:

list: a List of Position

get_num_pos(v)[source]

get the position of a group which have been filled the number(v

Parameters:v – int, the number to be found its position
Returns:None or a Point object
class sudoku.sudoku.GroupNumber(b, num, p, direction, idx)[source]

Group Number in Box

Parameters:
  • b – int, the Box Object Idx
  • num – int, the number that the object presents
  • p – Point List, the Point Object List that form this Group Number
  • direction – char, “x” or “y” means the direction of this Group Number
  • idx – int, the line(x or y) index
class sudoku.sudoku.LineX(idx, p)[source]

Line of X

The init function of a GroupBase object

Parameters:
  • idx – int, the id of this object
  • p – the point object list in this object
class sudoku.sudoku.LineY(idx, p)[source]

Line of Y

The init function of a GroupBase object

Parameters:
  • idx – int, the id of this object
  • p – the point object list in this object
class sudoku.sudoku.Matrix(file='')[source]

A Table of a Sudoku

Parameters:file – file, the define file of a sudoku

Properties:

rec: list of (x, y, v, t, d), the record of all the solving steps
filled: int, the amount of assigned points
done: bool, if solved or not
error: bool, if there is an error occurs
lineX: LineX list
lineY: LineY list
b: Box List
p: a two dimensions of list of Point
n: Number list
chain: Chain list
allow(x, y, v)[source]

Checking if the position x, y, can be set the value v

Parameters:
  • x – int, x position
  • y – int, y position
  • v – int, the number
Returns:

True or False

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

get the position list which can see the position, p

Parameters:
  • method – char, “u”: un-assigned positions, “a”: all, “s”: assigned positions
  • num – if method=”u”, the position must have be possible to be filled the number
Returns:

Point list

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

get all positions, to call the :meth:GroupBase.get_all_pos function to get it

Parameters:
  • diff – Point list, other than the postions in this list
  • method – char, “a” – all, “u” – unassigned, “s” – assigned
  • num – int, the unassigned position which has the [num] in its possible numbers
  • chain – bool, if set, check it, if it is True, just get the chain positions, or get the un-chained positions
  • possibles – int, if set, only get the unassigned positions that those possible numbers are equal to it
Returns:

Point list

print_rec()[source]

Print all the steps of solving process

read(file)[source]

Read Sudoku’s Define from file

Parameters:file – file, the sudoku define file, can give it the absolute path name, if only the file name, it would find file in the data path.
reduce(x, y, v, d='set', check=False, info='')[source]

reduce the position(x, y)’s possible numbers from v

Parameters:
  • x – int, x position
  • y – int, y position
  • v – int, the possible number
  • d – str, the description of solving
  • check – bool, check or not
  • info – str, the details of solving
Returns:

int, as following code

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

Parameters:
  • x – int, x position
  • y – int, y position
  • v – int, the number of this position
  • d – str, the description of the solving
  • info – str, the detail methods of the solving
Returns:

int, >=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

Parameters:possibles – 0 for all, >=2, mean get only the possible numbers for it
Returns:Point list
class sudoku.sudoku.Number(v)[source]

Number Object

Note

You can imagine that an object of this class is equal to a country, every country has its own id from 1-9.

Parameters:v – int, the id of the Number, from 1 to 9
can_see_by_group_number(p1)[source]

Check if the position, p1, can be seen of all this number’s group number

Parameters:p1 – Point, a position
Returns:gn if can be seen by it, or None
setit(p1)[source]

save assigned position in the p list

Parameters:p1 – Point, a position that are assigned to this object
class sudoku.sudoku.Point(x, y)[source]

A Position in a Sudoku’s table

Note

We can imagine that a Point object is an house

This is the init of Point Object

Parameters:
  • x – int, the x position of this object
  • y – int, the y position of this object
can_see(p1)[source]

Check this position can see p1?

Parameters:p1 – Point, a point object
Returns:int, the value can’t be 3 or 7 it means the same pos

return code:

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

Parameters:posList – list, a [(x, y),...] list
Returns:list, [(x, y),...
class sudoku.sudoku.SolveMethod(fun, idx, name='', level=0, obvious=True)[source]

Method Object

Parameters:
  • fun – function, the function name defined in Python
  • idx – int, the sequence of executing solving
  • name – str, the description of this function
  • level – int, the difficult level
  • obvious – bool, is it a obvious method for a human
run(m, *args, **ks)[source]

To execute a method to solve a sudoku

Parameters:
  • m – Matrix object
  • ks – first(int), only(bool)
Returns:

tuple, (sets, reduces, method index, first, only), as following

sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
class sudoku.sudoku.Status[source]

To Store running condition

methodLoopIdx: int, the index of the methods loops
methodIdx: int, the method idx which run now
checkPos: Point list, set this variable, if you want to make an exception when this position has been set
writeDownAlready: bool, if the every un-assigned positions have been writen down their possible number?
emulatePossibles: int, to set the possibles when using emulate method, for both positions and numbers
tryStack: list of (m, x, y, idx)
    m: Matrix object, the Matrix copy before try to set (x, y) to be the idx of possible numbers
    x: int, the position x of trying
    y: int, the position y of trying
    idx: int, the index of the possible numbers, starting from 0
tryIdx: int, the depth index of trying
tryUse: bool, if using Guess method or not
emuUse: bool, if using emulator methods or not
Scope: int, the difficult level of this sudoku
Level: int, the level Limit, 0 means no limit
Original: Matrix, the init Matrix
Result: Matrix, the now Matrix
printSteop: bool, if print the steps of solving
nowPath: str, record now path
exception sudoku.sudoku.SudokuDone(x, y, v)[source]

An exception When the table has been filled 81 positions

Parameters:
  • x – int, last position x, when done
  • y – int, last position y, when done
  • v – int, the last number be set, when done
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

Parameters:
  • x – int, position x, which occurs an error
  • y – int, position y, which occurs an error
  • v – int, the number be set or reduced that occurs an error
  • t – char, “s” – set, “r” – 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

Parameters:
  • x – int, position x, when one of the check positions has been set
  • y – int, position y, when one of the check positions has been set
  • v – int, the number be set
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”

Parameters:
  • m – Matrix object
  • first – the first number to be checked
  • only – bool, False – check all numbers, True – check the first number only
Returns:

tuple, (sets, reduces, method index, first, only), as following

sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
sudoku.sudoku.check_line_last_possible_for_number(m, **kw)[source]

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

Parameters:m – Matrix object
Returns:tuple, (sets, reduces, method index, first, only), as following
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
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

Parameters:
  • m – Matrix object
  • first – the first number to be checked
  • only – False, check all numbers, True, check the first number only
Returns:

tuple, (sets, reduces, method index, first, only), as following

sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
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

Parameters:
  • m – Matrix object
  • emu – a two dimension list, [[Point, Assigned Number],...]
  • result – Matrix list, the list of Matrix object of emulating results
Returns:

tuple, (sets, reduces), sets: the amount of setting, reduces: the amount of reducing

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

Parameters:
  • m – Matrix object
  • x – int, the start x position
  • y – int, the start y position
  • v – int, the number to be emulated be in the (x, y) position
  • targets – Point list, tho target list to be checked
  • checkval – int, when the targets be set, it will check whether the checkval has been assigned in those
Returns:

tuple, (rtn, matrix, idx)

rtn: 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
matrix: Matrix, the reulst of emulating
idx: int, if the rtn == 1, this will indicate the index of the target that has been assigned the number, checkval
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.

Parameters:
  • m – Matrix Object
  • sets – int, how many last sets want to be checked
Returns:

tuple, (sets, reduces, method index, first, only), as following

sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
sudoku.sudoku.fill_last_position_of_group(m, **kw)[source]

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

Parameters:m – Matrix Object
Returns:tuple, (sets, reduces, method index, first, only), as following
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
sudoku.sudoku.fill_only_one_possible(m, **kw)[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.

Parameters:
  • m – Matrix Object
  • first – int, the first number of checking
  • only – bool, just check the first number or not
Returns:

tuple, (sets, reduces, method index, first, only), as following

sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
sudoku.sudoku.get_chains(m, group, pos, numbers)[source]

get the possible positions(pos) numbers chain

Parameters:
  • m – Matrix object
  • group – lineX or lineY or Box object
  • pos – Point list of a group
  • numbers – int, the amount of the numbers to form a chain
Returns:

Chain list

sudoku.sudoku.guess(m, idx=0, **kw)[source]

Guess every possible number in a position by from the min possibles of positions

Parameters:
  • m – Matrix object
  • idx – int, 0 means the new guess, others mean the idx of the possible number of a position
Returns:

tuple, (sets, reduces, method index, first, only), as following

sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
sudoku.sudoku.reduce_by_emulate_possible_in_one_position(m, **kw)[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.
Parameters:m – Matrix object
Returns:tuple, (sets, reduces, method index, first, only), as following
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
sudoku.sudoku.reduce_by_emulate_possible_number_in_group(m, **kw)[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.
Parameters:m – Matrix object
Returns:tuple, (sets, reduces, method index, first, only), as following
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
sudoku.sudoku.reduce_by_group_number(m, first=1, only=False)[source]

Reduce the possible number in a posiition by GroupNumber

Parameters:m – Matrix object
Returns:tuple, (sets, reduces, method index, first, only), as following
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
sudoku.sudoku.reduce_by_two_possible_in_one_position(m, **kw)[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

Parameters:m – Matrix object
Returns:tuple, (sets, reduces, method index, first, only), as following
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
sudoku.sudoku.reg_method()[source]

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

Returns:SolveMethod list
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 fillLastPositionOfGroup=0 and checkObviousNumber=1

Parameters:
  • m – Matrix object
  • method1 – int, method index
  • p1 – Point, the postion to be checked
  • v – int, the number to be assigned
Returns:

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, print_step=False)[source]

Solve a sudoku which define in a file!

Parameters:
  • loop_limit – int, the limit for the method loops, 0: no limits
  • rec_limit – int, for debug, when the records >= recLimit, it will stop, 0: no limits
  • check – int, for debug, to set the a (x,y) list to check, it will stop when these positions have been set
  • level_limit – int, limit the methods using, 0 – no limit, >0 – just using the methods which lever < level_limit
  • emu_limits – imt, if using emulator method, this tells the emulator just emulate in emu_limits possible numbers or positions
  • use_try – bool, using guess method or not
  • use_emu – bool, using emulator methods or not
  • print_step – bool, printing the solving steps of not
Raises:
  • SudokuDone – when a method can solve the sudoku
  • SudokuError – when a step will break the rules of sudoku game
Returns:

Matrix object

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

Try Error Method, only fill the first possible position

Parameters:
  • m – Matrix object, if it is None, you should give it the sudoku define file
  • file – file, the sudoku define file
  • depth – int, count the call depth of this function
Returns:

bool, True if the sudoku has been solved

sudoku.sudoku.update_chain(m, **kw)[source]

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

Parameters:m – Matrix object
Returns:tuple, (sets, reduces, method index, first, only), as following
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
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

Parameters:
  • m – Matrix object
  • num – the number to be updated
Returns:

tuple, (sets, reduces, method index, first, only), as following

sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
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 Group Number already known, a recursive function

Parameters:
  • m – Matrix object
  • num – int, the number to be checked
  • amt – int, the amount of Group Number found
  • start – int, the method idx which will be called when it return back to another method
  • first – int, the pointed number to be checked firstly when it return back to another method
  • only – bool, just check the first number or not when it return back to another method
Returns:

tuple, (sets, reduces, method index, first, only), as following

sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not
sudoku.sudoku.write_down_possible(m, **kw)[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

Parameters:m – Matrix object
Returns:tuple, (sets, reduces, method index, first, only), as following
sets: int, the amount of set a number
reduces: int, the amount of reduce a number
mothod index: int, method Index to restart using
first: int, the first number to scan for the methods need this information
only: bool, if only check the first number or not

Previous topic

Start Programming to Solve a Sudoku

This Page