Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
138 lines (111 loc) · 4.64 KB
/
Copy pathMain.java
File metadata and controls
138 lines (111 loc) · 4.64 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.JFrame;
import java.awt.*;
public class Main {
public static CubeUI gamePanel;
// public static Cube cube;
public static Cube cube;
public static CubeSolver solver;
public static Heuristic heuristic;
public static int maxDepth = 10;
public static void main(String[] args) {
// cube = new Cube();
// gamePanel = new CubeUI();
// cube.performMoves(" "); // example scramble
cube = new Cube();
gamePanel = new CubeUI();
cube.performMoves("F2 D2 U' R2 U F2 D2 U' R2 U' B' L2 R' B' D2 U B2");
// cube.performMoves("U2 B2 F R2 B L2 R2 D2 B'");
}
public static void solve() {
new Thread(() -> {
solver = new CubeSolver();
System.out.println("Starting to solve the cube...");
long startTime = System.currentTimeMillis();
boolean solved = solver.solveCube(cube, maxDepth);
// Update UI and print results on the EDT
javax.swing.SwingUtilities.invokeLater(() -> CubeUI.refresh());
if (solved) {
solved = solver.solvePhase2(cube, maxDepth);
}
long endTime = System.currentTimeMillis();
String timeStr = formatTime(endTime - startTime);
long states = solver.getStateCounter();
String moves = solver.getMoves();
System.out.print("Solving took " + timeStr);
if (solved) {
System.out.println("Total states explored: " + formatNumber(states));
System.out.println("Moves taken: " + moves);
} else {
System.out.println("Could not solve the cube within the given depth.");
}
System.out.println();
CubeUI.refresh();
}, "solver-thread").start();
}
private static String formatNumber(long number) {
long abs = Math.abs(number);
if (abs < 1000) {
return Long.toString(number);
}
// exp: 1 -> K, 2 -> M, 3 -> B, 4 -> T
int exp = (int) (Math.log10(abs) / 3);
if (exp < 1) exp = 1;
if (exp > 4) exp = 4; // cap at T (trillion)
char[] suffixes = {'K', 'M', 'B', 'T'};
char suffix = suffixes[exp - 1];
double scaled = (double) number / Math.pow(1000, exp);
String num = String.format("%.1f", scaled);
if (num.endsWith(".0")) {
num = num.substring(0, num.length() - 2);
}
return num + suffix;
}
private static String formatTime(long timeInMillis) {
long totalSeconds = timeInMillis / 1000;
long minutes = totalSeconds / 60;
long seconds = totalSeconds % 60;
long millis = timeInMillis % 1000;
if (timeInMillis < 1000) {
return String.format("Time taken: %d ms%n", millis);
} else if (timeInMillis < 60_000) {
return String.format("Time taken: %d sec, %d ms%n", totalSeconds, millis);
} else {
return String.format("Time taken: %d min, %d sec, %d ms%n", minutes, seconds, millis);
}
}
public static void refreshUI() {
gamePanel.refresh();
}
public static void reset() {
}
// ========================================
// AI GENERATED CODE - solve3D method
// Purpose: Solve cube with messages going to MessageLog for 3D UI
// ========================================
public static void solve3D() {
new Thread(() -> {
solver = new CubeSolver();
MessageLog.getInstance().logSolver("Starting to solve the cube...");
long startTime = System.currentTimeMillis();
boolean solved = solver.solveCube(cube, maxDepth);
// Update UI
javax.swing.SwingUtilities.invokeLater(() -> CubeUI3D.refresh());
if (solved) {
solved = solver.solvePhase2(cube, maxDepth);
}
long endTime = System.currentTimeMillis();
String timeStr = formatTime(endTime - startTime);
long states = solver.getStateCounter();
String moves = solver.getMoves();
// Log to MessageLog instead of System.out
MessageLog.getInstance().logSolver(timeStr.replace("Time taken: ", "").replace("\n", ""));
if (solved) {
MessageLog.getInstance().logSuccess("States explored: " + formatNumber(states));
MessageLog.getInstance().logSuccess("Solution: " + moves);
} else {
MessageLog.getInstance().logError("Could not solve within depth " + maxDepth);
}
CubeUI3D.refresh();
}, "solver-thread-3d").start();
}
}