Skip to content

Commit 8b39c19

Browse files
committed
Major Update1
1 parent 13a4d43 commit 8b39c19

File tree

3 files changed

+165
-30
lines changed

3 files changed

+165
-30
lines changed

src/com/company/Main.java

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

3+
import java.awt.*;
4+
import java.util.ArrayList;
5+
36
public class Main {
4-
static int[][] sudoku_array = new int[9][9];
7+
8+
static protected int[][] sudoku_array = new int[9][9]; //Stores the sudoku
9+
static protected ArrayList<Point> points = new ArrayList<>(); //stores the EMPTY points
10+
static protected guiController gui = new guiController();//Initializes program, calls sudokuInitializer as well
511
public static void main(String[] args) {
6-
guiController gui = new guiController();//Initializes program
7-
gui.setVisible(true);//sets the GUI as visible
12+
13+
//gui.setVisible(true);//sets the GUI as visible
14+
System.out.println("points : "+ points);
15+
16+
Thread t1 = new Thread(gui);
17+
t1.start();
818
}
919
}
10-
class solver{
20+
class solver implements Runnable{
21+
//Choose a box with minimum empty values - MVP(minimum value remaining) heuristic
22+
//Reduce the domain of each box by prechecking the already fixed value in the box
1123

12-
}
24+
public void run(){
25+
if(getResult())
26+
Main.gui.sud.printArray();
27+
28+
}
29+
private boolean getResult(){
30+
if (checkSolved()) {
31+
return true;
32+
}
33+
int row_num = -1;
34+
int col_num = -1;
35+
for (int i=0; i<9; i++)
36+
for (int j=0; j<9; j++){
37+
if (Main.sudoku_array[i][j] == 0){
38+
row_num=i;
39+
col_num=j;
40+
}
41+
}
42+
43+
for (int i=1; i<=9; i++){
44+
if (checkConstraint(i,row_num,col_num)){
1345

46+
Main.sudoku_array[row_num][col_num] = i;
1447

48+
//Main.gui.sud.printArray();
1549

50+
Main.gui.refreshGUI();
51+
timer();
52+
if (getResult()){
53+
return true;
54+
}
55+
else{
56+
Main.sudoku_array[row_num][col_num] = 0;
57+
58+
//Main.gui.sud.printArray();
59+
Main.gui.refreshGUI();
60+
timer();
61+
}
62+
}
63+
}
64+
return false;
65+
66+
}
67+
private void timer(){
68+
try {
69+
Thread.sleep(10);
70+
//Thread.sleep(10000);
71+
} catch (InterruptedException e) {
72+
e.printStackTrace();
73+
}
74+
}
75+
private boolean checkSolved(){
76+
for (int i = 0 ; i < 9 ; i++)
77+
for (int j = 0 ; j < 9 ; j++)
78+
if (Main.sudoku_array[i][j] == 0)
79+
return false;
80+
return true;
81+
}
82+
private boolean checkConstraint(int value, int row_num,int col_num){
83+
/*
84+
Below for loop checks for the given value in row and col specified
85+
*/
86+
for (int i = 0 ;i < 9 ; i++){
87+
if (Main.sudoku_array[row_num][i] == value ||
88+
Main.sudoku_array[i][col_num] == value)
89+
return false;
90+
}
91+
/*
92+
Below code, checks the box for the given value
93+
*/
94+
int boxRow_num = boxRowCol(row_num);
95+
int boxCol_num = boxRowCol(col_num);
96+
for (int i=boxRow_num; i<3+boxRow_num ;i++)
97+
for (int j=boxCol_num; j<3+boxCol_num ;j++){
98+
if (Main.sudoku_array[i][j] == value)
99+
return false;
100+
}
101+
//if every constraint is satisfied
102+
return true;
103+
}
104+
private int boxRowCol(int num){
105+
if (num==0||num==1||num==2)
106+
return 0;
107+
else if (num==3||num==4||num==5)
108+
return 3;
109+
else
110+
return 6;
111+
}
112+
}

src/com/company/guiController.java

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,23 @@
22

33
import javax.swing.*;
44
import java.awt.*;
5-
import java.awt.event.ActionEvent;
6-
import java.awt.event.ActionListener;
7-
import java.io.IOException;
85

