对于我的编程课程,我决定在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
答案 0 :(得分:1)
你的allTaken方法似乎没有做它应该做的事情。
我的理解是你遍历整个槽矩阵并尝试从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
但这不起作用,因为一旦找到号码,就不会从头开始重新开始搜索。例如,假设矩阵在位置[8] [8]只有1,那么循环将不会在任何地方找到2,3或其他数字,因为y和z的循环已完成。
一种可能的解决方法是跟踪您在矩阵中遇到的所有唯一数字(您可以将它们存储在一个集合中),并在集合大小为10时停止。
但即使使用此修复程序,我也不确定您的算法是否有效:根据我对您的代码的理解,allTaken函数应验证所有数字是否不在完整矩阵中,而是在给定行中,或者列(或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;
}
即使这样,你也不应该期望算法是有效的:随机填充数独的大部分时间都不会起作用。
答案 1 :(得分:0)
问题是,一旦你到达一个没有数字适合的单元格,因为它已经在单元格列中的某个位置,或者行taken
始终为true,并且无限循环。
我也不明白为什么你会使用随机的,这样你就会一遍又一遍地检查相同的数字而浪费计算时间。
您最好使用尚未测试单元格的数字列表,以及尚未在途中使用的数字列表。
此外,你的allTaken方法似乎已经过时了。
你可能想要这样的东西:
public static boolean allTaken(int row, int column, int [][] slot){
int z = 0;
boolean[] taken = new boolean[10];
for (int x = 0; x < 9; x++) {
taken[slot[row][x]] = true;
}
for (int x = 0; x < 9; x++) {
taken[slot[x][column]] = true;
}
boolean f = true;
for (boolean b : taken)
f = f && b;
return f;
}
从这里开始,看看你能走多远。