Home
Manage Your Code
Snippet: DiceBag (C#)
Title: DiceBag Language: C#
Description: Class for generating random values to simulate rolling dice. Views: 219
Author: Eric Ritzie Date Added: 8/20/2008
Copy Code  
1#region VERSION HISTORY   ...#endregion
13#region LOCAL RESOURCE IMPORTS   ...#endregion
19
20/// <remarks>

21///	Dice.DiceRoller - Dice rolling funtions.

22/// </remarks>

23namespace DiceBag
24{
25    /// <summary>

26    /// Dice is a collection of dice rolling functions.

27    /// </summary>

28    /// <example> This sample shows how to instantiate the DiceRoller Class

29    /// <code>

30    ///		using DiceBag;

31    ///		

32    ///		namespace MyNamespace

33    ///		{

34    ///			class MyClass

35    ///			{

36    ///				//Create Class Object.

37    ///				private Dice oDice = new Dice();

38    ///				

39    ///				public MyClass

40    ///				{

41    ///					// Create local variables.

42    ///					ArrayList oAL = new ArrayList();

43    ///					ArrayList oRR = new ArrayList();

44    ///

45    ///					// Populate Re-Roll ArrayList.

46    ///					oRR.Add(1);

47    ///

48    ///					// Call desired dice roller method.

49    ///					oAL = oDice.Roll_ReturnAL(4, Dice.diceTypes.D6,oRR);

50    ///			

51    ///					// Loop through resulting ArrayList and populate the control.

52    ///					for (int i = 0; i < oAL.Count; i++)

53    ///					{

54    ///						textBox9.Text += oAL[i].ToString() + "  ";

55    ///					}

56    ///				}

57    ///			}

58    ///		}

59    /// </code>

60    /// </example>

61    public class Dice
62    {
63        private	Random _rNumber; 
64
65		public Dice()
66		{
67			//

68			// TODO: Add constructor logic here

69			//

70			
71			// Set the Randomize Seed so that the random number will be unique.

72			_rNumber = new Random(unchecked((int)DateTime.Now.Second + (int)DateTime.Now.Ticks));
73            
74		}
75
76
77		/// <summary>

78		///	Types of Dice available to roll.

79		/// </summary>

80        [Description("Types of Dice available to roll.")]
81		public enum diceTypes
82		{
83			D4 = 4,
84			D6 = 6,
85			D8 = 8,
86			D10 = 10,
87			D12 = 12,
88			D20 = 20,
89			D100 = 100
90		}
91
92
93		/// <summary>

94                ///	Roll_ReturnAL will roll the dice for the NumberOfDice and TypeOfDice specified and return an ArrayList of the random results.

95		/// </summary>

96		/// <param name="NumberOfDice">Number of dice to be rolled.</param>

97		/// <param name="TypeOfDice">Type of dice to be rolled.</param>

98		/// <returns>ArrayList of the results.</returns>

99        public ArrayList Roll_ReturnAL(int NumberOfDice, diceTypes TypeOfDice)
100		{
101
102			// Check to make sure that the parameters passed in are greater than 0.

103			// If not then throw an exception.

104			if ((int)NumberOfDice <= 0) throw new Exception("The number of dice to be rolled must be greater than 0.");
105			if ((int)TypeOfDice <= 0) throw new Exception("The type of dice to be roled must be greater than 0.");
106
107			// Define a local ArrayList to store the resulting values to be passed back.

108			ArrayList _result = new ArrayList();
109
110			// Loop through the number of dice we are rolling.

111			// Generate the random numbers and place them in the ArrayList.

112			for (int i = 0; i < (int)NumberOfDice; i++)
113			{
114				// Assign the resulting value to our ArrayList.

115				_result.Add(_rNumber.Next(1, (int)TypeOfDice + 1));				
116			}
117			
118			// Pass back the resulting ArrayList.

119			return _result;
120		}
121
122
123		/// <summary>

124        ///	Roll_ReturnAL(overload1) will roll the dice for the NumberOfDice and TypeOfDice specified and return an ArrayList of the random results.

125		///	This overload will also reroll any dice result matching values contained in the ReRoll ArrayList.

126		/// </summary>

127		/// <param name="NumberOfDice">Number of dice to be rolled.</param>

128		/// <param name="TypeOfDice">Type of dice to be rolled.</param>

129		/// <param name="ReRoll">List of results to reroll.</param>

130		/// <returns>ArrayList of the results.</returns>

131        public ArrayList Roll_ReturnAL(int NumberOfDice, diceTypes TypeOfDice, ArrayList ReRoll)
132		{
133			// Check to make sure that the parameters passed in are greater than 0.

134			// If not then throw an exception.

135			if ((int)NumberOfDice <= 0) throw new Exception("The number of dice to be rolled must be greater than 0.");
136			if ((int)TypeOfDice <= 0) throw new Exception("The type of dice to be roled must be greater than 0.");
137
138			// Define a local ArrayList to store the resulting values to be passed back.

139			ArrayList _result = new ArrayList();
140			
141			// Define a temporary storage object for the result value.

142			int _tmp = 0;
143
144			// Loop through the number of dice we are rolling.

145			// Generate the random numbers and place them in the ArrayList.

146			for (int i = 0; i < (int)NumberOfDice; i++)
147			{
148			RollDice:
149				// Capture the random value in our temp storage.

150				_tmp = _rNumber.Next(1, (int)TypeOfDice + 1);
151
152				// Loop through our ReRoll values and check if _tmp matches.

153				// If _tmp does match one of our ReRoll values then reroll the dice and check again.

154				for (int r = 0; r < ReRoll.Count; r++)
155				{
156					if ((int)_tmp == Convert.ToInt32(ReRoll[r].ToString())) goto RollDice;
157				}
158
159				// Assign the resulting value to our ArrayList.

160				_result.Add((int)_tmp);				
161			}
162			
163			// Pass back the resulting ArrayList.

164			return _result;
165		}
166
167
168		/// <summary>

169		/// Roll_ReturnINT will roll the dice for the NumberOfDice specified and return a random total value.

170		/// </summary>

171		/// <param name="NumberOfDice">Number of dice to be rolled.</param>

172		/// <param name="TypeOfDice">Type of dice to be rolled.</param>

173		/// <returns>Random total value for the dice rolled.</returns>

174		public int Roll_ReturnINT(int NumberOfDice, diceTypes TypeOfDice)
175		{
176			// Check to make sure that the parameters passed in are greater than 0.

177			// If not then throw an exception.

178			if ((int)NumberOfDice <= 0) throw new Exception("The number of dice to be rolled must be greater than 0.");
179			if ((int)TypeOfDice <= 0) throw new Exception("The type of dice to be roled must be greater than 0.");
180
181			// Set the minimum and maximum values for the number and type of dice passed in.

182//			int _minValue = (int)NumberOfDice;

183//			int _maxValue = ((int)NumberOfDice * (int)TypeOfDice);

184
185			// Define a temporary storage object for the result value.

186			int _tmp = 0;
187
188			// Loop through the number of dice we are rolling.

189			// Generate the random numbers and place them in the ArrayList.

190			for (int i = 0; i < (int)NumberOfDice; i++)
191			{
192				// Assign the resulting value to our ArrayList.

193				_tmp += _rNumber.Next(1, (int)TypeOfDice + 1);
194			}
195
196			// Pass back the resulting total value.

197			return (int)_tmp;
198		}
199
200
201		/// <summary>

202		/// Roll_ReturnINT(overload1) will roll the dice for the NumberOfDice specified and return a random total value.

203		///	This overload will also reroll any dice result matching values contained in the ReRoll ArrayList.

204		/// </summary>

205		/// <param name="NumberOfDice">Number of dice to be rolled.</param>

206		/// <param name="TypeOfDice">Type of dice to be rolled.</param>

207		/// <param name="ReRoll">List of results to reroll.</param>

208		/// <returns>Random total value for the dice rolled.</returns>

209		public int Roll_ReturnINT(int NumberOfDice, diceTypes TypeOfDice, ArrayList ReRoll)
210		{
211			// Check to make sure that the parameters passed in are greater than 0.

212			// If not then throw an exception.

213			if ((int)NumberOfDice <= 0) throw new Exception("The number of dice to be rolled must be greater than 0.");
214			if ((int)TypeOfDice <= 0) throw new Exception("The type of dice to be roled must be greater than 0.");
215
216			// Set the minimum and maximum values for the number and type of dice passed in.

217//			int _minValue = (int)NumberOfDice;

218//			int _maxValue = ((int)NumberOfDice * (int)TypeOfDice);

219
220			// Define a temporary storage object for the result value.

221			int _tmp = 0;
222			int _tmp2 = 0;
223
224			// Loop through the number of dice we are rolling.

225			// Generate the random numbers and place them in the ArrayList.

226			for (int i = 0; i < (int)NumberOfDice; i++)
227			{
228			RollDice:
229				// Capture the random value in our temp storage.

230				_tmp2 = _rNumber.Next(1, (int)TypeOfDice + 1);
231
232				// Loop through our ReRoll values and check if _tmp matches.

233				// If _tmp does match one of our ReRoll values then reroll the dice and check again.

234				for (int r = 0; r < ReRoll.Count; r++)
235				{
236					if ((int)_tmp2 == Convert.ToInt32(ReRoll[r].ToString())) goto RollDice;
237				}
238
239				_tmp += (int)_tmp2;
240			}
241			// Pass back the resulting total value.

242			return (int)_tmp;
243		}
244
245
246		/// <summary>

247		/// Roll_ReturnINT(overload1) will take the minimum value and maximum value and return a random total value.

248		/// </summary>

249		/// <param name="minValue">Minimum value allowed.</param>

250		/// <param name="maxValue">Maximum value allowed</param>

251		/// <returns>Random total value for the dice rolled.</returns>

252        public int Roll_ReturnINT(int minValue, int maxValue)
253		{
254			// Check to make sure that the parameters passed in are greater than 0.

255			// If not then throw an exception.

256			if ((int)minValue <= 0) throw new Exception("Minimum Value for the Dice must be greater than 0.");
257			if ((int)maxValue <= 0) throw new Exception("Maximum Value for the Dice must be greater than 0.");
258            if ((int)minValue > maxValue) throw new Exception("Minimum Value for the Dice must be less than Maximum Value.");
259            if ((int)maxValue < minValue) throw new Exception("Maximum Value for the Dice must be greater than Minimum Value.");
260
261			// Generate the random number and pass back the resulting total value.

262			return _rNumber.Next(minValue,maxValue + 1);
263		}
264    }
265}
266
Usage
Usage notes are included in the Code.