77import java .io .IOException ;
88
99class guiController extends JFrame {
10-
11- private JPanel grid = new JPanel (new GridLayout (9 , 9 ));
10+ /*
11+ This class handle GUI related functions and also allows GUI refresh whenever any move is taken by AI
12+ */
13+ private JPanel grid = new JPanel (new GridLayout (9 , 9 )); //Grid layout
1214 private JPanel buttons = new JPanel (new GridLayout (2 , 1 ));
13- private JTextField [][] fieldArray = new JTextField [9 ][9 ];
15+ private JTextField [][] fieldArray = new JTextField [9 ][9 ]; //array of fields for easy extraction
1416
15- private sudokuInitializer sud = new sudokuInitializer ();
17+ private sudokuInitializer sud = new sudokuInitializer (); //initializes array from file
1618 private int flag ;
1719
1820 guiController () {
19- flag = chooseInputStream ();
20- guiInit ();
21+ flag = chooseInputStream (); //choose input method
22+ guiInit ();//Initializes GUI
2123 //sud.printArray();
2224 }
23-
25+ /*
26+ Function to display GUI for the user to choose input method method
27+ File or Manual
28+ */
2429 private int chooseInputStream () {
2530 String [] options = new String []{"File" , "Manual" };
2631 int response = JOptionPane .showOptionDialog (null , "Choose one way" , "" ,
@@ -30,13 +35,15 @@ private int chooseInputStream() {
3035 if (response == 0 ) {
3136 try {
3237 sud .fileInput ();
33- } catch (IOException e ) {
34- e . printStackTrace ( );
38+ } catch (Exception e ) {
39+ JOptionPane . showMessageDialog ( null , "Critical Error" );
3540 }
3641 }
3742 return response ;
3843 }
39-
44+ /*
45+ Initializes GUI with the given content from the array
46+ */
4047 private void guiInit () {
4148 for (int i = 0 ; i < 9 ; i ++) {
4249 for (int j = 0 ; j < 9 ; j ++) {
@@ -65,39 +72,48 @@ private void guiInit() {
6572
6673 }
6774 add (grid );
68- JButton solve = new JButton ("Solve" );
75+ JButton solve = new JButton ("Solve" );//Button to compute
6976 solve .setBorder (null );
7077 solve .setBackground (Color .white );
7178 buttons .add (solve );
7279 buttons .add (new JLabel ());
7380 buttons .setBackground (Color .white );
7481 add (buttons , BorderLayout .SOUTH );
75- solve .addActionListener (e -> {
76- if (flag != 0 )
77- getText ();
78- sud .printArray ();
79- solve .setEnabled (false );
82+ solve .addActionListener (e -> { //If the button is pressed
83+ if (flag != 0 ) {
84+ try {
85+ getText (); //takes in text from the GUI
86+ sud .printArray (); //debugging purpose
87+ solve .setEnabled (false );//cell value becomes fixed
88+ } catch (Exception ex ) {
89+ JOptionPane .showMessageDialog (null ,"Invalid Input" );//Throws error if the input is invalid
90+ }
91+ }
8092 });
81-
93+ //Basic Frame parameters are set below this
8294 setSize (400 , 500 );
83- //setVisible(true);
95+ //setVisible(true);//done in Main function
8496 setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
8597 setLocationRelativeTo (null );
8698 }
87-
88- private void getText () {
99+ /*
100+ takes in valid input and sets the GUI cells as non editable
101+ */
102+ private void getText () throws Exception {
89103 String temp ;
90104 for (int i = 0 ; i < 9 ; i ++) {
91105 for (int j = 0 ; j < 9 ; j ++) {
92106 temp = fieldArray [i ][j ].getText ();
93107 if (!temp .equals ("" )) {
94- Main .sudoku_array [i ][j ] = Integer . parseInt (temp );
95- fieldArray [i ][j ].setEditable (false );
108+ Main .sudoku_array [i ][j ] = sud . checkValidInput (temp ); //throws exception
109+ fieldArray [i ][j ].setEditable (false );
96110 }
97111 }
98112 }
99113 }
100-
114+ /*
115+ Refreshes the GUI whenever AI makes some changes
116+ */
101117 void refreshGUI (){
102118 for (int i = 0 ; i < 9 ; i ++) {
103119 for (int j = 0 ; j < 9 ; j ++) {
0 commit comments