-
Notifications
You must be signed in to change notification settings - Fork 1
/
rubik_solver.java
161 lines (152 loc) · 4.85 KB
/
rubik_solver.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
import java.awt.BorderLayout;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import lejos.nxt.remote.NXTCommand;
import lejos.pc.comm.NXTComm;
import lejos.pc.comm.NXTCommException;
import lejos.pc.comm.NXTCommFactory;
import lejos.pc.comm.NXTInfo;
import org.kociemba.twophase.Search;
public class TiltedTwister {
private static final byte INBOX = 11;
private static final byte OUTBOX = 5;
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame();
final JTextArea area = new JTextArea();
area.setEditable(false);
frame.add(area);
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// Add a scrolling text area
area.setRows(20);
frame.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER);
PipedOutputStream os = new PipedOutputStream();
System.setOut(new PrintStream(os));
final BufferedReader reader = new BufferedReader(
new InputStreamReader(new PipedInputStream(os)));
new Thread(new Runnable() {
@Override
public void run() {
try {
for ( int c = -1; (c = reader.read()) != -1; ) {
area.append(Character.toString((char)c));
area.setCaretPosition( area.getText().length() );
}
} catch (IOException ioe) {
}
}
}).start();
System.out.println("Two-Phase Kociemba Algorithm");
System.out.println("Initiating bluetooth...");
NXTInfo[] nxtInfo = null;
try {
nxtInfo = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH).search(null, NXTCommFactory.BLUETOOTH);
} catch (NXTCommException e1) {
}
int connected=0;
NXTComm[] nxtComm=new NXTComm[nxtInfo.length];
NXTCommand[] nxtCommand = new NXTCommand[nxtInfo.length];
for(int i=0;i<nxtInfo.length;i++)
{
try {
nxtComm[i]=NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
nxtCommand[i]=new NXTCommand();
nxtComm[i].open(nxtInfo[i]);
nxtCommand[i].setNXTComm(nxtComm[i]);
connected++;
} catch (NXTCommException e) {
}
}
if(connected==0)
{
System.out.println("No brick connected. Aborting...");
System.out.println("Good bye");
}
else
{
for(int i=0;i<nxtInfo.length;i++)
{
System.out.print(nxtInfo[i].name + " (" + nxtInfo[i].deviceAddress + ") ");
if(nxtCommand[i].isOpen())
System.out.println("connected");
else
System.out.println("not connected");
}
System.out.println("Initiating solver...");
Search.solution("UUUUUURRRDRRDRRDRRFFFFFFFFFLLLDDDDDDLLULLULLUBBBBBBBBB", 30, 60, false);
System.out.println("Ready");
while(true){
for(int i=0;i<nxtCommand.length;i++)
if(nxtCommand[i].isOpen())
{
try {
byte[] message = null;
message = nxtCommand[i].messageRead(INBOX,(byte)0,true);
if(message.length==55)
{
System.out.println("Request from " + nxtInfo[i].name + " (" + nxtInfo[i].deviceAddress + ") ");
String msg = new String(message).substring(0,54);
//Order of faces is different
String cube=msg.substring(4*9,4*9+9)+msg.substring(2*9,2*9+9)+msg.substring(1*9,1*9+9)+msg.substring(5*9,5*9+9)+msg.substring(0*9,0*9+9)+msg.substring(3*9,3*9+9);
System.out.println("Cube = " + cube);
System.out.println("Searching solution...");
String solution = Search.solution(cube, 30, 30, false);
String response = "";
if(solution.contains("Error"))
{
if(solution.charAt(solution.length() - 1)=='8')
System.out.println("Timeout, no solution found within maximum time!");
else
System.out.println("Invalid cube, probably due to error in color scanning");
response="ERROR";
}
else
{
System.out.println("Solution = " + solution);
for(int pos=0;pos<solution.length();pos++)
{
switch(solution.charAt(pos))
{
case 'L':
case 'F':
case 'R':
case 'B':
case 'U':
case 'D':
{
response+=solution.charAt(pos);
break;
}
case '2':
{
response+=solution.charAt(pos-1);
break;
}
case '\'':
{
response+=solution.charAt(pos-1);
response+=solution.charAt(pos-1);
break;
}
}
}
}
response += '\u0000';
nxtCommand[i].messageWrite(response.getBytes(), OUTBOX);
System.out.println("Ready");
}
} catch (IOException e) {
}
}
}
}
}
}