View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Test that "commons-rng-simple" can be used as a module in Java 9.
28   */
29  public final class DiceGameApplication {
30      /** Line separator. */
31      private static final String LINE_SEP = System.getProperty("line.separator");
32      /** Required functionality. */
33      private final DiceGame game;
34  
35      /**
36       * Application.
37       *
38       * @param numPlayers Number of players.
39       * @param numRounds Number of rounds per game.
40       * @param identifier RNG algorithm identifier.
41       * @param mu Mean rolls per round.
42       * @param sigma Standard deviation of rolls per round.
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                              mu, sigma);
52      }
53  
54      // Allow System.out and multiple " ---" strings
55      // CHECKSTYLE: stop RegexpCheck
56      // CHECKSTYLE: stop MultipleStringLiteralsCheck
57  
58      /**
59       * Application's entry point.
60       *
61       * @param args Arguments, in the following order
62       * <ol>
63       *  <li>Number of games</li>
64       *  <li>Number of players</li>
65       *  <li>Number of rounds per game</li>
66       *  <li>RNG {@link RandomSource identifier}</li>
67       *  <li>Mean rolls per round</li>
68       *  <li>Standard deviation of rolls per round</li>
69       * </ol>
70       */
71      public static void main(String[] args) {
72          final int numGames = Integer.parseInt(args[0]);
73          final DiceGameApplication app = new DiceGameApplication(Integer.parseInt(args[1]),
74                                                                  Integer.parseInt(args[2]),
75                                                                  RandomSource.valueOf(args[3]),
76                                                                  Double.parseDouble(args[4]),
77                                                                  Double.parseDouble(args[5]));
78  
79          app.displayModuleInfo();
80  
81          for (int i = 1; i <= numGames; i++) {
82              System.out.println("--- Game " + i + " ---");
83              System.out.println(display(app.game.play()));
84          }
85      }
86  
87      /**
88       * Display the list of players in decreasing order of scores.
89       *
90       * @param scores Scores returned by {@link #play()}.
91       * @return a text diplaying the result of the game.
92       */
93      private static String display(int[] scores) {
94          final int[][] a = new int[scores.length][2];
95          for (int i = 0; i < scores.length; i++) {
96              a[i][0] = i;
97              a[i][1] = scores[i];
98          }
99          Arrays.sort(a, Comparator.comparingInt(x -> -x[1]));
100 
101         final StringBuilder result = new StringBuilder(512);
102         for (int i = 0; i < scores.length; i++) {
103             result.append("Player ").append(a[i][0] + 1)
104                 .append(" has ").append(a[i][1])
105                 .append(" points").append(LINE_SEP);
106         }
107 
108         return result.toString();
109     }
110 
111     /**
112      * Display JPMS information.
113      */
114     private void displayModuleInfo() {
115         for (final Module mod : new Module[] {DiceGame.class.getModule(),
116                                               DiceGameApplication.class.getModule()}) {
117             System.out.println("--- " + mod + " ---");
118             final ModuleDescriptor desc = mod.getDescriptor();
119 
120             for (final ModuleDescriptor.Requires r : desc.requires()) {
121                 System.out.println(mod.getName() + " requires " + r.name());
122             }
123 
124             System.out.println();
125         }
126     }
127 }