-3

I'm working on sudoku project and I found this:
http://www.codeproject.com/Articles/23206/Sudoku-Algorithm-Generates-a-Valid-Sudoku-in

I converted that code into java but I just can't see where I have made mistake...

It almost works but it still places few numbers on same lines.

enter image description here

I have been stuggling with this sudoku generator for some time, I just can't see where I have done mistake, first I converted that vb.net code to C# and it worked fine.

Here is my java code (I left out code that produces grid):

private static JTextField sudoku[][] = new JTextField[9][9];
public static List<Square> sudokuGrid = new ArrayList<Square>();

public static void GenerateGrid(){
    Square[] Squares = new Square[81];

    for(int i = 0; i <= 80; i++){
        Squares[i] = new Square();
    }

    List<List<Integer>> available = new ArrayList<List<Integer>>();
    int c = 0;

    for(int x = 0; x <= 80; x++){
        available.add(new ArrayList<Integer>());
        for(int i = 1; i <= 9; i++){
            available.get(x).add(i);
        }
    }

    while(!(c == 81)){
        if(available.get(c).size() != 0){
             int i = GetRan(0, available.get(c).size() - 1);
             int z = available.get(c).get(i);

             if (Conflicts(Squares, Item(c, z)) == false){
                 Squares[c] = Item(c, z);
                 available.get(c).remove(i);
                 c += 1;
             }else{
                 available.get(c).remove(i);
             }
        }else{
             for (int y = 1; y <= 9; y++){
                 available.get(c).add(y);
             }
             Squares[c - 1] = new Square();
             c -= 1;
        }

        int j = 0;

        for (j = 0; j <= 80; j++)
        {
            sudokuGrid.add(Squares[j]);
        }
    }

}

private static boolean Conflicts(Square[] CurrentValues, Square test){

    for (Square s : CurrentValues){
        if ((s.Across != 0 && s.Across == test.Across) || (s.Down != 0 && s.Down == test.Down) || (s.Region != 0 && s.Region == test.Region)){
            if (s.Value == test.Value){
                return true;
            }
        }
    }

    return false;
}

private static Square Item(int n, int v){
    Square functionReturnValue = new Square();
    n += 1;
    functionReturnValue.Across = GetAcrossFromNumber(n);
    functionReturnValue.Down = GetDownFromNumber(n);
    functionReturnValue.Region = GetRegionFromNumber(n);
    functionReturnValue.Value = v;
    functionReturnValue.Index = n - 1;
    return functionReturnValue;
}

public static int GetAcrossFromNumber(int n){
    int k = 0;
    k = n % 9;
    if (k == 0)
        return 9;
    else
        return k;
}


public static int GetDownFromNumber(int n){
    int k = 0;
    if (GetAcrossFromNumber(n) == 9)
    {
        k = n / 9;
    }
    else
    {
        k = n / 9 + 1;
    }
    return k;
}

private static int GetRegionFromNumber(int n){
    int k = 0;
    int a = GetAcrossFromNumber(n);
    int d = GetDownFromNumber(n);

    if (1 <= a && a < 4 && 1 <= d && d < 4) {
        k = 1;
    } else if (4 <= a && a < 7 && 1 <= d && d < 4) {
        k = 2;
    } else if (7 <= a && a < 10 && 1 <= d && d < 4) {
        k = 3;
    } else if (1 <= a && a < 4 && 4 <= d && d < 7) {
        k = 4;
    } else if (4 <= a && a < 7 && 4 <= d && d < 7) {
        k = 5;
    } else if (7 <= a && a < 10 && 4 <= d && d < 7) {
        k = 6;
    } else if (1 <= a && a < 4 && 7 <= d && d < 10) {
        k = 7;
    } else if (4 <= a && a < 7 && 7 <= d && d < 10) {
        k = 8;
    } else if (7 <= a && a < 10 && 7 <= d && d < 10) {
        k = 9;
    }
    return k;
}

public static int GetRan(int lower, int upper){
    Random rand = new Random();
    return rand.nextInt((upper - lower) + 1) + lower;
}


public void newGame() {
    for (int x = 0; x <= 8; x++) {
        for (int y = 0; y <= 8; y++) {
            sudoku[x][y].setEditable(true);
            sudoku[x][y].setText("");
        }
    }

    sudokuGrid.clear();
    GenerateGrid();

    for(Square s : sudokuGrid){
        for(int x = 0; x <= 8; x++){
            for(int y = 0; y <= 8; y++){
                int index = s.Index;
                if(sudoku[x][y].getName().equals(String.valueOf(index))){
                    sudoku[x][y].setText(String.valueOf(s.Value));
                }
            }
        }
    }   
}

class Square{

    public int Across;
    public int Down;
    public int Region;
    public int Value;
    public int Index;

}

Some help please :)

plexcell
  • 1,647
  • 2
  • 15
  • 29

1 Answers1

1

If you look closely at the linked code in GenerateGrid:

int j = 0;

for (j = 0; j <= 80; j++)
{
    sudokuGrid.add(Squares[j]);
}

should come after the while(c != 81) loop. In your code, it is inside the loop.

This means that you're adding too many squares to sudokuGrid. To check that this is happening, print the size of sudokuGrid at the end of the method -- it'll be a lot greater than 81.

Edit:

Also, there might be problems with the way you're displaying the board. Try this at the end of newGame, after the GenerateGrid call:

for(int x = 0; x <= 8; x++) {
    for(int y = 0; y <= 8; y++) {
        sudoku[x][y].setText("" + sudokuGrid.get(x * 9 + y).Value);
    }
}
Community
  • 1
  • 1
irrelephant
  • 4,091
  • 2
  • 25
  • 41