2

I am working on a sudoku puzzle, so I put all the items in an array.
So, whenever I get a invalid number, I have to call a function recursively, but I cant do that. I can't understand what the problem id.

My methods are:

function checkValidity(x,y) {
    var number = Math.floor((Math.random()) * 10);
    var validnumber = true;
        for (i = 0; i < 9; i++) {
            if (sudokuValueArray[i][y] == number) {
                validnumber = false;
            }
        }
        for (i = 0; i < 9; i++) {
            if (sudokuValueArray[x][i] == number) {
                validnumber = false;
            }
        }
    if(validnumber==true) {
        return number;
    }
    else if(validnumber == false) {
     return   checkValidity(x, y);
    }
}

And second function is:

function CreateSudokeSample() {
    for (var x = 0; x < 9; x++) {
        for (var y = 0; y < 9; y++) {
           sudokuValueArray[x][y] = checkValidity(x, y);
        }
    }
}

By default I had initilized sudokuValueArray with 0;

Now how do I get recursion?

samgak
  • 23,944
  • 4
  • 60
  • 82
  • 1
    you don't need recursion for what you're trying to achieve - not sure why you think you are NOT getting recursion, that thing will recurse like a recursive recursion on steroids - you're more likely to get into a situation where you've recursed too much – Jaromanda X Jul 14 '15 at 08:47
  • Shouldn't your array be 3x3 for sudoku (not 9x9) ? – SDekov Jul 14 '15 at 08:52
  • Did you try google? There are plenty of sudoku algorithms e.g: https://medium.com/@0xsven/sudoku-validation-with-javascript-1297712093bf – areim Jul 14 '15 at 08:56
  • You can find a working algorithm here: https://github.com/callumacrae/sudoku-creator – Maxim Gritsenko Jul 14 '15 at 09:20

2 Answers2

5

You're getting a stack overflow from recursing too much. However, even if you changed your code to use an iterative algorithm, it won't work because unfortunately the algorithm is broken.

If you go through the array and add random values, checking that they are valid each time, it's still possible to end up in a situation where there are no possible values to add, despite all the values being valid up until that point.

For example, suppose you generate the first two rows like this:

3 9 5 7 1 2 6 4 8
5 1 6 2 4 3 7 9 ?

What are you going to put in the space with the ?

An alternative way of generating the grid is to start off with a regular grid, e.g:

1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
3 4 5 6 7 8 9 1 2
4 5 6 7 8 9 1 2 3
5 6 7 8 9 1 2 3 4
6 7 8 9 1 2 3 4 5
7 8 9 1 2 3 4 5 6
9 1 2 3 4 5 6 7 8

and then randomize it by swapping rows, swapping columns, or swapping numbers (e.g. change all 2s to 7s and all 7s to 2s etc).

samgak
  • 23,944
  • 4
  • 60
  • 82
  • Actually, this algorithm is also not valid because sudocu has also a 3x3 rule. To satisfy that rule there there are more conditions to be met: 1)base layout should be valid; 2) only swaping of the rows and cols inside 3-groups or group swaps are allowed; 3) also tranponse of the whole matrix is allowed. – Maxim Gritsenko Jul 14 '15 at 09:16
  • @MaximGritsenko sure, but I am just assuming the same validation rules as in the OP's code, which does not consider 3x3 blocks – samgak Jul 14 '15 at 09:22
3

You recursion work perfectly fine. The problem with your code is that it is very unlikely that the algorithm you use can produce a valid sudoku.

At some point it becomes impossible to find a valid number so your recursion just goes and goes until the call stack exceeds.

Maxim Gritsenko
  • 2,396
  • 11
  • 25