Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Sudoku

...
[3 byte] By [Akole] at [2007-11-11 7:51:57]
# 1 Re: Sudoku
If you want to return the answer of a specified square, then you're going to need to solve it first. I would recommend having a class Square, which would hold all the possible values of that square.
The board would be a 9 by 9 array of these squares.

class Square {
ArrayList<Integer> possibilities = new ArrayList<Integer>();

public Square() {
for (int i = 1; i <= 9; i++) {
possibilities.add(i);
}
}

/** this will be called only for the squares that are given to us */
public Square(int num) {
if (num => 1 && num <= 9) {
possibilities.add(num);
}
}

public void removePossibility(int num) {
possibilities.remove(num);
}
}

etc.

About the algorithm, you're going to have to come up with that yourself. For example, if you have a number 4 given to you at index (1, 0), then you'll have to do something like:

for (int i = 0; i < 9; i++) {
if (i != 0) {
square[1][i].remove(4);
}
if (i != 1) {
square[i][0].remove(4);
}
}

for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i != 1 and j != 0) {
square[i][j].remove(4);
}
}
}

Good luck! :)
destin at 2007-11-11 22:37:09 >
# 2 Re: Sudoku
ArrayList<Integer> possibilities = new ArrayList<Integer>();

Implemented as of java 1.5, from what i have read (I think) :confused:
major at 2007-11-11 22:38:14 >