Archive for Agosto 20th, 2008

DiceBag

CodeKeep C# Feed Agosto 20th, 2008

Description: Class for generating random values to simulate rolling dice.

Link: http://www.codekeep.net/snippets/8e5054d2-02fa-4cbb-9614-a290d9ab8271.aspx

#region VERSION HISTORY
/*===============================================================================================
	DATE CREATED: 	10.18.2003
	CREATED BY:	ERIC RITZIE

	NOTES:

	HISTORY:		
        ER	10.18.2003	1.0.0	INITIAL CREATION
===============================================================================================*/
#endregion

#region LOCAL RESOURCE IMPORTS
using System;
using System.Collections;
using System.ComponentModel;
#endregion

/// <remarks>
///	Dice.DiceRoller - Dice rolling funtions.
/// </remarks>
namespace DiceBag
{
    /// <summary>
    /// Dice is a collection of dice rolling functions.
    /// </summary>
    /// <example> This sample shows how to instantiate the DiceRoller Class
    /// <code>
    ///		using DiceBag;
    ///		
    ///		namespace MyNamespace
    ///		{
    ///			class MyClass
    ///			{
    ///				//Create Class Object.
    ///				private Dice oDice = new Dice();
    ///				
    ///				public MyClass
    ///				{
    ///					// Create local variables.
    ///					ArrayList oAL = new ArrayList();
    ///					ArrayList oRR = new ArrayList();
    ///
    ///					// Populate Re-Roll ArrayList.
    ///					oRR.Add(1);
    ///
    ///					// Call desired dice roller method.
    ///					oAL = oDice.Roll_ReturnAL(4, Dice.diceTypes.D6,oRR);
    ///			
    ///					// Loop through resulting ArrayList and populate the control.
    ///					for (int i = 0; i < oAL.Count; i++)
    ///					{
    ///						textBox9.Text += oAL[i].ToString() + "  ";
    ///					}
    ///				}
    ///			}
    ///		}
    /// </code>
    /// </example>
    [Description("Collection of dice rolling functions.")]
    public class Dice
    {
        private	Random _rNumber; 

		public Dice()
		{
			//
			// TODO: Add constructor logic here
			//
			
			// Set the Randomize Seed so that the random number will be unique.
			_rNumber = new Random(unchecked((int)DateTime.Now.Second + (int)DateTime.Now.Ticks));
            
		}


		/// <summary>
		///	Types of Dice available to roll.
		/// </summary>
        [Description("Types of Dice available to roll.")]
		public enum diceTypes
		{
			D4 = 4,
			D6 = 6,
			D8 = 8,
			D10 = 10,
			D12 = 12,
			D20 = 20,
			D100 = 100
		}


		/// <summary>
        ///	Roll_ReturnAL will roll the dice for the NumberOfDice and TypeOfDice specified and return an ArrayList of the random results.
		/// </summary>
		/// <param name="NumberOfDice">Number of dice to be rolled.</param>
		/// <param name="TypeOfDice">Type of dice to be rolled.</param>
		/// <returns>ArrayList of the results.</returns>
		[Description("Roll the dice for the number of dice and type of dice specified and return an ArrayList of the random results.")]
        public ArrayList Roll_ReturnAL(int NumberOfDice, diceTypes TypeOfDice)
		{

			// Check to make sure that the parameters passed in are greater than 0.
			// If not then throw an exception.
			if ((int)NumberOfDice <= 0) throw new Exception("The number of dice to be rolled must be greater than 0.");
			if ((int)TypeOfDice <= 0) throw new Exception("The type of dice to be roled must be greater than 0.");

			// Define a local ArrayList to store the resulting values to be passed back.
			ArrayList _result = new ArrayList();

			// Loop through the number of dice we are rolling.
			// Generate the random numbers and place them in the ArrayList.
			for (int i = 0; i < (int)NumberOfDice; i++)
			{
				// Assign the resulting value to our ArrayList.
				_result.Add(_rNumber.Next(1, (int)TypeOfDice + 1));				
			}
			
			// Pass back the resulting ArrayList.
			return _result;
		}


		/// <summary>
        ///	Roll_ReturnAL(overload1) will roll the dice for the NumberOfDice and TypeOfDice specified and return an ArrayList of the random results.
		///	This overload will also reroll any dice result matching values contained in the ReRoll ArrayList.
		/// </summary>
		/// <param name="NumberOfDice">Number of dice to be rolled.</param>
		/// <param name="TypeOfDice">Type of dice to be rolled.</param>
		/// <param name="ReRoll">List of results to reroll.</param>
		/// <returns>ArrayList of the results.</returns>
        [Description("Roll the dice for the number of dice and type of dice specified and return an ArrayList of the random results. Also specify what results will trigger a re-roll of the dice.")]
        public ArrayList Roll_ReturnAL(int NumberOfDice, diceTypes TypeOfDice, ArrayList ReRoll)
		{
			// Check to make sure that the parameters passed in are greater than 0.
			// If not then throw an exception.
			if ((int)NumberOfDice <= 0) throw new Exception("The number of dice to be rolled must be greater than 0.");
			if ((int)TypeOfDice <= 0) throw new Exception("The type of dice to be roled must be greater than 0.");

			// Define a local ArrayList to store the resulting values to be passed back.
			ArrayList _result = new ArrayList();
			
			// Define a temporary storage object for the result value.
			int _tmp = 0;

			// Loop through the number of dice we are rolling.
			// Generate the random numbers and place them in the ArrayList.
			for (int i = 0; i < (int)NumberOfDice; i++)
			{
			RollDice:
				// Capture the random value in our temp storage.
				_tmp = _rNumber.Next(1, (int)TypeOfDice + 1);

				// Loop through our ReRoll values and check if _tmp matches.
				// If _tmp does match one of our ReRoll values then reroll the dice and check again.
				for (int r = 0; r < ReRoll.Count; r++)
				{
					if ((int)_tmp == Convert.ToInt32(ReRoll[r].ToString())) goto RollDice;
				}

				// Assign the resulting value to our ArrayList.
				_result.Add((int)_tmp);				
			}
			
			// Pass back the resulting ArrayList.
			return _result;
		}


