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                              4.3, 2.1);
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       * </ol>
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       * Display the list of players in decreasing order of scores.
87       *
88       * @param scores Scores returned by {@link #play()}.
89       * @return a text diplaying the result of the game.
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      * Display JPMS information.
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 }