Skip to content

Commit 2ddfb01

Browse files
committed
Modularization
1 parent 8b39c19 commit 2ddfb01

File tree

2 files changed

+95
-93
lines changed

2 files changed

+95
-93
lines changed

src/com/company/Main.java

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -17,96 +17,3 @@ public static void main(String[] args) {
1717
t1.start();
1818
}
1919
}
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
23-
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)){
45-
46-
Main.sudoku_array[row_num][col_num] = i;
47-
48-
//Main.gui.sud.printArray();
49-
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/solver.java

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.company;
2+
3+
class solver implements Runnable{
4+
//Choose a box with minimum empty values - MVP(minimum value remaining) heuristic
5+
//Reduce the domain of each box by prechecking the already fixed value in the box
6+
7+
public void run(){
8+
if(getResult())
9+
Main.gui.sud.printArray();
10+
11+
}
12+
private boolean getResult(){
13+
if (checkSolved()) {
14+
return true;
15+
}
16+
int row_num = -1;
17+
int col_num = -1;
18+
for (int i=0; i<9; i++)
19+
for (int j=0; j<9; j++){
20+
if (Main.sudoku_array[i][j] == 0){
21+
row_num=i;
22+
col_num=j;
23+
}
24+
}
25+
26+
for (int i=1; i<=9; i++){
27+
if (checkConstraint(i,row_num,col_num)){
28+
29+
Main.sudoku_array[row_num][col_num] = i;
30+
31+
//Main.gui.sud.printArray();
32+
33+
Main.gui.refreshGUI();
34+
timer();
35+
if (getResult()){
36+
return true;
37+
}
38+
else{
39+
Main.sudoku_array[row_num][col_num] = 0;
40+
41+
//Main.gui.sud.printArray();
42+
Main.gui.refreshGUI();
43+
timer();
44+
}
45+
}
46+
}
47+
return false;
48+
49+
}
50+
private void timer(){
51+
try {
52+
Thread.sleep(1);
53+
//Thread.sleep(10000);
54+
} catch (InterruptedException e) {
55+
e.printStackTrace();
56+
}
57+
}
58+
private boolean checkSolved(){
59+
for (int i = 0 ; i < 9 ; i++)
60+
for (int j = 0 ; j < 9 ; j++)
61+
if (Main.sudoku_array[i][j] == 0)
62+
return false;
63+
return true;
64+
}
65+
private boolean checkConstraint(int value, int row_num,int col_num){
66+
/*
67+
Below for loop checks for the given value in row and col specified
68+
*/
69+
for (int i = 0 ;i < 9 ; i++){
70+
if (Main.sudoku_array[row_num][i] == value ||
71+
Main.sudoku_array[i][col_num] == value)
72+
return false;
73+
}
74+
/*
75+
Below code, checks the box for the given value
76+
*/
77+
int boxRow_num = boxRowCol(row_num);
78+
int boxCol_num = boxRowCol(col_num);
79+
for (int i=boxRow_num; i<3+boxRow_num ;i++)
80+
for (int j=boxCol_num; j<3+boxCol_num ;j++){
81+
if (Main.sudoku_array[i][j] == value)
82+
return false;
83+
}
84+
//if every constraint is satisfied
85+
return true;
86+
}
87+
private int boxRowCol(int num){
88+
if (num==0||num==1||num==2)
89+
return 0;
90+
else if (num==3||num==4||num==5)
91+
return 3;
92+
else
93+
return 6;
94+
}
95+
}

0 commit comments

Comments
 (0)