@@ -8,7 +8,7 @@ class guiController extends JFrame implements Runnable{
88This class handle GUI related functions and also allows GUI refresh whenever any move is taken by AI
99 */
1010 private JPanel grid = new JPanel (new GridLayout (9 , 9 )); //Grid layout
11- private JPanel buttons = new JPanel (new GridLayout (2 , 2 ));
11+ private JPanel buttons = new JPanel (new GridLayout (2 , 3 ));
1212 private JTextField [][] fieldArray = new JTextField [9 ][9 ]; //array of fields for easy extraction
1313
1414 sudokuInitializer sud = new sudokuInitializer (); //initializes array from file
@@ -49,6 +49,7 @@ private void guiInit() {
4949 if (Main .sudoku_array [i ][j ] != 0 ) {
5050 fieldArray [i ][j ].setText (Main .sudoku_array [i ][j ] + "" );
5151 fieldArray [i ][j ].setEditable (false );
52+ fieldArray [i ][j ].setBackground (Color .lightGray );
5253 } else
5354 fieldArray [i ][j ].setText ("" );
5455 fieldArray [i ][j ].setFont (new Font ("Arial" , Font .ITALIC , 24 ));
@@ -82,15 +83,22 @@ private void guiInit() {
8283
8384 JButton solve = new JButton ("Solve" );//Button to compute
8485 JButton Clear = new JButton ("Clear" );//Button to compute
86+ JButton CheckSol = new JButton ("Checksol" ); //Button to check
87+
88+ CheckSol .setBorder (null );
89+ CheckSol .setBackground (Color .white );
90+ buttons .add (CheckSol );
91+
8592 solve .setBorder (null );
8693 solve .setBackground (Color .white );
8794 buttons .add (solve );
8895 buttons .setBackground (Color .white );
8996 add (buttons , BorderLayout .SOUTH );
9097 solve .addActionListener (e -> { //If the button is pressed
98+ Main .checkSol = false ;
9199 if (flag != 0 ) {
92100 try {
93- getText (); //takes in text from the GUI
101+ getText (true ); //takes in text from the GUI
94102 callSolver .run ();
95103
96104
@@ -112,13 +120,40 @@ private void guiInit() {
112120
113121 });
114122
123+ CheckSol .addActionListener (e -> { //If the button is pressed
124+ Main .checkSol = true ;
125+ if (flag != 0 ) {
126+ try {
127+ getText (false ); //takes in text from the GUI
128+ callSolver .run ();
129+
130+
131+ } catch (Exception ex ) {
132+ JOptionPane .showMessageDialog (null ,"Invalid Input" );//Throws error if the input is invalid
133+ clearGUI ();
134+ //flag=-1;
135+ }
136+ }else {
137+ try {
138+ getText (false );
139+ callSolver .run ();
140+
141+ } catch (Exception ex ) {
142+ JOptionPane .showMessageDialog (null ,"Invalid Input" );//Throws error if the input is invalid
143+ clearGUI ();
144+ //flag=-1;
145+ }
146+ }
147+
148+ });
115149
116150 Clear .setBorder (null );
117151 Clear .setBackground (Color .white );
118152 buttons .add (Clear );
119153 Clear .addActionListener (e -> { //If the button is pressed
120154 clearGUI ();
121155
156+
122157 });
123158
124159
@@ -136,12 +171,13 @@ private void guiInit() {
136171 */
137172 callSolver = () -> {
138173 sud .printArray (); //debugging purpose
139- solve . setEnabled ( false ); //cell value becomes fixed
174+
140175 solver solver = new solver (Integer .parseInt (delay_input .getText ()));
141176 Thread t2 = new Thread (solver );
142177 t2 .start ();
143178 System .out .println ("Solving..." );
144- Clear .setEnabled (false );
179+ //Clear.setEnabled(false);
180+ //solve.setEnabled(false);//cell value becomes fixed
145181 };
146182 }
147183
@@ -163,17 +199,22 @@ private void clearGUI(){
163199/*
164200takes in valid input and sets the GUI cells as non editable
165201 */
166- private void getText () throws Exception {
202+ private void getText (boolean editable ) throws Exception {
167203 String temp ;
168204 for (int i = 0 ; i < 9 ; i ++) {
169205 for (int j = 0 ; j < 9 ; j ++) {
170206 temp = fieldArray [i ][j ].getText ();
171207 if (!temp .equals ("" )) {
172208 if (sud .checkValidInput (temp +"" ))
173209 Main .sudoku_array [i ][j ] = Integer .parseInt (temp + "" );//checks valid input, catches exception
174- fieldArray [i ][j ].setEditable (false );
175- }else
210+ if (editable ) {
211+ fieldArray [i ][j ].setEditable (false );
212+ fieldArray [i ][j ].setBackground (Color .lightGray );
213+ }
214+ }else {
176215 Main .points .add (new Point (i , j ));
216+ Main .sudoku_array [i ][j ] = 0 ;
217+ }
177218 }
178219 }
179220 sud .printPoints ();
@@ -188,5 +229,30 @@ protected void refreshGUI(){
188229 else
189230 fieldArray [(int )Main .points .get (i ).getX ()][(int )Main .points .get (i ).getY ()].setText ("" );
190231 }
232+ protected void errorCell (int i , int j ){
233+ for (int k =0 ,x =0 ,y =0 ;k <=Math .max (i ,j );k ++) {
234+ if (x <i )
235+ x ++;
236+ if (y <j )
237+ y ++;
238+ fieldArray [x ][j ].setBackground (Color .red );
239+ fieldArray [i ][y ].setBackground (Color .red );
240+ try {
241+ Thread .sleep (50 );
242+ } catch (InterruptedException e ) {
243+ e .printStackTrace ();
244+ }
245+ if (Main .points .contains (new Point (x ,j )))
246+ fieldArray [x ][j ].setBackground (Color .white );
247+ else
248+ fieldArray [x ][j ].setBackground (Color .lightGray );
249+ if (Main .points .contains (new Point (i ,y )))
250+ fieldArray [i ][y ].setBackground (Color .white );
251+ else
252+ fieldArray [i ][y ].setBackground (Color .lightGray );
253+
254+ }
255+ }
256+
191257
192258}
0 commit comments