View Javadoc

1   /***
2    * Redistribution  and use  in source  and binary  forms, with  or without
3    * modification, are permitted provided  that the following conditions are
4    * met :
5    *
6    * . Redistributions  of  source  code  must  retain  the  above copyright
7    *   notice, this list of conditions and the following disclaimer.
8    *
9    * . Redistributions in  binary form  must reproduce  the above  copyright
10   *   notice, this list of conditions  and the following disclaimer in  the
11   *   documentation and/or other materials provided with the distribution.
12   *
13   * . The name of the author may not be used to endorse or promote products
14   *   derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS  PROVIDED BY THE  AUTHOR ``AS IS''  AND ANY EXPRESS  OR
17   * IMPLIED  WARRANTIES,  INCLUDING,  BUT   NOT  LIMITED  TO,  THE   IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE ARE
19   * DISCLAIMED.  IN NO  EVENT SHALL  THE AUTHOR  BE LIABLE  FOR ANY  DIRECT,
20   * INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES
21   * (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT OF  SUBSTITUTE  GOODS OR
22   * SERVICES;  LOSS  OF USE,  DATA,  OR PROFITS;  OR  BUSINESS INTERRUPTION)
23   * HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN  CONTRACT,
24   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25   * ANY  WAY  OUT OF  THE  USE OF  THIS  SOFTWARE, EVEN  IF  ADVISED OF  THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   *
28   * $Id: Dice.java,v 1.4 2005/07/15 11:20:08 mat007 Exp $
29   */
30  
31  package jtge.util.random;
32  
33  /***
34   * Emulates a dice.
35   * <p>
36   * A dice is defined by :
37   * <ul>
38   * <li>the type of dice (ie the range of possible values)
39   * <li>the number of dices to roll
40   * <li>the bonus added to the result
41   * </ul>
42   * <p>
43   * Example : 2d8+3 stands for 2 dices of type 8 with a bonus of 3.
44   *
45   * @author Jean-Laurent
46   * @version $Revision: 1.4 $ $Date: 2005/07/15 11:20:08 $
47   */
48  public class Dice
49  // TODO handle the minus symbol for the bonus
50  // FIXME should be able to combine several dices in an expression
51  {
52      /***
53       * Number of dices to throw.
54       */
55      private final int number;
56      /***
57       * Maximum number of each dice.
58       */
59      private final int type;
60      /***
61       * Bonus (or malus) to apply on the result.
62       */
63      private final int bonus;
64  
65      /***
66       * Create a dice out of the number of dices, the type and the bonus value.
67       * <p>
68       * Number and type must be positive.
69       *
70       * @param number the number of rolls
71       * @param type the type of dice
72       * @param bonus the number added to the result
73       */
74      public Dice( final int number, final int type, final int bonus )
75      {
76          if( number < 0 )
77              throw new IllegalArgumentException( "parameter 'number' must be positive (is " + number + ")" );
78          if( type < 0 )
79              throw new IllegalArgumentException( "parameter 'type' must be positive (is " + type + ")" );
80          this.number = number;
81          this.type = type;
82          this.bonus = bonus;
83      }
84  
85      /***
86       * Create a dice out of the number of dices and a type (e.g. no bonus).
87       * <p>
88       * Number and type must be positive.
89       *
90       * @param number the number of rolls
91       * @param type the type of dice
92       */
93      public Dice( final int number, final int type )
94      {
95          this( number, type, 0 );
96      }
97  
98      /***
99       * Create a dice from a textual definition.
100      * <p>
101      * The expected format is : <i>{diceNumber}</i><b>d</b><i>{diceType}</i><b>+</b><i>{bonus}</i>.
102      *
103      * @param definition the definition.
104      */
105     public Dice( final String definition )
106     {
107         // FIXME wrap parseInt NumberFormatException
108         int diceIndex = definition.indexOf( 'd' );
109         if( diceIndex == -1 )
110             throw new RuntimeException( "wrong dice definition: " + definition );
111         this.number = Integer.parseInt( definition.substring( 0, diceIndex ) );
112         // FIXME : maybe we should change this to a less aggressive exception e.g. DiceException
113         int plusIndex = definition.indexOf( '+' );
114         if( plusIndex != -1 )
115         {
116             this.type = Integer.parseInt( definition.substring( diceIndex + 1, plusIndex ) );
117             this.bonus = Integer.parseInt( definition.substring( plusIndex + 1, definition.length() ) );
118         }
119         else
120         {
121             this.type = Integer.parseInt( definition.substring( diceIndex + 1, definition.length() ) );
122             this.bonus = 0;
123         }
124     }
125 
126     /***
127      * Roll the dice.
128      *
129      * @return the result
130      */
131     public final int roll()
132     {
133         int result = 0;
134         for( int i = 0; i < number; i++ )
135             result += getRandom( type ) + 1;
136         return result + bonus;
137     }
138 
139     private int getRandom( final int maxVal ) // FIXME: MUST BE CHANGED to use a jtge.util.Random object instead.
140     {
141         return (int)Math.floor( (Math.random() * maxVal) % maxVal );
142     }
143 
144     /***
145      * {@inheritDoc}
146      */
147     public final String toString()
148     {
149         String result;
150         if( bonus > 0 )
151             result = number + "d" + type + "+" + bonus;
152         else if( bonus < 0 )
153             result = number + "d" + type + bonus;
154         else
155             result = number + "d" + type;
156         return result;
157     }
158 }