对于我的编程课程,我决定在 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