Results 1 to 3 of 3

Thread: [.NET 2.0+] Rolling Dice

Threaded View

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    [.NET 2.0+] Rolling Dice

    VB version here.

    Quite a few people have asked how to "roll dice" for games. Here's the "proper" way. First you define a Die class to represent a die.
    CSharp Code:
    1. public class Die
    2. {
    3.     private static Random faceSelector = new Random();
    4.  
    5.     private int _faceCount;
    6.     private int _value;
    7.  
    8.     public int FaceCount
    9.     {
    10.         get
    11.         {
    12.             return this._faceCount;
    13.         }
    14.         set
    15.         {
    16.             this._faceCount = value;
    17.         }
    18.     }
    19.  
    20.     public int Value
    21.     {
    22.         get
    23.         {
    24.             return this._value;
    25.         }
    26.     }
    27.  
    28.     public Die(int faceCount)
    29.     {
    30.         if (faceCount <= 0)
    31.         {
    32.             throw new ArgumentOutOfRangeException("faceCount", "Dice must have one or more faces.");
    33.         }
    34.  
    35.         this._faceCount = faceCount;
    36.     }
    37.  
    38.     public int Roll()
    39.     {
    40.         this._value = faceSelector.Next(1, this.FaceCount + 1);
    41.         return this.Value;
    42.     }
    43. }
    Note that the Die class has a static variable of type Random. This means that there is one and only one Random object for the class. This same object is used to generate random sides for each and every instance of the class.

    The Roll method generates the random number based on the number of faces the current die has. It returns this value and it is also available via the Value property thereafter.

    Many games use multiple dice though, so it's appropriate to define a class that represents multiple Die objects.

    (Note that this code snippet is labelled as VB.NET code but it is obvioulsy C#. It's just that the C# and CSharp code parsers seem to butcher opening square brackets, which is a problem when indexing in C# code.)
    CSharp Code:
    1. public class DieCollection : System.Collections.ObjectModel.Collection<Die>
    2. {
    3.     public int[] RollAll()
    4.     {
    5.         int[] values = new int[this.Count];
    6.  
    7.         for (int index = 0; index < values.Length; index++)
    8.         {
    9.             values[index] = this.Items[index].Roll();
    10.         }
    11.  
    12.         return values;
    13.     }
    14. }
    There's very little code to write for this class because it inherits all the standard collection functionality for the generic Collection<Die> class. Inherited functionality includes an indexer of type Die and an Add method that takes a Die parameter.

    Here's an example of using these classes. As is supposed to be the case in OOP, using the types is dead simple because all the work is done inside the classes themselves. You simply create a DieCollection, create and add as many Die objects as you need and call RollAll:
    CSharp Code:
    1. // Create the collection to store the dice.
    2. DieCollection dice = new DieCollection();
    3.  
    4. // Add two six-sided dice.
    5. dice.Add(new Die(6));
    6. dice.Add(new Die(6));
    7.  
    8. // Roll all the dice.
    9. int[] rolls = dice.RollAll();
    10.  
    11. // Display the values rolled.
    12. foreach (int roll in rolls)
    13. {
    14.     MessageBox.Show(roll.ToString(), "You rolled a ...");
    15. }
    Obviously for a game you would assign your DieCollection to a member variable and then call it's RollAll method each time a player had to roll the dice. You would not create a new DieCollection and Die objects each time you wanted to roll.
    Last edited by jmcilhinney; Apr 25th, 2012 at 12:26 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width