-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithmRunner.java
More file actions
120 lines (104 loc) · 4.41 KB
/
Copy pathAlgorithmRunner.java
File metadata and controls
120 lines (104 loc) · 4.41 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
import java.awt.image.BufferedImage;
import javax.swing.SwingUtilities;
public class AlgorithmRunner implements Runnable {
private static int width = 50;
private static int height = 30;
private static int shapeSize = 20;
private static Type shapeType = Type.SQUARE; // 4: square, 6: hexagon, 3: triangle, (add to enum new Type accordingly)
private static int time = 3;
private static boolean test;
private LabyrinthAlgorithm labyrinthAlgorithm;
private LabyrinthDrawer labyrinthDrawer;
private Thread thread;
private int dimension;
public static void main(String[] args) {
for (int i = 0; i < args.length; i += 2) {
String value = "?";
switch (args[i].toLowerCase()) {
case "-test":
i--;
test = true;
break;
case "-width":
try {
value = args[i + 1];
width = Integer.parseInt(value);
} catch (Exception e) {
System.out.println("Error: value \"" + value + "\" not valid for propertie -width");
}
break;
case "-height":
try {
value = args[i + 1];
height = Integer.parseInt(value);
} catch (Exception e) {
System.out.println("Error: value \"" + value + "\" not valid for propertie -height");
}
break;
case "-shapesize":
try {
value = args[i + 1];
shapeSize = Integer.parseInt(value);
} catch (Exception e) {
System.out.println("Error: value \"" + value + "\" not valid for propertie -shapesize");
}
break;
case "-shapetype":
try {
value = args[i + 1];
switch (value.toLowerCase()) {
case "square" -> shapeType = Type.SQUARE;
case "hexagon" -> shapeType = Type.HEXAGON;
case "triangle" -> shapeType = Type.TRIANGLE;
default -> new Exception();
};
} catch (Exception e) {
System.out.println("Error: value \"" + value + "\" not valid for propertie -shapetype");
}
break;
case "-time":
try {
value = args[i + 1];
time = Integer.parseInt(value);
} catch (Exception e) {
System.out.println("Error: value \"" + value + "\" not valid for propertie -time");
}
break;
default:
System.out.println("Error: propertie \"" + args[i] + "\" not found");
break;
}
}
if (test)
time = 0;
SwingUtilities.invokeLater(() -> new AlgorithmRunner());
}
public AlgorithmRunner() {
this.dimension = width * height;
BufferedImage buffer = new BufferedImage(1920, 1080, BufferedImage.TYPE_INT_ARGB);
LabyrinthGenerator generator = new LabyrinthGenerator(width, height, shapeSize, shapeType, buffer);
Shape[][] labyrinth = generator.getLabyrinth();
System.out.println(width + "x" + height + " labyrinth generated.");
this.labyrinthAlgorithm = new LabyrinthAlgorithm(labyrinth, buffer);
if (!test)
this.labyrinthDrawer = new LabyrinthDrawer(buffer);
this.thread = new Thread(this);
this.thread.start();
}
@Override
public void run() {
long startTime = System.nanoTime();
while (labyrinthAlgorithm.running()) {
labyrinthAlgorithm.update(test);
if (!test)
labyrinthDrawer.repaint();
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
long endTime = System.nanoTime();
System.out.println("Number of shapes: " + this.dimension + ", completed in " + (endTime - startTime) / 1000_000.0 + " ms.");
}
}