My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* hexagonlib - Multi-Purpose ActionScript 3 Library.
* __ __
* __/ \__/ \__ __
* / \__/HEXAGON \__/ \
* \__/ \__/ LIBRARY _/
* \__/ \__/
*
* Licensed under the MIT License
*
* Copyright (c) 2007-2008 Sascha Balkau / Hexagon Star Softworks
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.hexagonstar.util
{
/**
* Provides a set of methods for rolling multi-sided dice.
*
* @author Sascha Balkau
* @version 1.0.0
*/
public class Dice
{
////////////////////////////////////////////////////////////////////////////////////////
// Public Methods //
////////////////////////////////////////////////////////////////////////////////////////

/**
* Rolls a specified amount of four sided dice.
*
* @param amount The amount of dice to roll.
* @return The rolled die.
*/
public static function fourSided(amount:int = 1):int
{
return roll(4, amount);
}


/**
* Rolls a specified amount of six sided dice.
*
* @param amount The amount of dice to roll.
* @return The rolled die.
*/
public static function sixSided(amount:int = 1):int
{
return roll(6, amount);
}


/**
* Rolls a specified amount of eight sided dice.
*
* @param amount The amount of dice to roll.
* @return The rolled die.
*/
public static function eightSided(amount:int = 1):int
{
return roll(8, amount);
}


/**
* Rolls a specified amount of ten sided dice.
*
* @param amount The amount of dice to roll.
* @return The rolled die.
*/
public static function tenSided(amount:int = 1):int
{
return roll(10, amount);
}


/**
* Rolls a specified amount of twelve sided dice.
*
* @param amount The amount of dice to roll.
* @return The rolled die.
*/
public static function twelveSided(amount:int = 1):int
{
return roll(12, amount);
}


/**
* Rolls a specified amount of twenty sided dice.
*
* @param amount The amount of dice to roll.
* @return The rolled die.
*/
public static function twentySided(amount:int = 1):int
{
return roll(20, amount);
}


/**
* Rolls a percentile dice (1% to 100%). This method rolls a percentile dice
* like it is rolled with two realistic ten sided dice where both dice specify
* a digit for the percentage resulting in 0 to 99 + 1.
*
* @return The rolled die.
*/
public static function percentile():int
{
var d1:String = (tenSided() - 1).toString();
var d2:String = (tenSided() - 1).toString();
return parseInt(d1 + d2) + 1;
}


/**
* Rolls a random boolean result based on the chance value. Returns true or false
* based on the chance value (default 50%). For example if you wanted a player to
* have a 30% chance of getting a bonus, call chance(30) - true means the chance
* passed, false means it failed.
*
* @param chanceVal The chance of receiving the value. Should be given as a int
* between 0 and 100 (effectively 0% to 100%).
* @return true if the roll passed, or false if not.
*/
public static function chance(chanceVal:int = 50):Boolean
{
if (chanceVal <= 0)
{
return false;
}
else if (chanceVal >= 100)
{
return true;
}
else
{
return (Math.random() * 100 <= chanceVal);
}
}


/**
* Rolls a set of dice with the specified maximum number.
*
* @param max The maximum value of a single die.
* @param amount The amount of dice to roll.
* @return The rolled die.
*/
public static function roll(max:int, amount:int = 1):int
{
var result:int = 0;
var number:int;
for (var i:int = 0; i < amount; i++)
{
number = NumberGenerator.random(1, max);
result += number;
}
return result;
}
}
}

Change log

r232 by sbalkau on Dec 4, 2009   Diff
renamed project folder.
Go to: 
Project members, sign in to write a code review

Older revisions

r230 by sbalkau on Dec 4, 2009   Diff
- added rhombuslib (game class
library).
- renamed source folders.
r211 by sbalkau on Nov 25, 2009   Diff
added tetragon framework and
restructured source folder.
All revisions of this file

File info

Size: 4918 bytes, 177 lines
Powered by Google Project Hosting