9-
class guiController extends JFrame {
6+
class guiController extends JFrame implements Runnable{
107
/*
118
This class handle GUI related functions and also allows GUI refresh whenever any move is taken by AI
129
*/
1310
private JPanel grid = new JPanel(new GridLayout(9, 9)); //Grid layout
14-
private JPanel buttons = new JPanel(new GridLayout(2, 1));
11+
private JPanel buttons = new JPanel(new GridLayout(1, 2));
1512
private JTextField[][] fieldArray = new JTextField[9][9]; //array of fields for easy extraction
1613

17-
private sudokuInitializer sud = new sudokuInitializer(); //initializes array from file
14+
sudokuInitializer sud = new sudokuInitializer(); //initializes array from file
1815
private int flag;
1916

20-
guiController() {
17+
public void run() {
2118
flag = chooseInputStream(); //choose input method
2219
guiInit();//Initializes GUI
2320
//sud.printArray();
21+
setVisible(true);
2422
}
2523
/*
2624
Function to display GUI for the user to choose input method method
@@ -31,7 +29,6 @@ private int chooseInputStream() {
3129
int response = JOptionPane.showOptionDialog(null, "Choose one way", "",
3230
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
3331
null, options, options[0]);
34-
System.out.println(response);
3532
if (response == 0) {
3633
try {
3734
sud.fileInput();
@@ -73,29 +70,59 @@ private void guiInit() {
7370
}
7471
add(grid);
7572
JButton solve = new JButton("Solve");//Button to compute
73+
JButton Clear = new JButton("Clear");//Button to compute
7674
solve.setBorder(null);
7775
solve.setBackground(Color.white);
7876
buttons.add(solve);
79-
buttons.add(new JLabel());
8077
buttons.setBackground(Color.white);
8178
add(buttons, BorderLayout.SOUTH);
8279
solve.addActionListener(e -> { //If the button is pressed
8380
if (flag != 0) {
8481
try {
8582
getText(); //takes in text from the GUI
86-
sud.printArray(); //debugging purpose
87-
solve.setEnabled(false);//cell value becomes fixed
8883
} catch (Exception ex) {
8984
JOptionPane.showMessageDialog(null,"Invalid Input");//Throws error if the input is invalid
85+
clearGUI();
86+
flag=-1;
9087
}
9188
}
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+
}
98+
});
99+
100+
101+
Clear.setBorder(null);
102+
Clear.setBackground(Color.white);
103+
buttons.add(Clear);
104+
Clear.addActionListener(e -> { //If the button is pressed
105+
clearGUI();
106+
92107
});
108+
109+
buttons.setPreferredSize(new Dimension(400,50));
93110
//Basic Frame parameters are set below this
94111
setSize(400, 500);
95112
//setVisible(true);//done in Main function
96113
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
97114
setLocationRelativeTo(null);
98115
}
116+
private void clearGUI(){
117+
for (int i = 0; i < 9; i++)
118+
for (int j = 0; j < 9; j++) {
119+
Main.sudoku_array[i][j] = 0;
120+
fieldArray[i][j].setEditable(true);
121+
fieldArray[i][j].setText("");
122+
}
123+
Main.points.clear();
124+
refreshGUI();
125+
}
99126
/*
100127
takes in valid input and sets the GUI cells as non editable
101128
*/
@@ -105,23 +132,24 @@ private void getText() throws Exception{
105132
for (int j = 0; j < 9; j++) {
106133
temp = fieldArray[i][j].getText();
107134
if (!temp.equals("")) {
108-
Main.sudoku_array[i][j] = sud.checkValidInput(temp); //throws exception
109-
fieldArray[i][j].setEditable(false);
110-
}
135+
if(sud.checkValidInput(temp+""))
136+
Main.sudoku_array[i][j] = Integer.parseInt(temp + "");//checks valid input, catches exception
137+
fieldArray[i][j].setEditable(false);
138+
}else
139+
Main.points.add(new Point(i, j));
111140
}
112141
}
142+
sud.printPoints();
113143
}
114144
/*
115145
Refreshes the GUI whenever AI makes some changes
116146
*/
117-
void refreshGUI(){
118-
for (int i = 0; i < 9; i++) {
119-
for (int j = 0; j < 9; j++) {
120-
if (Main.sudoku_array[i][j] != 0) {
121-
fieldArray[i][j].setText(Main.sudoku_array[i][j]+"");
122-
}
123-
}
124-
}
147+
protected void refreshGUI(){
148+
for (int i=0; i<Main.points.size();i++)
149+
if (Main.sudoku_array[(int)Main.points.get(i).getX()][(int)Main.points.get(i).getY()]!=0)
150+
fieldArray[(int)Main.points.get(i).getX()][(int)Main.points.get(i).getY()].setText(Main.sudoku_array[(int)Main.points.get(i).getX()][(int)Main.points.get(i).getY()]+"");
151+
else
152+
fieldArray[(int)Main.points.get(i).getX()][(int)Main.points.get(i).getY()].setText("");
125153
}
126154

127155
}

src/com/company/sudokuInitializer.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22

33
import javax.swing.*;
44
import java.io.*;
5+
import java.awt.Point;
56

67
class sudokuInitializer {
8+
79
/*
810
Function to check whether the input given via file or manually is valid or not
911
Thorws exception for Invalid Input
1012
*/
11-
int checkValidInput(String input) throws Exception{
13+
boolean checkValidInput(String input) throws Exception{
1214
int n = Integer.parseInt(input+"");
1315
if (n<10 && n>-1)
14-
return n;
16+
return true;
1517
else throw new Exception();
1618
}
1719
/*
@@ -26,6 +28,9 @@ void printArray(){
2628
System.out.print(Main.sudoku_array[i][j] + " ");
2729
}
2830
}
31+
void printPoints(){
32+
System.out.println(Main.points);
33+
}
2934
/*
3035
Takes input from file using Jfilechooser, and the input from the file is inserted in the array
3136
While inserting, it checks for valid input as well
@@ -44,7 +49,11 @@ void fileInput() throws IOException {
4449
st.insert(0,stt);
4550
for (int j=0;j<st.length();j++){
4651
try {
47-
Main.sudoku_array[i][j] = checkValidInput(st.charAt(j)+""); //checks valid input, catches exception
52+
if(checkValidInput(st.charAt(j)+"")) {
53+
Main.sudoku_array[i][j] = Integer.parseInt(st.charAt(j) + "");//checks valid input, catches exception
54+
}
55+
if (Integer.parseInt(st.charAt(j) + "")==0)
56+
Main.points.add(new Point(i, j));
4857
}catch (Exception E){
4958
JOptionPane.showMessageDialog(null,"Error in extracting data from file\nExiting...");
5059
System.exit(1);
@@ -55,6 +64,7 @@ void fileInput() throws IOException {
5564
}
5665
}
5766
}
67+
5868
/*
5969
6070
//NOTE : Method only to be used in catastrophic situations only, used to take in console input

0 commit comments

Comments
 (0)