Results 1 to 5 of 5

Thread: [.NET 2.0+] Rolling Dice

Threaded View

  1. #1
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    81,249

    [.NET 2.0+] Rolling Dice

    C# 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.
    vb.net Code:
    1. Public Class Die
    2.  
    3.     Private Shared faceSelector As New Random
    4.  
    5.     Private _faceCount As Integer
    6.     Private _value As Integer
    7.  
    8.     Public Property FaceCount() As Integer
    9.         Get
    10.             Return Me._faceCount
    11.         End Get
    12.         Set(ByVal value As Integer)
    13.             Me._faceCount = value
    14.         End Set
    15.     End Property
    16.  
    17.     Public ReadOnly Property Value() As Integer
    18.         Get
    19.             Return Me._value
    20.         End Get
    21.     End Property
    22.  
    23.     Public Sub New(ByVal faceCount As Integer)
    24.         If faceCount <= 0 Then
    25.             Throw New ArgumentOutOfRangeException("faceCount", "Dice must have one or more faces.")
    26.         End If
    27.  
    28.         Me._faceCount = faceCount
    29.     End Sub
    30.  
    31.     Public Function Roll() As Integer
    32.         Me._value = faceSelector.Next(1, Me.FaceCount + 1)
    33.         Return Me.Value
    34.     End Function
    35.  
    36. End Class
    Note that the Die class has a Shared 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.
    vb.net Code:
    1. Public Class DieCollection
    2.     Inherits System.Collections.ObjectModel.Collection(Of Die)
    3.  
    4.     Public Function RollAll() As Integer()
    5.         Dim values(Me.Count - 1) As Integer
    6.  
    7.         For index As Integer = 0 To values.GetUpperBound(0)
    8.             values(index) = Me.Items(index).Roll()
    9.         Next
    10.  
    11.         Return values
    12.     End Function
    13.  
    14. End Class
    There's very little code to write for this class because it inherits all the standard collection functionality from the generic Collection(Of Die) class. Inherited functionality includes an Item property 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:
    vb.net Code:
    1. 'Create the collection to store the dice.
    2. Dim dice As 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. Dim rolls As Integer() = dice.RollAll()
    10.  
    11. 'Display the values rolled.
    12. For Each roll As Integer In rolls
    13.     MessageBox.Show(roll.ToString(), "You rolled a ...")
    14. Next
    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; Aug 31st, 2009 at 09:49 PM.

Posting Permissions

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