Skip to content

Commit 183b5fc

Browse files
committed
User Playing option Added(Only works by file as of now)
1 parent 9153b82 commit 183b5fc

File tree

3 files changed

+121
-12
lines changed

3 files changed

+121
-12
lines changed

src/com/company/Main.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class Main {
3030
static protected int[][] sudoku_array = new int[9][9]; //Stores the sudoku
3131
static protected ArrayList<Point> points = new ArrayList<>(); //stores the EMPTY points
3232
static protected guiController gui = new guiController();//Initializes program, calls sudokuInitializer as well
33+
static protected boolean checkSol = false;
3334

3435

3536
public static void main(String[] args) {

src/com/company/guiController.java

Lines changed: 73 additions & 7 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(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
/*
164200
takes 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
}

src/com/company/solver.java

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.company;
22

3+
import javax.swing.*;
34
import java.awt.*;
45
import java.util.*;
56
import java.util.List;
@@ -8,13 +9,50 @@ class solver implements Runnable{ //Implemented as a Thread to avoid freezing an
89
//Chooses a box with MINIMUM VALUES REMAINING (It acts as a heuristic which optimizes the search for the solution
910
ArrayList<Point> sortedPoints = new ArrayList<>(); //It stores the sorted points according
1011
// to box with least number of values unfilled
11-
public void run(){ //acts as a constructor for the class, works with Thread.start function
12+
public void run(){//acts as a constructor for the class, works with Thread.start function
13+
1214
setSortedPoints(); //sets sorted point
13-
if(getResult()) //calls the solving function
14-
Main.gui.sud.printArray();
15+
//new addition
16+
17+
if (Main.checkSol) {
18+
int checkSolved=0,temp,i,j, err_i=0,err_j=0;
19+
20+
for (Point pointIndex : sortedPoints) {
21+
if (Main.sudoku_array[(int) pointIndex.getX()][(int) pointIndex.getY()] != 0) {
22+
i = (int) pointIndex.getX();
23+
j = (int) pointIndex.getY();
24+
25+
temp=Main.sudoku_array[i][j];
26+
Main.sudoku_array[i][j]=0;
27+
if (temp!=0 && !checkConstraint(temp, i, j)) {
28+
err_i=i;
29+
err_j=j;
30+
checkSolved = 1;
31+
Main.sudoku_array[i][j]=temp;
32+
break;
33+
}
34+
Main.sudoku_array[i][j]=temp;
35+
36+
}
37+
}
38+
if (checkSolved==0){
39+
JOptionPane.showMessageDialog(null,"correct");
40+
}else {
41+
//System.out.println(err_i+" "+i+" "+err_j+" "+j);
42+
Main.gui.errorCell(err_i,err_j);
43+
//JOptionPane.showMessageDialog(null,"Wrong");
44+
}
45+
}
46+
else {
47+
48+
if (getResult()) { //calls the solving function
49+
Main.gui.sud.printArray();
50+
System.out.println("Count of backtracking: " + count_iteration);
51+
}
52+
}
1553

1654
}
17-
int delay_time=5;
55+
private int delay_time=5;
1856
solver(int time){
1957
delay_time=time;
2058
}
@@ -92,9 +130,12 @@ private static HashMap<String, Integer> sortByValue(HashMap<String , Integer> ma
92130
}
93131
return temp;
94132
}
133+
134+
95135
/*
96136
This is the MAIN function which solves the sudoku by using sortedpoints arraylist and constraints
97137
*/
138+
int count_iteration=0;
98139
private boolean getResult(){
99140
if (checkSolved()) {
100141
return true;
@@ -124,11 +165,12 @@ private boolean getResult(){
124165
Main.gui.refreshGUI();
125166
timer();
126167
if (getResult()){
168+
//count_iteration++;
127169
return true;
128170
}
129171
else{
130172
Main.sudoku_array[row_num][col_num] = 0;
131-
173+
count_iteration++;
132174
//Main.gui.sud.printArray();
133175
Main.gui.refreshGUI();
134176
timer();

0 commit comments

Comments
 (0)