[Pythonsudoku-devel] Code rewrite
Brought to you by:
xoseotero
From: Xosé A. O. F. <xos...@gm...> - 2008-09-26 19:04:48
|
Hi, I'll begin to rewrite the code, as I advance you in the "help wanted" message. I'm planning to rewrite 2 thinks: the file format and how the sudokus are solved. 1. File format: Currently, the file format that Python Sudoku uses is a plain text file with a optional header setting the size of the sudoku and a list of values. 0 in that list means that the number is not known. I have already wrote a new version of the file format, but still not in subversion, that uses XML to store the information. In the first version only the given numbers and the calculated numbers are set. I'll try to commit the code to work with this format this night. 2. Solver method: If you have read the pythonsudoku/sudoku.py code you'd find that the code is spaghetti code: the 3/4 methods that Python Sudoku already uses are defined inside the Sudoku class with the use of methods inside methods... I'm planning to change the way that the methods are defined. Create a base Method class with a solve() method that tries to calculate new values and a Sudoku class with methods to get the positions of a row/column/region, to set calculate values and to remove possible values. class Sudoku: row(x, y): iterator with the positions of the row column(x, y): iterator with the positions of the column region(x, y): iterator with the positions of the region value(x, y, value): set the value of a position remove_candidate(x, y, value): remove a value from the candidates if there is only one candidate: value(x, y, VALUE) solve(): continue = True while continue: continue = False for method in self.methods: if method.solve(): continue = True create(): brute force as is right now class Method: def solve(): I think this way it will be easier to write new methods. What do you think? Future improvements: -Pass to the methods the changes from the last execution of the method so it's not needed to try to solve all the sudoku. -Show/save the method used to calculate a value or remove a candidate value. 3. Rewrite branch: I'll begin to rewrite the code in the rewrite branch [0]. https://pythonsudoku.svn.sourceforge.net/svnroot/pythonsudoku/pythonsudoku/branches/rewrite As always, any question/idea will be welcomed. |