forked from graalvm/game-of-life-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
65 lines (55 loc) · 2.74 KB
/
Main.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
package gameoflife;
import java.io.IOException;
import java.util.function.Consumer;
public class Main {
record Args(
String patternFile,
int maxWindowWidth,
int maxWindowHeight,
int periodMilliseconds,
int leftPadding,
int topPadding,
int rightPadding,
int bottomPadding,
boolean rotate,
boolean logRate) {
static Args parse(String[] args) {
return new Args(
args.length > 0 ? args[0] : "patterns/gosper_glider_gun.txt",
args.length > 1 ? Integer.parseInt(args[1]) : 640,
args.length > 2 ? Integer.parseInt(args[2]) : 480,
args.length > 3 ? Integer.parseInt(args[3]) : 20,
args.length > 4 ? Integer.parseInt(args[4]) : 10,
args.length > 5 ? Integer.parseInt(args[5]) : 10,
args.length > 6 ? Integer.parseInt(args[6]) : 40,
args.length > 7 ? Integer.parseInt(args[7]) : 40,
args.length > 8 ? Boolean.parseBoolean(args[8]) : false,
args.length > 9 ? Boolean.parseBoolean(args[9]) : false);
}
}
public static void main(String[] args) throws IOException {
Args a = Args.parse(args);
boolean[][] original = PatternParser.parseFile(a.patternFile);
boolean[][] rotated = a.rotate ? PatternParser.rotate(original) : original;
boolean[][] pattern = PatternParser.pad(rotated, a.leftPadding, a.topPadding, a.rightPadding, a.bottomPadding);
Channel<boolean[][]> gridChannel = new Channel<>(); // channel carries aggregated liveness matrices
Dimensions dimensions = new Dimensions(pattern.length, pattern[0].length);
GameOfLife game = new GameOfLife(dimensions, pattern, a.periodMilliseconds, gridChannel);
game.start();
double scale = calculateScale(dimensions.rows(), dimensions.cols(), a.maxWindowWidth, a.maxWindowHeight);
var width = (int) (scale * dimensions.cols());
var height = (int) (scale * dimensions.rows());
System.out.printf("rows=%d, columns=%d, width=%d, height=%d\n", dimensions.rows(), dimensions.cols(), width, height);
Consumer<boolean[][]> consumer = new CountingOutput();
while (true) {
consumer.accept(gridChannel.take());
}
}
private static double calculateScale(int rows, int cols, int maxWindowWidth, int maxWindowHeight) {
double aspect = (double) maxWindowWidth / maxWindowHeight;
double actual = (double) cols / rows;
return actual < aspect
? (double) maxWindowHeight / rows
: (double) maxWindowWidth / cols;
}
}