View Javadoc
1   /*
2    * Copyright (c) 2022 Kaiserpfalz EDV-Service, Roland T. Lichti.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16   */
17  
18  package de.kaiserpfalzedv.rpg.core.dice.bag;
19  
20  import de.kaiserpfalzedv.rpg.core.dice.Die;
21  import de.kaiserpfalzedv.rpg.core.dice.mat.DieResult;
22  
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.Objects;
26  import java.util.StringJoiner;
27  
28  /**
29   * GenericNumericDie -- Implements the die rolling for numeric die from 1 to {@link #max}.
30   *
31   * @author rlichti {@literal <rlichti@kaiserpfalz-edv.de>}
32   * @since 2020-08-12
33   */
34  public class GenericNumericDie implements Die {
35      /**
36       * The number of sides of the die.
37       */
38      public final int max;
39  
40      public GenericNumericDie(final int max) {
41          this.max = max;
42      }
43  
44      /**
45       * The die roll itself.
46       * @return the numeric result of the roll of this die.
47       */
48      protected int rollSingle() {
49          return (int) (Math.random( ) * max + 1);
50      }
51  
52  
53      @Override
54      public DieResult roll() {
55          String roll = Integer.toString(rollSingle(), 10);
56          return DieResult.builder()
57                  .die(this)
58                  .total(roll)
59                  .rolls(Collections.singletonList(roll).toArray(new String[0]))
60                  .build();
61      }
62  
63      @Override
64      public final DieResult[] roll(final int number) {
65          ArrayList<DieResult> results = new ArrayList<>(number);
66  
67          for(int i = 1; i <= number; i++) {
68              results.add(roll());
69          }
70  
71          return results.toArray(new DieResult[0]);
72      }
73  
74      @Override
75      public boolean isNumericDie() {
76          return true;
77      }
78  
79      @Override
80      public String getDieType() {
81          return "D" + max;
82      }
83  
84  
85      @Override
86      public final boolean equals(Object o) {
87          if (this == o) return true;
88          if (!(o instanceof GenericNumericDie)) return false;
89          GenericNumericDie basicDie = (GenericNumericDie) o;
90          return max == basicDie.max;
91      }
92  
93      @Override
94      public final int hashCode() {
95          return Objects.hash(max);
96      }
97  
98      @Override
99      public final String toString() {
100         return new StringJoiner(", ",
101                 getDieType() + "[",
102                 "]")
103                 .add("identity=" + System.identityHashCode(this))
104                 .toString();
105     }
106 }