Code:Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim foo As New Dice() 'standard game - 2, 6 sided dice 'Dim foo As New Dice(2, 1) 'as a coin Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.ffff")) Dim dt As DateTime = DateTime.Now Const sampleThrows As Integer = 1000000 'how many times to throw the dice For x As Integer = 1 To sampleThrows 'throw dice foo.RollAll() Next Dim rolls As Long = foo.GetRolls Debug.WriteLine(rolls.ToString("N0") & " took " & (DateTime.Now - dt).TotalMilliseconds & " ms.") Dim stats As Dictionary(Of Long, Integer) = foo.GetStats 'get the stats For Each kvp As KeyValuePair(Of Long, Integer) In stats Debug.WriteLine(String.Format("{0} - occurred {1} times ({2})", _ kvp.Key.ToString, _ kvp.Value.ToString("n0"), _ (kvp.Value / rolls).ToString("P2"))) Next End Sub End Class Public Class Dice Private Shared PRNG As New Random 'one pseudo random generator Private _theDice As Dictionary(Of Integer, die) 'a collection of items Private _total As Long = 0 'the total for all items Private _keepStats As Boolean = True 'keep stats? Private _theStats As Dictionary(Of Long, Integer) 'the stats Private _rolls As Long = 0L 'number of rolls / throws / flips / etc. Public Sub New() 'the default constructor - 2 dice, 6 faces Me.New(6, 2) End Sub Public Sub New(ByVal Faces As Integer, ByVal NumOfDie As Integer) If Faces < 2 OrElse NumOfDie < 1 Then Throw New ArgumentException Me._theDice = New Dictionary(Of Integer, die) 'create storage for the dice For dieNum As Integer = 1 To NumOfDie 'create a die for each Dim aDie As New die 'a new die aDie.Faces = Faces 'set the number of faces Me._theDice.Add(dieNum, aDie) 'add to collection Next 'init stats Me._theStats = New Dictionary(Of Long, Integer) For dienum As Long = NumOfDie To NumOfDie * Faces Me._theStats.Add(dienum, 0) 'add bucket, count = 0 Next End Sub Public Sub RollAll() 'roll all dice in collection Me._total = 0L 'accumulate total when rolling all For Each d As Integer In Me._theDice.Keys Me._theDice(d).CurrentFace = Dice.PRNG.Next(1, Me._theDice(d).Faces + 1) Me._total += CLng(Me._theDice(d).CurrentFace) Next 'if keeping stats increment count If Me._keepStats Then Me._theStats(Me._total) += 1 'inc. count Me._rolls += 1L 'inc. roll count End If End Sub Public Sub Roll1(ByVal theDieToRoll As Integer) 'roll a die Me._theDice(theDieToRoll).CurrentFace = Dice.PRNG.Next(1, Me._theDice(theDieToRoll).Faces + 1) End Sub ReadOnly Property Total As Long 'return total Get Return Me._total End Get End Property ReadOnly Property GetDice As List(Of die) 'return the dice Get Return Me._theDice.Values.ToList End Get End Property Property StatsOnOff As Boolean 'turn stats on / off Get Return Me._keepStats End Get Set(ByVal value As Boolean) 'reset rolls when going from false to true If value = True AndAlso Me._keepStats = False Then Me._rolls = 0L Me._keepStats = value End Set End Property ReadOnly Property GetStats As Dictionary(Of Long, Integer) 'return the stats Get Return Me._theStats End Get End Property ReadOnly Property GetRolls As Long 'return the rolls Get Return Me._rolls End Get End Property End Class Public Class die Private _faces As Integer = -1 'the number of faces this die Private _curFace As Integer = -1 'the current face of this die Public Property Faces As Integer Get Return Me._faces End Get Set(ByVal value As Integer) Me._faces = value End Set End Property Public Property CurrentFace() As Integer Get Return Me._curFace End Get Set(ByVal value As Integer) Me._curFace = value End Set End Property End Class




Reply With Quote
