forked from lgarron/min2phase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainProgram.java
330 lines (305 loc) · 12.2 KB
/
MainProgram.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package ui;
import cs.min2phase.Tools;
import cs.min2phase.Search;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.Color;
import java.awt.event.*;
import java.io.*;
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//A simple GUI example to demonstrate how to use the package org.kociemba.twophase
/**
* This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation, company or business for any purpose whatever) then
* you should purchase a license for each developer using Jigloo. Please visit www.cloudgarden.com for details. Use of
* Jigloo implies acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR THIS MACHINE, SO
* JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class MainProgram extends javax.swing.JFrame {
// +++++++++++++These variables used only in the GUI-interface+++++++++++++++++++++++++++++++++++++++++++++++++++++++
private static final long serialVersionUID = 1L;
private JButton[][] facelet = new JButton[6][9];
private final JButton[] colorSel = new JButton[6];
private final int FSIZE = 45;
private final int[] XOFF = { 3, 6, 3, 3, 0, 9 };// Offsets for facelet display
private final int[] YOFF = { 0, 3, 3, 6, 3, 3 };
private final Color[] COLORS = { Color.white, Color.red, Color.green, Color.yellow, Color.orange, Color.blue };
private JCheckBox checkBoxShowStr;
private JButton buttonRandom;
private JCheckBox checkBoxUseSep;
private JCheckBox checkBoxInv;
private JCheckBox checkBoxShowLen;
private JButton Solve;
private JLabel jLabel2;
private JLabel jLabel1;
private JSpinner spinnerMaxMoves;
private JSpinner spinnerTimeout;
private Color curCol = COLORS[0];
private int maxDepth = 21, maxTime = 5;
boolean useSeparator = true;
boolean showString = false;
boolean inverse = true;
boolean showLength = true;
Search search = new Search();
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public static void main(String[] args) {
try {
DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream("data")));
Tools.initFrom(dis);
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
if (!Tools.isInited()) {
try {
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("data")));
Tools.saveTo(dos);
dos.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainProgram inst = new MainProgram();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public MainProgram() {
super();
initGUI();
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
private void initGUI() {
getContentPane().setLayout(null);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setTitle("Two-Phase Package GUI-Example");
// ++++++++++++++++++++++++++++++++++ Set up Solve Cube Button ++++++++++++++++++++++++++++++++++++++++++++++++++++
Solve = new JButton("Solve Cube");
getContentPane().add(Solve);
Solve.setBounds(422, 64, 114, 48);
Solve.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
solveCube(evt);
}
});
// ++++++++++++++++++++++++++++++++++ Set up Move Limit Spinner +++++++++++++++++++++++++++++++++++++++++++++++++++
{
jLabel1 = new JLabel();
getContentPane().add(jLabel1);
jLabel1.setText("Move Limit");
jLabel1.setBounds(282, 65, 72, 16);
}
{
SpinnerModel model = new SpinnerNumberModel(21, 1, 24, 1);
spinnerMaxMoves = new JSpinner(model);
getContentPane().add(spinnerMaxMoves);
spinnerMaxMoves.setBounds(354, 62, 56, 21);
spinnerMaxMoves.getEditor().setPreferredSize(new java.awt.Dimension(37, 19));
spinnerMaxMoves.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent evt) {
maxDepth = ((Integer) spinnerMaxMoves.getValue()).intValue();
}
});
}
// ++++++++++++++++++++++++++++++++++ Set up Time Limit Spinner +++++++++++++++++++++++++++++++++++++++++++++++++++
{
jLabel2 = new JLabel();
getContentPane().add(jLabel2);
jLabel2.setText("Time Limit");
jLabel2.setBounds(282, 93, 72, 16);
}
{
SpinnerModel model = new SpinnerNumberModel(5, 1, 3600, 1);
spinnerTimeout = new JSpinner(model);
getContentPane().add(spinnerTimeout);
spinnerTimeout.setModel(model);
spinnerTimeout.setBounds(354, 90, 56, 21);
spinnerTimeout.getEditor().setPreferredSize(new java.awt.Dimension(36, 17));
spinnerTimeout.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent evt) {
maxTime = ((Integer) spinnerTimeout.getValue()).intValue();
}
});
}
// ++++++++++++++++++++++++++++++++++ Set up Use Separator CheckBox +++++++++++++++++++++++++++++++++++++++++++++++
{
checkBoxInv = new JCheckBox("Inverse", true);
getContentPane().add(checkBoxInv);
checkBoxInv.setBounds(12, 297, 121, 20);
checkBoxInv.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
inverse = checkBoxInv.isSelected();
}
});
checkBoxUseSep = new JCheckBox("Use Separator", true);
getContentPane().add(checkBoxUseSep);
checkBoxUseSep.setBounds(12, 320, 121, 20);
checkBoxUseSep.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
useSeparator = checkBoxUseSep.isSelected();
}
});
}
{
checkBoxShowStr = new JCheckBox("Show String", false);
getContentPane().add(checkBoxShowStr);
checkBoxShowStr.setBounds(12, 343, 121, 20);
checkBoxShowStr.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
showString = checkBoxShowStr.isSelected();
}
});
checkBoxShowLen = new JCheckBox("Show Length", true);
getContentPane().add(checkBoxShowLen);
checkBoxShowLen.setBounds(12, 366, 121, 20);
checkBoxShowLen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
showLength = checkBoxShowLen.isSelected();
}
});
}
// ++++++++++++++++++++++++++++++++++ Set up Random Button ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
buttonRandom = new JButton("Random Cube");
getContentPane().add(buttonRandom);
buttonRandom.setBounds(422, 17, 114, 22);
buttonRandom.setText("Scramble");
buttonRandom.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// +++++++++++++++++++++++++++++ Call Random function from package org.kociemba.twophase ++++++++++++++++++++
String r = Tools.randomCube();
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
for (int i = 0; i < 6; i++)
for (int j = 0; j < 9; j++) {
switch (r.charAt(9 * i + j)) {
case 'U':
facelet[i][j].setBackground(COLORS[0]);
break;
case 'R':
facelet[i][j].setBackground(COLORS[1]);
break;
case 'F':
facelet[i][j].setBackground(COLORS[2]);
break;
case 'D':
facelet[i][j].setBackground(COLORS[3]);
break;
case 'L':
facelet[i][j].setBackground(COLORS[4]);
break;
case 'B':
facelet[i][j].setBackground(COLORS[5]);
break;
}
}
}
});
}
// ++++++++++++++++++++++++++++++++++ Set up editable facelets ++++++++++++++++++++++++++++++++++++++++++++++++++++
for (int i = 0; i < 6; i++)
for (int j = 0; j < 9; j++) {
facelet[i][j] = new JButton();
getContentPane().add(facelet[i][j]);
facelet[i][j].setBackground(Color.gray);
facelet[i][j].setRolloverEnabled(false);
facelet[i][j].setOpaque(true);
facelet[i][j].setBounds(FSIZE * XOFF[i] + FSIZE * (j % 3), FSIZE * YOFF[i] + FSIZE * (j / 3), FSIZE, FSIZE);
facelet[i][j].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
((JButton) evt.getSource()).setBackground(curCol);
}
});
}
String[] txt = { "U", "R", "F", "D", "L", "B" };
for (int i = 0; i < 6; i++)
facelet[i][4].setText(txt[i]);
for (int i = 0; i < 6; i++) {
colorSel[i] = new JButton();
getContentPane().add(colorSel[i]);
colorSel[i].setBackground(COLORS[i]);
colorSel[i].setOpaque(true);
colorSel[i].setBounds(FSIZE * (XOFF[1] + 1) + FSIZE / 4 * 3 * i, FSIZE * (YOFF[3] + 1), FSIZE / 4 * 3,
FSIZE / 4 * 3);
colorSel[i].setName("" + i);
colorSel[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
curCol = COLORS[Integer.parseInt(((JButton) evt.getSource()).getName())];
}
});
}
pack();
this.setSize(556, 441);
}
// ++++++++++++++++++++++++++++++++++++ End initGUI +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++++++++++++++++++++++++++++ Generate cube from GUI-Input and solve it ++++++++++++++++++++++++++++++++++++++++
private void solveCube(ActionEvent evt) {
StringBuffer s = new StringBuffer(54);
for (int i = 0; i < 54; i++)
s.insert(i, 'B');// default initialization
for (int i = 0; i < 6; i++)
// read the 54 facelets
for (int j = 0; j < 9; j++) {
if (facelet[i][j].getBackground() == facelet[0][4].getBackground())
s.setCharAt(9 * i + j, 'U');
if (facelet[i][j].getBackground() == facelet[1][4].getBackground())
s.setCharAt(9 * i + j, 'R');
if (facelet[i][j].getBackground() == facelet[2][4].getBackground())
s.setCharAt(9 * i + j, 'F');
if (facelet[i][j].getBackground() == facelet[3][4].getBackground())
s.setCharAt(9 * i + j, 'D');
if (facelet[i][j].getBackground() == facelet[4][4].getBackground())
s.setCharAt(9 * i + j, 'L');
if (facelet[i][j].getBackground() == facelet[5][4].getBackground())
s.setCharAt(9 * i + j, 'B');
}
String cubeString = s.toString();
if (showString) {
JOptionPane.showMessageDialog(null, "Cube Definiton String: " + cubeString);
}
int mask = 0;
mask |= useSeparator ? Search.USE_SEPARATOR : 0;
mask |= inverse ? Search.INVERSE_SOLUTION : 0;
mask |= showLength ? Search.APPEND_LENGTH : 0;
long t = System.nanoTime();
// ++++++++++++++++++++++++ Call Search.solution method from package org.kociemba.twophase ++++++++++++++++++++++++
String result = search.solution(cubeString, maxDepth, maxTime << 10, 0, mask);
t = System.nanoTime() - t;
// +++++++++++++++++++ Replace the error messages with more meaningful ones in your language ++++++++++++++++++++++
if (result.contains("Error"))
switch (result.charAt(result.length() - 1)) {
case '1':
result = "There are not exactly nine facelets of each color!";
break;
case '2':
result = "Not all 12 edges exist exactly once!";
break;
case '3':
result = "Flip error: One edge has to be flipped!";
break;
case '4':
result = "Not all 8 corners exist exactly once!";
break;
case '5':
result = "Twist error: One corner has to be twisted!";
break;
case '6':
result = "Parity error: Two corners or two edges have to be exchanged!";
break;
case '7':
result = "No solution exists for the given maximum move number!";
break;
case '8':
result = "Timeout, no solution found within given maximum time!";
break;
}
JOptionPane.showMessageDialog(null, result, Double.toString((t/1000)/1000.0) + "ms", JOptionPane.INFORMATION_MESSAGE);
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
}
}