Java 我的随机数独生成器一直停转

Java 我的随机数独生成器一直停转,java,sudoku,Java,Sudoku,对于我的编程课程,我决定在GUI中创建一个数独板。我已经为电路板的代码创建了一个类,每当需要填充的插槽无法填充任何内容时,电路板就会暂停,并且我似乎无法使其重新启动。有什么帮助吗?是什么导致它失速?如何使其重新启动 到目前为止,我的代码是: public class GameCode{ public static void main(String [] args){ //Declare array for the game's code int [][]

对于我的编程课程,我决定在GUI中创建一个数独板。我已经为电路板的代码创建了一个类,每当需要填充的插槽无法填充任何内容时,电路板就会暂停,并且我似乎无法使其重新启动。有什么帮助吗?是什么导致它失速?如何使其重新启动

到目前为止,我的代码是:

public class GameCode{
    public static void main(String [] args){
        //Declare array for the game's code
        int [][] slot = new int[9][9];
        int [] slotSectorNums = new int[9];
        int num = 1;
        int tries = 0;
        boolean taken = false;
        boolean complete = true;

        do{
            //Reset slot array
            for (int x = 0; x < 9; x++){
                for (int y = 0; y < 9; y++){

                    //Clear the slot
                    slot[x][y] = 0;
                }//End of y for loop
            }//End of x for loop

            //Loop through rows of the array
            for (int row = 0; row < 9; row++){

                //Loop through columns of the array
                for (int column = 0; column < 9; column++){
                    tries = 0;

                    do{
                        tries++;

                        //Make the check-variable true
                        taken = false;

                        //Generate random integer for num
                        num = (int)(Math.random() * 9 + 1);

                        //Loop check within the row
                        for (int rowSlot = 0; rowSlot < 9; rowSlot++){

                            //Compare random number against row number
                            if (num == slot[rowSlot][column])
                                taken = true;
                        }//End of rowSlot loop

                        //Loop check within the column
                        for (int columnSlot = 0; columnSlot < 9; columnSlot++){

                            //Compare random number against column number
                            if(num == slot[row][columnSlot])
                                taken = true;
                        }
                    }while(taken == true && allTaken(row, column, slot) == false);
                    slot[row][column] = num;
                    temp(slot);
                }//End of column for loop
            }//End of row for loop
            temp(slot);
        }while(slot[8][8] != 0);
    }//End of main method

    public static int[] slotSectorNumDecide(int row, int column, int [][] slot){
        int [] slotNums = new int[9];
        int slotSectorRow = slotSectorRow(row) * 3;
        int slotSectorColumn = slotSectorColumn(column) * 3;
        int z = 0;

        //Loop for every slot
        for (int x = slotSectorRow; x < slotSectorRow + 2; x++){
            //Loop for every dimension
            for (int y = slotSectorColumn; y < slotSectorColumn + 2; y++){

                //Add the slot in the correct dimension to slotNums
                slotNums[z] = slot[x][y];

                //Increment the space in slotNums
                z++; 
            }//End of y for loop
        }//End of x for loop

        return slotNums;
    }//End of slot sectorSectorNumDecide

    public static int slotSectorRow(int row){
        int slotRow;
        slotRow = row / 3;
        System.out.println(row + " " + slotRow);

        return slotRow;
    }//End of slotSectorRow

    public static int slotSectorColumn(int column){
        int slotColumn;
        slotColumn = column / 3;
        System.out.println(column + " " + slotColumn);

        return slotColumn;
    }//End of slotSectorColumn method

    public static boolean allTaken(int row, int column, int [][] slot){
        int x = 1;

        for (int y = 0; y < 9 && x < 10; y++){
            for (int z = 0; z < 9 && x < 10; z++){
                if(slot[y][z] == x && x < 10)
                    x++;
            }//End of z for loop
        }//End of y for loop

        if (x == 10)
            return true;
        else
            return false;
    }//End of allTaken method

    public static void temp(int [][] slot){
        for (int x = 0; x < 9; x++){
            for (int y = 0; y < 9; y++){
                System.out.print("|" + slot[x][y]);
            }//End of y for loop
            System.out.println();
        }//End of x for loop
        System.out.println();
    }//End of temp method
}//End of class
公共类游戏代码{
公共静态void main(字符串[]args){
//为游戏代码声明数组
int[][]插槽=新int[9][9];
int[]slotSectorNums=新int[9];
int num=1;
int=0;
布尔值=假;
布尔完成=真;
做{
//复位槽阵列
对于(int x=0;x<9;x++){
对于(int y=0;y<9;y++){
//清理插槽
槽位[x][y]=0;
}//循环的y端
}//循环x的结束
//循环遍历数组的行
对于(int行=0;行<9;行++){
//循环遍历数组的列
for(int列=0;列<9;列++){
尝试=0;
做{
尝试++;
//使check变量为true
take=false;
//为num生成随机整数
num=(int)(Math.random()*9+1);
//行内循环检查
对于(int rowSlot=0;rowSlot<9;rowSlot++){
//比较随机数和行号
if(num==插槽[rowSlot][column])
假设为真;
}//行槽循环结束
//列中的循环检查
对于(int columnSlot=0;columnSlot<9;columnSlot++){
//比较随机数和列数
if(num==插槽[行][列插槽])
假设为真;
}
}while(take==true&&alltake(行、列、槽)==false);
槽[行][列]=num;
温度(槽);
}//循环的列结束
}//循环的行结束
温度(槽);
}而(槽位[8][8]!=0);
}//主方法结束
公共静态int[]slotsetornumdecide(int行、int列、int[]slot){
int[]slotNums=新int[9];
int slotSectorRow=slotSectorRow(行)*3;
int slotSectorColumn=slotSectorColumn(列)*3;
int z=0;
//每个插槽的循环
对于(int x=slotsetorrow;x
您的Alltake方法似乎没有达到应有的效果

我的理解是,您遍历整个插槽矩阵,并尝试查找从1到9的每个数字的一次出现:

    for(int y = 0; y < 9 && x < 10; y++) {
        for(int z = 0; z < 9 && x < 10; z++) {
            if(slot[y][z] == x && x < 10)
                x++;
        }// End of z for loop
    }// End of y for loop
for(int y=0;y<9&&x<10;y++){
对于(intz=0;z<9&&x<10;z++){
if(槽[y][z]==x&&x<10)
x++;
}//循环的z结束
}//循环的y端
但这不会起作用,因为一旦找到号码,就不会从一开始就重新启动搜索。例如,假设矩阵仅在位置[8][8]处有1,则循环不会在任何位置找到2、3或其他数字,因为y和z的循环已完成

一个可能的解决办法是跟踪矩阵中遇到的所有唯一数字(可以将它们存储在一个集合中),并在集合大小为10时停止

但即使有了这个修正,我也不确定您的算法是否能工作:根据我对代码的理解,alltake函数应该验证所有数字是否都出现在给定的行或列(或3乘3平方)中,而不是出现在完整的矩阵中

在伪代码中,这类似于:

boolean allTaken(int row, int column, int[][] slot) {
  Set<Integer> taken = Collections.emptySet();

  // Add elements from the row
  for(int x=0; x<9; x++) {
    taken.add(slot[row][x]);
  }

  // Add elements from the column
  for(int y=0; y<9; y++) {
    taken.add(slot[y][column]);
  }

  // Check the 3-by-3 square this row/column is in

  // sx and sy are the top-left coordinates of this square
  int sx = (row/3)*3; 
  int sy = (column/3)*3;
  for(int dx=0; dx<3; dx++) {
    for(int dy=0; dy<3; dy++) {
        taken.add(slot[sx+dx][sy+dy]);
    }
  }

  // All options are taken if the numbers from 1 to 9 appear in the set
  // Zero will be present too because slot[row][col] is zero.
  // So we expect 10 elements.
  return taken.size() == 10;
}
boolean-alltake(int行、int列、int[]slot){
Set take=Collections.emptySet();
//添加行中的元素

对于(intx=0;x您的alltake方法似乎没有做到它应该做的事情

我的理解是,您遍历整个插槽矩阵,并尝试查找从1到9的每个数字的一次出现:

    for(int y = 0; y < 9 && x < 10; y++) {
        for(int z = 0; z < 9 && x < 10; z++) {
            if(slot[y][z] == x && x < 10)
                x++;
        }// End of z for loop
    }// End of y for loop