Re: sudoku
<hawat.thufir@gmail.com> wrote in message
news:1150249058.517241.105370@i40g2000cwc.googlegroups.com...
Oliver Wong wrote:
[...]
SudokuGrid initialGrid = new SudokuGrid();
SudokuGrid solution = initialGrid.solve();
[...]
Yes, although I still would like to break it down further than "solve"
:)
When designing, I find it's useful to first think of how the user is
going to use your class, and establish an interface from that, and only
after having established that interface, do you start worrying about
implementation.
As a user of your Sudoku API, perhaps I'd very much would like a solve()
method which returns a new, completely solved grid.
The implementation of solve() could, of course, be "broken down" in the
sense that it calls a bunch of other (private) methods. e.g.
<pseudoCode>
public SudokuGrid solve() {
if (!isSolveable()) {
throw new IllegalStateException("Sudoku grid is not solveable");
}
SudokuGrid currentGrid = this;
for (Solver s : getSolvers()) {
if (currentGrid.isSolved()) {
break;
}
currentGrid = s.solve(currentGrid);
}
assert currentGrid.isSolved();
return currentGrid;
}
private List<Solver> getSolvers() {
List<Solver> returnValue = new LinkedList<Solver>();
returnValue.add(new SinglePositionSolver());
returnValue.add(new SingleCandidateSolver());
returnValue.add(new CandidateLinesSolver());
returnValue.add(new DoublePairSolver());
returnValue.add(new MultipleLinesSolver());
returnValue.add(new NakedPairSolver());
returnValue.add(new NakedTripletSolver());
returnValue.add(new HiddenPairSolver());
returnValue.add(new HiddenTripletSolver());
returnValue.add(new XWingSolver());
returnValue.add(new SwordFishSolver());
returnValue.add(new ForcingChainsSolver());
returnValue.add(new DancingChainsSolver());
}
</pseudoCode>
- Oliver