-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI.java
208 lines (156 loc) · 5.11 KB
/
GUI.java
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package inlämning;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Label;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GUI {
private Sudoku soduko;
private JTextField[][] a;
/*
* Loads the graphical interface
*/
public GUI() {
soduko = new Sudoku();
a = new JTextField[9][9];
// Code for the interface
JFrame frame = new JFrame("Soduko");
frame.setSize(300, 300);
JPanel boardPanel = new JPanel(new GridLayout(9,9));
frame.add(boardPanel, BorderLayout.CENTER);
for (int i = 0; i < 9; i++) { // Creates the squares for the board
for (int k = 0; k < 9; k++) {
a[i][k]= new JTextField("");
boardPanel.add(a[i][k]);
}
}
JPanel buttonPanel = new JPanel(new GridLayout(1,2));
frame.add(buttonPanel, BorderLayout.SOUTH);
JButton clearButton = new JButton("Clear");
JButton solveButton = new JButton("Solve");
buttonPanel.add(clearButton);
buttonPanel.add(solveButton);
// Code for the interface END.
clear(); // Clears the board from numbers.
loadColors(a); // Loads the colors
frame.setVisible(true); // Makes everything visible.
// Code for Button Press
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
clear();
loadColors(a);
popupWindow("Cleared");
}
});
solveButton.addActionListener(new ActionListener() { // adds a listener. This happens on button press.
public void actionPerformed(ActionEvent arg0) {
if (insertSoduko(a) == false) {
popupWindow("invalid input");
return; // Completes the button press.
}
for (int i = 0; i < 9; i++) { // If
for (int k = 0; k < 9; k++) {
if (a[i][k].getText().isEmpty()) continue;
if (soduko.check(i, k, Integer.parseInt(a[i][k].getText())) == false) { // a quicker way to check if the user for example inserted two 5s in the first row
popupWindow("Can't be solved");
return;
}
}
}
if (soduko.solve() == true) {
popupWindow("solved");
loadColors(a); // If a square is red it will return to its normal color
soduko.print(a);
}
else {
popupWindow("Can't be solved");
}
}
});
// Code for button press
}
/*
* Clears the graphical board
*/
public void clear() {
for (int i = 0; i < 9; i++) {
for(int k = 0; k < 9; k++) {
a[i][k].setText("");
}
}
insertSoduko(a);
}
/* Loads the colors of the graphical board
* @param a is the graphical board to load colors on
*/
public void loadColors(JTextField[][] a) {
for (int x = 0; x < 9; x++) {
for (int y = 0; y < 9; y++) {
if(y==0 && x==3 || y==0 && x==4 || y==0 && x==5 || y==1 && x==3 || y==1 && x==4 || y==1 && x==5 ||
y==2 && x==3 || y==2 && x==4 || y==2 && x==5) {
a[x][y].setBackground(Color.LIGHT_GRAY);
} else if(y==3 && x==0 || y==3 && x==1 || y==3 && x==2 || y==4 && x==0 || y==4 && x==1 || y==4 && x==2 ||
y==5 && x==0 || y==5 && x==1 || y==5 && x==2) {
a[x][y].setBackground(Color.LIGHT_GRAY);
}else if(y==3 && x==6 || y==3 && x==7 || y==3 && x==8 || y==4 && x==6 || y==4 && x==7 || y==4 && x==8 ||
y==5 && x==6 || y==5 && x==7 || y==5 && x==8) {
a[x][y].setBackground(Color.LIGHT_GRAY);
}else if(y==6 && x==3 || y==6 && x==4 || y==6 && x==5 || y==7 && x==3 || y==7 && x==4 || y==7 && x==5 ||
y==8 && x==3 || y==8 && x==4 || y==8 && x==5) {
a[x][y].setBackground(Color.LIGHT_GRAY);
}
else {
a[x][y].setBackground(Color.WHITE);
}
}
}
}
/* Creates a popup window
* @param text is the text to be displayed in the window.
*/
public void popupWindow(String text) {
JFrame frame = new JFrame("Soduko");
JOptionPane.showMessageDialog(frame, text);
}
public static void main(String[] args) {
new GUI();
}
/* inserts values placed in the board
* @return false if the square contains an out of range number or a symbol.
*/
public boolean insertSoduko(JTextField[][] a) {
boolean InsertSuccess = true;
for (int row = 0; row < 9; row ++) {
for (int col = 0; col < 9; col ++) {
if (a[row][col].getText().isEmpty()) {
soduko.set(row,col,0);
continue;
}
int value = 0;
try { // Tries to parseInt. parseInt returns exception if the arguement isn't a number.
value = Integer.parseInt(a[row][col].getText());
soduko.set(row,col,value);
} catch(NumberFormatException e) { // If there is a letter or symbol in the soduko.
InsertSuccess = false;
a[row][col].setBackground(Color.RED); // Mark red
}
if (value < 1 || value > 9) { // Value can only be from 1 to 9
InsertSuccess = false;
a[row][col].setBackground(Color.RED);
}
}
}
return InsertSuccess;
}
}