0

this is my code for generating a random solved sudoku board. I used the backtrack alghoritm to track the possibilities for every cell. Sometimes (like 3 times out of 10) it doesn't work and give me this [EDIT] I know what this error is about but I don't know how to solve

Exception in thread "main" java.lang.StackOverflowError

This is the code:

how can I solve this? (the checkSquare method is too long to post here, but it only check if a number is valid in the selected square(3x3)

    int [][] board = new int[9][9];
    int [][][] poss = sudoku.creaposs();
    Object[] obj = new Object[2];
    obj[1] = poss;
    obj[0] = board;
    boolean generato;
    do{

    try{
        long start = System.currentTimeMillis();
        obj[0] = creasudo((int[][])obj[0],(int[][][])obj[1],0,0);
        long end = System.currentTimeMillis();
        System.out.println("Tempo trascorso: "+(end-start));
        generato = true;
    }
    catch(Exception exp){
        System.out.println(exp);
        generato = false;
    }
        //sudoku.stampa((int[][]) obj[0]);
    }while(!generato);
    sudoku.stampa((int[][])obj[0]);

METHODS

public static int[][] creasudo(int[][] board, int[][][] poss,int i, int j){
        if (j<0){
            if (i == 0){
                System.out.println("FINEEE");
                System.exit(-1);

            }
            else
            {j = 8;
            i--;
            }
        }
        if (j>8){
            j=0;
            i++;
        }
        if (i == 9)
            return board;
        int n; 
        Random rand = new Random();
        if (poss[i][j][0] == 0){
            poss[i][j] = new int[9];
            for (n=0;n<9;n++){
                poss[i][j][n]=n+1;
            }
            board[i][j]=0;
            return creasudo(board,poss,i,j-1);
        }
        n = rand.nextInt(poss[i][j].length);
        if (!sudoku.checkcol(board,j,poss[i][j][n])|| !sudoku.checkrow(board,i,poss[i][j][n]) ||
                sudoku.checksquare(board,i,j,poss[i][j][n])){
                poss[i][j] = sudoku.removeEl(poss[i][j], poss[i][j][n]);

                return creasudo(board,poss,i,j);
        }
        else{
            board[i][j] = poss[i][j][n];
            return creasudo(board,poss,i,j+1);
        }

    }

public static boolean checkrow(int[][] board, int r, int num){
    int i;
    for (i = 0; i<RIGHE; i++){
        if (board[r][i] == num)
            return false;

    }

    return true;
}

public static boolean checkcol(int[][]board, int c, int num){
    int i;
    for (i = 0; i<RIGHE; i++){
        if (board[i][c] == num)
            return false;
    }

    return true;
}

public static int[]removeEl(int[] array, int n){
    int[] array2;
    int r = 0;
    int i;
    int count = 0;
    for (i = 0; i<array.length; i++){
        if (array[i] != n){
            count ++;
        }
    }
    if (count == 0||(array.length == 1 && array[0] == 0)){
        array2 = new int[1];
        array2[0] = 0;
    }
    else{
        array2 = new int[count];
        for (i = 0; i<array.length; i++){
            if (array[i] != n){
                array2[r] = array[i];
                r++;
            }
        }
    }
    return array2;
}
Leonardo
  • 499
  • 1
  • 7
  • 18
  • 2
    Possible duplicate of [What is a StackOverflowError?](http://stackoverflow.com/questions/214741/what-is-a-stackoverflowerror) – BackSlash Oct 13 '15 at 09:33
  • I know what stackoverflowerror is, I would like to know how to solve this problem, if possible – Leonardo Oct 13 '15 at 09:37
  • Did you open the linked question? – BackSlash Oct 13 '15 at 09:38
  • I already read that, and tried to stop my recursive call, but somtimes it gave me this error – Leonardo Oct 13 '15 at 09:41
  • If you really need that much stack space, this may help: http://stackoverflow.com/questions/2127217/java-stack-overflow-error-how-to-increase-the-stack-size-in-eclipse – Henry Oct 13 '15 at 09:47

0 Answers0