0

I'm working on a sudoku game using backtracking, in the game there's a button that generates a solution to the game working with what the user has input so far. Im trying to make it so i can show the backtracking process within the interface( showing system attempting values for the solution then removing them if there is no success).I tried adding a thread.sleep to test it out but i receive the same output, a fully solved/unsolved grid. Please help. Thank you

   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");
    }
}

}

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details and the fix. – Andrew Thompson Dec 31 '17 at 18:02
  • 1
    In the future, please [search out your problem before posting](https://www.google.com/search?q=site%3Astackoverflow.com+java+add+delay+to+swing+gui). The solution (as you'll see in the thousands of duplicates) is to use a [Swing Timer](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html). – Hovercraft Full Of Eels Dec 31 '17 at 18:03
  • I did, didnt find a solution. – Whiteheart Awashreh Dec 31 '17 at 18:06
  • Then improve your Google skills. Please see my example search in the link above. – Hovercraft Full Of Eels Dec 31 '17 at 18:28

0 Answers0