When we have created a sudoku simulate world in the computer to solve a sudoku, now we should go ahead to implement some methods which we solve it in our own hands. In other world, programming, is the stuff which we teach computer to do something that we have known it.
We first introduce the solve environment, then we will introduce some basic methods in this library.
We create a function solve() to do the real solving a sudoku, and we make two exception classes, SudokuDone, SudokuError to capture event happen when we use methods to solve a sudoku.
Note
What is “Exception”?
Exception is an event defined, when the event condition has occurred, system will stop the processing and jump to the exception processing. There are two major exceptions in the environment:
To let the environment know how many methods they can use to solve a sudoku, we create a class, SolveMethod. We use this class to create all methods in a BRAIN. We can treat this BRAIN like the god of this valley. Every time, when people don’t how to choose their suitable houses, you can ask the god of valley, and it will give an answer, or it would say that, “I don’t know how to do either!“
Every method register in the Brain as a SovleMethod object, they have these major properties:
- fun: the function name of the method in python coding
- idx: the index of the method, from the easier to the more difficult, the brain will use this sequence to solve a game one by one.
- name: the name of the method
- level: the difficult level for human, using to count a game’s difficult level
The following is the flow chart of solve():
Note
WORK or NOT WORK?
A method works or not means that using this method can:
In this flow chart, we know that:
fill_only_one_possible:
Find every house in a group, if there is only one house that one country people can live there, that house must let that country’s people to live in.
fill_last_position_of_group:
When in a group(line or box) are only one left, it must allow only one country people to live there.
check_obvious_number:
Check every country people who has lived in a house, and when these people observe other boxes which has yet not lived their country people, can find an only house that allowed their country people or not?
check_inobvious_number:
It is the same method as check_obvious_number, but some boxes’ houses are formed as a Group Number.
reduce_by_group_number:
If there is a Group Number in a box, the empty houses of its same direction could be reduce the possible country people of this Group Number.
update_chain:
As the houses have been lived some people, this would make some empty houses reduce some possible countries’ people. And then these empty houses in a group(x-way line, y-way line, box) may form a Chain.
We could implement a method called check_obvious_for_a_country(m, num) method as an example:
1 def check_obvious_for_a country(m, num): 2 checked = list() 3 for p1 in m.n[num].p: 4 for b in m.b[p1.b].effects: 5 possible = [] 6 if b in checked: 7 continue 8 else: 9 checked.add(b) 10 if num not in m.b[b].possible: 11 continue 12 for p2 in m.b[b].p: 13 if p2.v != 0 or p2.can_see(p1) > 0: 14 continue; 15 if not m.lineX[p2.x].allow(num): 16 continue 17 if not m.lineY[p2.y].allow(num): 18 continue 19 possible.append(p2) 20 if len(possible) == 1: 21 m.setit(possible[0].x, possible[0].y, num, d="Obvious For a Country People")