對於我的編程課程,我決定在GUI中創建一個Sudoku板。我已經爲電路板的代碼創建了一個類,只要需要填充的插槽不能填充任何東西,電路板就會停止工作,我似乎無法使其重新啓動。任何幫助?是什麼導致它失速?我如何讓它重新啓動?我的隨機數獨發生器保持失速
我迄今爲止代碼:
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
你是什麼意思,拖延?你的意思是它沒有找到解決方案?順便說一句:使用'Random.nextInt(9)'可能會快得多。 –
您可以附加調試器和/或使用日誌記錄來確定失速發生的位置嗎? –
通過拖延,我的意思是沒有輸出發生,並沒有找到解決方案(由於所有可用的號碼被採取)。 調試沒有說什麼。 –