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