		/// <summary>
		/// Roll_ReturnINT will roll the dice for the NumberOfDice specified and return a random total value.
		/// </summary>
		/// <param name="NumberOfDice">Number of dice to be rolled.</param>
		/// <param name="TypeOfDice">Type of dice to be rolled.</param>
		/// <returns>Random total value for the dice rolled.</returns>
        [Description("Roll the dice for the number of dice and type of dice specified and return an Integer value.")]
		public int Roll_ReturnINT(int NumberOfDice, diceTypes TypeOfDice)
		{
			// Check to make sure that the parameters passed in are greater than 0.
			// If not then throw an exception.
			if ((int)NumberOfDice <= 0) throw new Exception("The number of dice to be rolled must be greater than 0.");
			if ((int)TypeOfDice <= 0) throw new Exception("The type of dice to be roled must be greater than 0.");

			// Set the minimum and maximum values for the number and type of dice passed in.
//			int _minValue = (int)NumberOfDice;
//			int _maxValue = ((int)NumberOfDice * (int)TypeOfDice);

			// Define a temporary storage object for the result value.
			int _tmp = 0;

			// Loop through the number of dice we are rolling.
			// Generate the random numbers and place them in the ArrayList.
			for (int i = 0; i < (int)NumberOfDice; i++)
			{
				// Assign the resulting value to our ArrayList.
				_tmp += _rNumber.Next(1, (int)TypeOfDice + 1);
			}

			// Pass back the resulting total value.
			return (int)_tmp;
		}


		/// <summary>
		/// Roll_ReturnINT(overload1) will roll the dice for the NumberOfDice specified and return a random total value.
		///	This overload will also reroll any dice result matching values contained in the ReRoll ArrayList.
		/// </summary>
		/// <param name="NumberOfDice">Number of dice to be rolled.</param>
		/// <param name="TypeOfDice">Type of dice to be rolled.</param>
		/// <param name="ReRoll">List of results to reroll.</param>
		/// <returns>Random total value for the dice rolled.</returns>
        [Description("Roll the dice for the number of dice and type of dice specified and return an Integer value. Also specify what results will trigger a re-roll of the dice.")]
		public int Roll_ReturnINT(int NumberOfDice, diceTypes TypeOfDice, ArrayList ReRoll)
		{
			// Check to make sure that the parameters passed in are greater than 0.
			// If not then throw an exception.
			if ((int)NumberOfDice <= 0) throw new Exception("The number of dice to be rolled must be greater than 0.");
			if ((int)TypeOfDice <= 0) throw new Exception("The type of dice to be roled must be greater than 0.");

			// Set the minimum and maximum values for the number and type of dice passed in.
//			int _minValue = (int)NumberOfDice;
//			int _maxValue = ((int)NumberOfDice * (int)TypeOfDice);

			// Define a temporary storage object for the result value.
			int _tmp = 0;
			int _tmp2 = 0;

			// Loop through the number of dice we are rolling.
			// Generate the random numbers and place them in the ArrayList.
			for (int i = 0; i < (int)NumberOfDice; i++)
			{
			RollDice:
				// Capture the random value in our temp storage.
				_tmp2 = _rNumber.Next(1, (int)TypeOfDice + 1);

				// Loop through our ReRoll values and check if _tmp matches.
				// If _tmp does match one of our ReRoll values then reroll the dice and check again.
				for (int r = 0; r < ReRoll.Count; r++)
				{
					if ((int)_tmp2 == Convert.ToInt32(ReRoll[r].ToString())) goto RollDice;
				}

				_tmp += (int)_tmp2;
			}
			// Pass back the resulting total value.
			return (int)_tmp;
		}


		/// <summary>
		/// Roll_ReturnINT(overload1) will take the minimum value and maximum value and return a random total value.
		/// </summary>
		/// <param name="minValue">Minimum value allowed.</param>
		/// <param name="maxValue">Maximum value allowed</param>
		/// <returns>Random total value for the dice rolled.</returns>
        [Description("take the minimum value and maximum value and return a random total value.")]
        public int Roll_ReturnINT(int minValue, int maxValue)
		{
			// Check to make sure that the parameters passed in are greater than 0.
			// If not then throw an exception.
			if ((int)minValue <= 0) throw new Exception("Minimum Value for the Dice must be greater than 0.");
			if ((int)maxValue <= 0) throw new Exception("Maximum Value for the Dice must be greater than 0.");
            if ((int)minValue > maxValue) throw new Exception("Minimum Value for the Dice must be less than Maximum Value.");
            if ((int)maxValue < minValue) throw new Exception("Maximum Value for the Dice must be greater than Minimum Value.");

			// Generate the random number and pass back the resulting total value.
			return _rNumber.Next(minValue,maxValue + 1);
		}
    }
}