0

I'm writing a sudoku generator.

static void change(int[][] x, int[][] y, int[][] z, int[][] w, int[][] u, int k) {
    int a = r1.nextInt(9);
    int b = r2.nextInt(2);
    int c = r3.nextInt(2);
    if (x[b][c] != 0 && vcheck(a, c, x, y, z) != false && hcheck(a, b, x, u, w) != false && tcheck(a, x) != false) {
        x[b][c] = a;
        k--;
    } else change(x, y, z, w, u, k);
}

this part of the program is the void that initiates a change of a number int the table on the randomly generated position (b,c) to the value a. the thrown error is

Exception in thread "main" java.lang.StackOverflowError
    at java.util.Random.nextInt(Random.java:307)

could someone tell me what should I change?

Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
  • 1
    Convert that recursive algorithm into an iterative one: http://stackoverflow.com/questions/159590/way-to-go-from-recursion-to-iteration – Colin D Jun 25 '12 at 14:09
  • 2
    What is wrong with a recursive number generator? Changing the algorithm from recursion to iteration will not help the OP understand recursion, which may in fact be the reason for writing it this way. @user1480211: You are getting a stack overflow error because you need return after you reach a base case. – Chris Dargis Jun 25 '12 at 14:17
  • 2
    Something is missing here: the else branch just recurses with the exact same parameters. Unless one of vcheck, hcheck, or tcheck is modifying the array contents, it will never return once it hits that condition. Also, please use variable names that make sense. I have no idea what this method is supposed to be doing. – Alex Jun 25 '12 at 14:21
  • @user1480211: Actually, your base case is probably never evaluating to true, which is probably leading to the `else` statement being called on every function call, leading to the overflow error. Also, it may be a good idea to outline your logic with some comments so we can understand what exactly you are trying to do recursively (i.e., what does vcheck(), hcheck, etc do?). And as the previous comment says, you are not changing any of the params or contents of the array, which will lead to the function being infinitely called. – Chris Dargis Jun 25 '12 at 14:23
  • Are the arrays initialized to anything? Specifically I think it is strange that if x[b][x] is not 0, then you can possibly set it to a. So, only fields already set can be changed again. For more heal please describe a bit of your data structures. – Roger Lindsjö Jun 25 '12 at 16:08

1 Answers1

1

StackOverflowError is what you get when your recursion runs too deep. That's usually a sign of a design error. It seems to me that you are allowing random number generators to control the depth of recursion. That's most definitely a bad idea, since it is not guaranteed that the RNGs will generate the numbers required to avoid recursive calls before VM runs out of stack space.

There are other problems as well. This line of code doesn't seem to do anything useful in the given context:

k--;

You should probably give a little more thought to your algorithm.

COME FROM
  • 2,497
  • 1
  • 14
  • 11