Java:完整的数独生成器,我在这里缺少什么?

时间:2014-11-06 19:04:31

标签: java sudoku

我正在从事数独项目,我发现了这个:
http://www.codeproject.com/Articles/23206/Sudoku-Algorithm-Generates-a-Valid-Sudoku-in

我将该代码转换为java但我无法看到我犯错的地方......

它几乎可以工作,但它仍然在同一行上放置很少的数字。

enter image description here

我已经和这个数独生成器玩了一段时间了,我只是看不出我犯了错误的地方,首先我将该vb.net代码转换为C#并且运行正常。

这是我的java代码(我遗漏了产生网格的代码):

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;

}

请一些帮助:)

1 个答案:

答案 0 :(得分:1)

如果仔细查看GenerateGrid中的链接代码:

int j = 0;

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

应该在while(c != 81)循环之后。在你的代码中,它在循环内。

这意味着您要向sudokuGrid添加太多方块。要检查是否发生了这种情况,请在方法结尾处打印sudokuGrid的大小 - 它将大于81。

编辑:

此外,您显示电路板的方式可能存在问题。在newGame来电后,GenerateGrid结束时尝试此操作:

for(int x = 0; x <= 8; x++) {
    for(int y = 0; y <= 8; y++) {
        sudoku[x][y].setText("" + sudokuGrid.get(x * 9 + y).Value);
    }
}