Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Sudoku generator problems(Stack overflow)

807603Oct 20 2007 — edited Oct 20 2007
I've been having some troubles trying to implement a program that generates sudoku problems. It takes a complete puzzle and pulls out the first number and tries to solve it with any of the 8 other numbers and if it can't then that means removing that variable completely will still keep the uniqueness of the solution. Problem is, I'm getting some stack overflow errors while implementing this and I have no idea why. My code is as follows, where the method solve is a program I've already written that can solve any sudoku problem.

public int[][] generate(int [][] board) throws java.io.IOException {
int oldValue;
int[][] tempBoard = new int[9][9];
for (int x = 0; x < 9; x++) {
for (int y = 0; y < 9; y++) {
tempBoard[x][y] = board[x][y];
}
}
for (int x = 0; x < 9; x++) {
for (int y = 0; y < 9; y++) {
oldValue = board[x][y];
for (int z = 1; z <= 9; z++) {
if (z != oldValue) {
board[x][y] = z;
tempBoard = solve(tempBoard);
if (tempBoard != null) {
board[x][y] = oldValue;
break;
}
else {
tempBoard = new int[9][9];
for (int a = 0; a < 9; a++) {
for (int b = 0; b < 9; b++) {
tempBoard[a] = board[a][b];
}
}
}
}
}
board[x][y] = 0;
for (int c = 0; c < 9; c++) {
for (int d = 0; d < 9; d++) {
tempBoard[c][d] = board[c][d];
}
}
}
}
return board;
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 17 2007
Added on Oct 20 2007
2 comments
225 views