Skip to content

Commit 82f2bc9

Browse files
committed
Initial Commit
0 parents  commit 82f2bc9

File tree

10 files changed

+234
-0
lines changed

10 files changed

+234
-0
lines changed

.idea/description.html

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/project-template.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sudoku_AI.iml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
12+

src/com/company/Main.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.company;
2+
3+
public class Main {
4+
static int[][] sudoku_array = new int[9][9];
5+
public static void main(String[] args) {
6+
guiController gui = new guiController();
7+
gui.setVisible(true);
8+
}
9+
}
10+
class solver{
11+
12+
}
13+
14+
15+

src/com/company/guiController.java

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.company;
2+
3+
import javax.swing.*;
4+
import java.awt.*;
5+
import java.awt.event.ActionEvent;
6+
import java.awt.event.ActionListener;
7+
import java.io.IOException;
8+
9+
class guiController extends JFrame {
10+
11+
private JPanel grid = new JPanel(new GridLayout(9, 9));
12+
private JPanel buttons = new JPanel(new GridLayout(2, 1));
13+
private JTextField[][] fieldArray = new JTextField[9][9];
14+
15+
private sudokuInitializer sud = new sudokuInitializer();
16+
private int flag;
17+
18+
guiController() {
19+
flag = chooseInputStream();
20+
guiInit();
21+
//sud.printArray();
22+
}
23+
24+
private int chooseInputStream() {
25+
String[] options = new String[]{"File", "Manual"};
26+
int response = JOptionPane.showOptionDialog(null, "Choose one way", "",
27+
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
28+
null, options, options[0]);
29+
System.out.println(response);
30+
if (response == 0) {
31+
try {
32+
sud.fileInput();
33+
} catch (IOException e) {
34+
e.printStackTrace();
35+
}
36+
}
37+
return response;
38+
}
39+
40+
private void guiInit() {
41+
for (int i = 0; i < 9; i++) {
42+
for (int j = 0; j < 9; j++) {
43+
fieldArray[i][j] = new JTextField(1);
44+
if (Main.sudoku_array[i][j] != 0) {
45+
fieldArray[i][j].setText(Main.sudoku_array[i][j] + "");
46+
fieldArray[i][j].setEditable(false);
47+
} else
48+
fieldArray[i][j].setText("");
49+
fieldArray[i][j].setFont(new Font("Arial", Font.ITALIC, 24));
50+
fieldArray[i][j].setHorizontalAlignment(JTextField.CENTER);
51+
52+
if (j == 2 || j == 5)
53+
fieldArray[i][j].setBorder(BorderFactory.createMatteBorder(1, 1, 1, 3, Color.BLACK));
54+
else
55+
fieldArray[i][j].setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
56+
57+
if (i == 2 || i == 5) {
58+
if (j == 2 || j == 5)
59+
fieldArray[i][j].setBorder(BorderFactory.createMatteBorder(1, 1, 3, 3, Color.BLACK));
60+
else
61+
fieldArray[i][j].setBorder(BorderFactory.createMatteBorder(1, 1, 3, 1, Color.BLACK));
62+
}
63+
grid.add(fieldArray[i][j]);
64+
}
65+
66+
}
67+
add(grid);
68+
JButton solve = new JButton("Solve");
69+
solve.setBorder(null);
70+
solve.setBackground(Color.white);
71+
buttons.add(solve);
72+
buttons.add(new JLabel());
73+
buttons.setBackground(Color.white);
74+
add(buttons, BorderLayout.SOUTH);
75+
solve.addActionListener(e -> {
76+
if (flag != 0)
77+
getText();
78+
sud.printArray();
79+
solve.setEnabled(false);
80+
});
81+
82+
setSize(400, 500);
83+
//setVisible(true);
84+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
85+
setLocationRelativeTo(null);
86+
}
87+
88+
private void getText() {
89+
String temp;
90+
for (int i = 0; i < 9; i++) {
91+
for (int j = 0; j < 9; j++) {
92+
temp = fieldArray[i][j].getText();
93+
if (!temp.equals("")) {
94+
Main.sudoku_array[i][j] = Integer.parseInt(temp);
95+
fieldArray[i][j].setEditable(false);
96+
}
97+
}
98+
}
99+
}
100+
101+
void refreshGUI(){
102+
for (int i = 0; i < 9; i++) {
103+
for (int j = 0; j < 9; j++) {
104+
if (Main.sudoku_array[i][j] != 0) {
105+
fieldArray[i][j].setText(Main.sudoku_array[i][j]+"");
106+
}
107+
}
108+
}
109+
}
110+
111+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.company;
2+
3+
import javax.swing.*;
4+
import java.io.BufferedReader;
5+
import java.io.File;
6+
import java.io.FileReader;
7+
import java.io.IOException;
8+
9+
class sudokuInitializer {
10+
void printArray(){
11+
System.out.println("Printing...");
12+
for (int i=0;i<9;i++) {
13+
System.out.println();
14+
for (int j = 0; j < 9; j++)
15+
System.out.print(Main.sudoku_array[i][j] + " ");
16+
}
17+
}
18+
void fileInput() throws IOException {
19+
System.out.println("opening Jfilechooser");
20+
//JFrame f = new JFrame("Choose a file");
21+
JFileChooser fileChooser = new JFileChooser("D:\\Windows\\Documents");
22+
int result = fileChooser.showOpenDialog(null);
23+
if (result == JFileChooser.APPROVE_OPTION) {
24+
File selectedFile = fileChooser.getSelectedFile();
25+
BufferedReader br = new BufferedReader(new FileReader(selectedFile));
26+
27+
StringBuilder st = new StringBuilder();
28+
String stt;
29+
int i=0;
30+
while (((stt = br.readLine())) != null){
31+
st.insert(0,stt);
32+
for (int j=0;j<st.length();j++){
33+
Main.sudoku_array[i][j] = Integer.parseInt(st.charAt(j)+"");
34+
//System.out.print(i+","+j+":"+Main.sudoku_array[i][j]+" ");
35+
}
36+
i++;
37+
//System.out.println();
38+
st.delete(0,st.length());
39+
}
40+
}
41+
}
42+
/*
43+
44+
//NOTE : Class only to be used in catastrophic situations only
45+
void manualInput(){
46+
String[] str = new String[9];
47+
Scanner input = new Scanner(System.in);
48+
int i=0;
49+
50+
StringBuilder st = new StringBuilder();
51+
for (int k = 0; k < 9 ; k++){
52+
str[k] = input.nextLine();
53+
st.insert(0,str[k]);
54+
for (int j=0;j<st.length();j++){
55+
Main.sudoku_array[i][j] = Integer.parseInt(st.charAt(j)+"");
56+
}
57+
i++;
58+
st.delete(0,st.length());
59+
}
60+
}
61+
62+
*/
63+
}

0 commit comments

Comments
 (0)