1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.rng.examples.jpms.app;
19
20 import java.util.Arrays;
21 import java.util.Comparator;
22 import java.lang.module.ModuleDescriptor;
23 import org.apache.commons.rng.simple.RandomSource;
24 import org.apache.commons.rng.examples.jpms.lib.DiceGame;
25
26
27
28
29 public final class DiceGameApplication {
30
31 private static final String LINE_SEP = System.getProperty("line.separator");
32
33 private final DiceGame game;
34
35
36
37
38
39
40
41
42
43
44 private DiceGameApplication(int numPlayers,
45 int numRounds,
46 RandomSource identifier,
47 double mu,
48 double sigma) {
49 game = new DiceGame(numPlayers, numRounds,
50 identifier.create(),
51 4.3, 2.1);
52 }
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public static void main(String[] args) {
70 final int numGames = Integer.parseInt(args[0]);
71 final DiceGameApplication app = new DiceGameApplication(Integer.parseInt(args[1]),
72 Integer.parseInt(args[2]),
73 RandomSource.valueOf(args[3]),
74 Double.parseDouble(args[4]),
75 Double.parseDouble(args[5]));
76
77 app.displayModuleInfo();
78
79 for (int i = 1; i <= numGames; i++) {
80 System.out.println("--- Game " + i + " ---");
81 System.out.println(display(app.game.play()));
82 }
83 }
84
85
86
87
88
89
90
91 private static String display(int[] scores) {
92 final int[][] a = new int[scores.length][2];
93 for (int i = 0; i < scores.length; i++) {
94 a[i][0] = i;
95 a[i][1] = scores[i];
96 }
97 Arrays.sort(a, Comparator.comparingInt(x -> -x[1]));
98
99 final StringBuilder result = new StringBuilder(512);
100 for (int i = 0; i < scores.length; i++) {
101 result.append("Player ").append(a[i][0] + 1)
102 .append(" has ").append(a[i][1])
103 .append(" points").append(LINE_SEP);
104 }
105
106 return result.toString();
107 }
108
109
110
111
112 private void displayModuleInfo() {
113 for (final Module mod : new Module[] {DiceGame.class.getModule(),
114 DiceGameApplication.class.getModule()}) {
115 System.out.println("--- " + mod + " ---");
116 final ModuleDescriptor desc = mod.getDescriptor();
117
118 for (final ModuleDescriptor.Requires r : desc.requires()) {
119 System.out.println(mod.getName() + " requires " + r.name());
120 }
121
122 System.out.println();
123 }
124 }
125 }