Skip to content

Commit 935268d

Browse files
committed
Added user delay
1 parent 398c1e6 commit 935268d

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

src/com/company/guiController.java

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class guiController extends JFrame implements Runnable{
88
This 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(1, 2));
11+
private JPanel buttons = new JPanel(new GridLayout(2, 2));
1212
private JTextField[][] fieldArray = new JTextField[9][9]; //array of fields for easy extraction
1313

1414
sudokuInitializer sud = new sudokuInitializer(); //initializes array from file
@@ -69,6 +69,13 @@ private void guiInit() {
6969

7070
}
7171
add(grid);
72+
73+
JLabel timer_delay = new JLabel("Set Delay(in ms)");
74+
JTextField delay_input = new JTextField("5");
75+
timer_delay.setHorizontalAlignment(SwingConstants.CENTER);
76+
buttons.add(timer_delay);
77+
buttons.add(delay_input);
78+
7279
JButton solve = new JButton("Solve");//Button to compute
7380
JButton Clear = new JButton("Clear");//Button to compute
7481
solve.setBorder(null);
@@ -80,21 +87,37 @@ private void guiInit() {
8087
if (flag != 0) {
8188
try {
8289
getText(); //takes in text from the GUI
90+
91+
sud.printArray(); //debugging purpose
92+
solve.setEnabled(false);//cell value becomes fixed
93+
solver solver = new solver(Integer.parseInt(delay_input.getText()));
94+
Thread t2 = new Thread(solver);
95+
t2.start();
96+
System.out.println("Solving...");
97+
Clear.setEnabled(false);
98+
8399
} catch (Exception ex) {
84100
JOptionPane.showMessageDialog(null,"Invalid Input");//Throws error if the input is invalid
85101
clearGUI();
86-
flag=-1;
102+
//flag=-1;
103+
}
104+
}else {
105+
try {
106+
sud.printArray(); //debugging purpose
107+
solve.setEnabled(false);//cell value becomes fixed
108+
solver solver = new solver(Integer.parseInt(delay_input.getText()));
109+
Thread t2 = new Thread(solver);
110+
t2.start();
111+
System.out.println("Solving...");
112+
Clear.setEnabled(false);
113+
114+
} catch (Exception ex) {
115+
JOptionPane.showMessageDialog(null,"Invalid Input");//Throws error if the input is invalid
116+
clearGUI();
117+
//flag=-1;
87118
}
88119
}
89-
if (flag!=-1) {
90-
sud.printArray(); //debugging purpose
91-
solve.setEnabled(false);//cell value becomes fixed
92-
solver solver = new solver();
93-
Thread t2 = new Thread(solver);
94-
t2.start();
95-
System.out.println("Solving...");
96-
Clear.setEnabled(false);
97-
}
120+
98121
});
99122

100123

@@ -106,12 +129,15 @@ private void guiInit() {
106129

107130
});
108131

109-
buttons.setPreferredSize(new Dimension(400,50));
132+
133+
buttons.setPreferredSize(new Dimension(450,100));
110134
//Basic Frame parameters are set below this
111-
setSize(400, 500);
135+
setSize(450, 550);
112136
//setVisible(true);//done in Main function
113137
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
114138
setLocationRelativeTo(null);
139+
if (flag==0)
140+
Clear.setEnabled(false);
115141
}
116142
/*
117143
\Function below clears the GUI

src/com/company/solver.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public void run(){ //acts as a constructor for the class, works wit
1414
Main.gui.sud.printArray();
1515

1616
}
17+
int delay_time=5;
18+
solver(int time){
19+
delay_time=time;
20+
}
1721
/*
1822
This function creates a map for all the 9 boxes
1923
Sorts them, and creates a new arraylist of points which are sorted according to the boxes
@@ -139,7 +143,7 @@ private boolean getResult(){
139143
*/
140144
private void timer(){
141145
try {
142-
Thread.sleep(5); //CHANGE DELAY TIME HERE , SET minimum 5ms for noticable difference and faster result
146+
Thread.sleep(delay_time); //CHANGE DELAY TIME HERE , SET minimum 5ms for noticable difference and faster result
143147
//Thread.sleep(10000);
144148
} catch (InterruptedException e) {
145149
e.printStackTrace();

0 commit comments

Comments
 (0)