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 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       */
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       * Application's entry point.
52       *
53       * @param args Arguments, in the following order
54       * <ol>
55       *  <li>Number of games</li>
56       *  <li>Number of players</li>
57       *  <li>Number of rounds per game</li>
58       *  <li>RNG {@link RandomSource indentifier}</li>
59       * </ol>
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       * Display the list of players in decreasing order of scores.
77       *
78       * @param scores Scores returned by {@link #play()}.
79       * @return a text diplaying the result of the game.
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      * Display JPMS information.
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 }