我正在使用回溯进行数独游戏,在游戏中有一个按钮,可以生成游戏的解决方案,使用目前用户输入的内容。我试图做到这一点,所以我可以在界面中显示回溯过程(显示系统尝试解决方案的值,然后删除它们,如果没有成功)。我尝试添加thread.sleep来测试它,但我收到相同的输出,一个完全解决/未解决的网格。请帮忙。谢谢
package generalTesting;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Container.*;
import java.math.*;
import java.awt.Component;
class driver {
static JComboBox choices;
static JFrame f;
static JTextField[] tf = new JTextField[81];
JPanel cont,p,p1;
static JButton b,b1;
static int x,y,size = 9 ;
static int[][] sudoku = new int[size][size] ;
static double time = 0.00 ;
static boolean flag = false ;
static int rowIndex=-1;
static int columnIndex=-1;
public driver()
{
f = new JFrame("Sudoku Solving Java Application");
String[] stuff= {"Hard","Easy","Manual"};
choices=new JComboBox(stuff);
cont = new JPanel();
cont.setLayout(new BoxLayout(cont,BoxLayout.Y_AXIS));
GridLayout layout=new GridLayout(size,size);
p = new JPanel(new GridLayout(size,size));
for(int i = 0 ; i < size ; i++ )
{
for(int j = 0 ; j < size ; j++ )
{
tf[i*size + j] = new JTextField("");
p.add(tf[i*size + j]);
}
}
p1 = new JPanel();
b = new JButton("Solve Sudoku");
b.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try {
solveSudoku();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
p1.add(b);
b1 = new JButton("Check Answer");
b1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
check();
}
});
p1.add(choices);
p1.add(b1);
cont.add(p);
cont.add(p1);
f.add(cont);
f.pack();
f.setVisible(true);
f.setSize(500,410);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private static void check()
{
makeSudoku();
for(int i=0 ; i<size ; i++ )
{
for(int j=0 ; j<size ; j++ )
{
if(usedInCol(i,j,sudoku[i][j]))
{
if(columnIndex!=-1){
while(columnIndex<81){
tf[columnIndex].setBackground(Color.red);
columnIndex+=9;
}
}
}
if(usedInRow(i,j,sudoku[i][j])){
int w=0;
if(rowIndex!=-1){
rowIndex=rowIndex*9;
while(w<9){
tf[rowIndex].setBackground(Color.red);
rowIndex+=1;
w++;
}
}
}
}
}
}
public static void main(String[] args) {
new driver();
}
private static void makeSudoku()
{
for(int i=0 ; i< size ; i++ )
{
for(int j=0 ; j< size ; j++ )
{
if( ( (tf[i*size + j]).getText() ).equals("") )
sudoku[i][j] = 0 ;
else
sudoku[i][j] = (int)( (tf[i*size + j]).getText().charAt(0) ) - 48;
}
}
}
private static boolean solveSudoku() throws InterruptedException
{
makeSudoku();
int row = 0 , col = 0;
boolean f = false;
//find unassigned location
for( row = 0 ; row < size ; row++ )
{
for( col = 0 ; col < size ; col++ )
{
if( sudoku[row][col] == 0 )
{
f = true;
break;
}
}
if( f == true )
break;
}
if( f == false )
return true ;
for(int n = 1 ; n <= size ; n++ )
{
Thread.sleep(10);
sudoku[row][col] = n ;
(tf[(row)*size + col]).setText(Integer.toString(n));
Thread.sleep(1);
if( isSafe(row,col,n) )
{
//make assignment
sudoku[row][col] = n ;
//print output
if( solveSudoku() )
return true;
{
sudoku[row][col] = 0 ;
tf[((row)*size+col)].setText("");
}
}
}
//trigger backtracking
return false ;
}
private static boolean validate()
{
for(int i=0 ; i<size ; i++ )
{
for(int j=0 ; j<size ; j++ )
{
if( sudoku[i][j] < 0 && sudoku[i][j] > size )
return false ;
if( sudoku[i][j] != 0 && (usedInRow(i,j,sudoku[i][j]) || usedInCol(i,j,sudoku[i][j]) || usedInBox(i,j, sudoku[i][j]) ) )
{
return false ;
}
}
}
return true ;
}
private static boolean isSafe(int r , int c , int n)
{
return ( !usedInRow(r,c,n) && !usedInCol(r,c,n) && !usedInBox(r,c,n) ) ;
}
private static boolean usedInRow(int r , int c, int n)
{
for(int col=0 ; col<size ; col++ )
{
if( col != c && sudoku[r][col] == n && n!=0)
{
rowIndex=r;
return true;
}
}
return false;
}
private static boolean usedInCol(int r,int c , int n)
{
for(int row=0 ; row < size ; row++ )
{
if( row != r && sudoku[row][c] == n && sudoku[row][c]!=0)
{
columnIndex=c;
return true;
}
}
return false;
}
private static boolean usedInBox(int r , int c , int n)
{
int r_st = r-r%((int)Math.sqrt(size)) ;
int c_st = c-c%((int)Math.sqrt(size)) ;
for(int i=0 ; i< (int)Math.sqrt(size) ; i++ )
{
for(int j=0 ; j< (int)Math.sqrt(size) ; j++ )
{
if( r_st+i != r && c_st+j != c && sudoku[r_st+i][c_st+j] == n && n!=0 )
{
return true;
}
}
}
return false;
}
public static void print(){
for(int i=0; i<size;i++)
{
for(int j=0;j<size;j++)
{
System.out.print(sudoku[i][j]+" ");
}
System.out.print("/n");
}
}
}