So I have a sudoku algorithm for a generating a completed game board. The problem is that the algorithm in some instances runs out of possible values to place in the grid, so by default it places a 0.
Here's the code:
public void generateBoard() {
// Determine values for board
int r, d;
// For every slot in grid
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
// Create HashSet with values 1-9
for (int k = 1; k <= 9; k++) {
set.add(k);
}
checkRow(i);
checkCol(j);
checkBox(i, j);
// Create an iterator
Iterator<Integer> iterator = set.iterator();
// Add HashSet values to ArrayList via Iterator
while (iterator.hasNext()) {
d = iterator.next();
values.add(d);
}
try {
// Randomly choose from list of viable values to be put into slot
r = new Random().nextInt(values.size());
slots[i][j] = (Integer) values.get(r);
} catch (Exception e) {
e.printStackTrace();
//resetBoard();
//continue;
}
// Clear HashSet and ArrayList after slot is assigned value
values.clear();
set.clear();
}
}
}
public void checkRow(int i) {
for (int c = 1; c <= 9; c++) {
for (int a = 0; a < 9; a++) {
// If c value is already in row remove from HashSet
if (c == slots[i][a]) {
set.remove(c);
}
}
}
}
public void checkCol(int j) {
for (int c = 1; c <= 9; c++) {
for (int a = 0; a < 9; a++) {
// If c value is already in column remove from HashSet
if (c == slots[a][j]) {
set.remove(c);
}
}
}
}
public void checkBox(int i, int j) {
int xSet = (i - (i % 3));
int ySet = (j - (j % 3));
for (int c = 1; c <= 9; c++) {
for (int x = xSet; x < xSet + 3; x++) {
for (int y = ySet; y < ySet + 3; y++) {
// If c value is already in box remove from HashSet
if (c == slots[x][y]) {
set.remove(c);
}
}
}
}
}
And this is what the generated board looks like: http://imgur.com/2cWh61j