-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI_ss.java
More file actions
138 lines (129 loc) · 4.95 KB
/
GUI_ss.java
File metadata and controls
138 lines (129 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;9
public class GUI_ss extends SudokuSolver {
private JFrame frame;
private JButton b1;
private JButton b2;
private JButton b3;
private JTextField[] fields;
public GUI_ss() {
frame = new JFrame();
frame.setPreferredSize(new Dimension(800, 600));
JPanel panel = new JPanel();
b1 = new JButton("Solve");
b1.addActionListener(e -> {
// Calling a function that takes the inputted value from the UI and return calls more
// methods to solve and update the UI with solved values
GUIToSudoku(fields);
});
b2 = new JButton("Clear grid");
b2.addActionListener(e -> {
// clearing all JTextFields by setting them ""
for (int i=0; i<81; i++) {
fields[i].setText("");
fields[i].setBackground(Color.YELLOW);
fields[i].setForeground(Color.BLACK);
}
});
b3 = new JButton("Exit");
b3.addActionListener(e -> {
// disposing the frame
frame.dispose();
System.out.println("Exited");
});
fields = getClearedFields();
panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
panel.setLayout(new GridLayout(10, 3)); //imported awt module for this
for (int x=0 ; x<81 ; x++) {
panel.add(fields[x]);
}
panel.add(b1);
panel.add(b2);
panel.add(b3);
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Sudoku Solver");
frame.pack();
frame.setVisible(true);
}
JTextField[] getClearedFields() {
JTextField[] newFields = new JTextField[81];
for (int x = 0 ; x < 81 ; x++){
newFields[x] = new JTextField();
JTextField f = newFields[x];
f.setHorizontalAlignment(JTextField.CENTER);
f.setBackground(Color.YELLOW);
f.setEditable(true);
Border border = BorderFactory.createLineBorder(Color.BLACK, 1);
f.setBorder(border);
f.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke) {
char pressedKey = ke.getKeyChar();
if (pressedKey == 8) {
//backspace
f.setText("");
f.setBackground(Color.LIGHT_GRAY);
} else if ((pressedKey >= '1' && pressedKey <= '9')) {
// after setting text we set editable false as automatically key listener would also add the value
// as soon as key is released which will lead to repeated value
f.setText(""+pressedKey);
f.setEditable(false);
f.setBackground(Color.GRAY);
} else {
f.setEditable(false);
}
}
});
// finally we set editable back to true
f.setEditable(true);
}
return newFields;
}
void populateFieldInteractive(JTextField f, int numberToUpdate, int initial) {
f.setEditable(true);
if (initial == 0) {
f.setForeground(Color.BLUE);
} else {
f.setForeground(Color.BLACK);
}
f.setText(""+numberToUpdate);
f.setEditable(false);
}
void SudokuToGUI(int[][] solution, int[][] initialSudoku){
for (int i=0; i<9; i++) {
for (int j=0; j<9; j++) {
populateFieldInteractive(fields[(i*9) + j], solution[i][j], initialSudoku[i][j]);
}
}
}
void copySudoku(int[][] originalSudoku, int[][] copiedSudoku) {
for(int i=0; i<9; i++){
for(int j=0; j<9; j++){
copiedSudoku[i][j] = originalSudoku[i][j];
}
}
}
void GUIToSudoku(JTextField[] fields){
SudokuSolver inputPuzzle = new SudokuSolver();
for(int i=0; i<9; i++){
for(int j=0; j<9; j++){
String val = fields[9 * i + j].getText();
if (val.equals("")) {
inputPuzzle.sudoku[i][j] = 0;
} else {
inputPuzzle.sudoku[i][j] = Integer.parseInt(val);
}
}
}
if (Solver.isValidcheck(inputPuzzle)){
int[][] initialSudoku = new int[9][9];
copySudoku(inputPuzzle.sudoku, initialSudoku);
int[][] solution = new int[9][9];
Solver.solveAndGetFirstSolution(inputPuzzle, 0, 0, solution);
SudokuToGUI(solution, initialSudoku);
}
}
}