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 private DiceGameApplication(int numPlayers,
43 int numRounds,
44 RandomSource identifier) {
45 game = new DiceGame(numPlayers, numRounds,
46 RandomSource.create(identifier),
47 4.3, 2.1);
48 }
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public static void main(String[] args) {
66 final int numGames = Integer.parseInt(args[0]);
67 final DiceGameApplication app = new DiceGameApplication(Integer.parseInt(args[1]),
68 Integer.parseInt(args[2]),
69 RandomSource.valueOf(args[3]));
70
71 app.displayModuleInfo();
72
73 for (int i = 1; i <= numGames; i++) {
74 System.out.println("--- Game " + i + " ---");
75 System.out.println(display(app.game.play()));
76 }
77 }
78
79
80
81
82
83
84
85 private static String display(int[] scores) {
86 final int[][] a = new int[scores.length][2];
87 for (int i = 0; i < scores.length; i++) {
88 a[i][0] = i;
89 a[i][1] = scores[i];
90 }
91 Arrays.sort(a, Comparator.comparingInt(x -> -x[1]));
92
93 final StringBuilder result = new StringBuilder();
94 for (int i = 0; i < scores.length; i++) {
95 result.append("Player ").append(a[i][0] + 1)
96 .append(" has ").append(a[i][1])
97 .append(" points").append(LINE_SEP);
98 }
99
100 return result.toString();
101 }
102
103
104
105
106 private void displayModuleInfo() {
107 final StringBuilder str = new StringBuilder();
108
109 for (Module mod : new Module[] {DiceGame.class.getModule(),
110 DiceGameApplication.class.getModule()}) {
111 System.out.println("--- " + mod + " ---");
112 final ModuleDescriptor desc = mod.getDescriptor();
113
114 for (ModuleDescriptor.Requires r : desc.requires()) {
115 System.out.println(mod.getName() + " requires " + r.name());
116 }
117
118 System.out.println();
119 }
120 }
121 }