Feb 04, 2009 13:10
Hi
Oh, object oriented programming. Sometimes I write Java programs to do a little something, like solve a sudoku puzzle or make me some prime numbers. Because it's Java, and it's so object oriented already, you can forget to really break things down. I end up writing a page of code that's just a bunch of methods for doing this or that, and a Main method. Looking at them afterward I think "hmm... with just a bit more forethought this could be something more."
I know, not every program needs to be a bunch of tiny fish moving together to form a shimmering shoal. Ten lines of code doesn't really need to be broken down. It would run faster if you didn't. If you did break it down, though! Oh my the good times! Think of Ruby: every integer is a member of a class! They all have their own methods etc... When you can just ask each number
n.isPrime? rather than having to make a whole new block of code you feel better about yourself and the world (granted it's nice if you were the one who implemented Integer::isPrime?). The program looks and feels like an organism. You know, with ORGANS.
So my implementation of sudoku is kind of excessive. The board is a member of class Cell, and the constructor method for Cell is recursive, so by calling sudoku = new Cell(3, 3, 1) you get a 3 by 3 square of 3 by 3 squares of squares, all of which are individual instances of Cell, all wrapped up in a Cell. Then you can initialize them to take an appropriate array of integers (in this case, int[9][9]) set up to describe the sudoku board. Each Cell receives a value: either an integer or an array of integers from 1-9. The ones that have only one value print that value to a display object, and the ones that don't will work busily by themselves to narrow down their array of values.
I don't think it can solve every sudoku! But that it because I don't think I can solve every sudoku. It thinks through the puzzles in the way that I have become accustomed to, but if more difficult sudoku puzzles require pathing through possibilities to find an answer, this board doesn't do that yet.
z.