0

so I am looking to either index a variable that is defined in a UDT of structpairofdice or erase the memory of the static intRollNum variable later on in the code. Preferably I'd like to index the sumdice variable although I haven't been able to figure it out, help is much appreciated

    Option Explicit

Type structPairOfDice
    diceOne As Integer
    diceTwo As Integer
    rollNum As Integer
    sumDice As Variant
End Type

'Randomizes the dice between 1 and 6
Function RandomizeDice() As Integer
        RandomizeDice = Application.WorksheetFunction.RandBetween(1, 6)
End Function
Sub RollDice(structDice As structPairOfDice)
    Static intRollNum As Integer
    intRollNum = intRollNum + 1
    With structDice
        .rollNum = intRollNum
        .diceOne = RandomizeDice()
        .diceTwo = RandomizeDice()
        .sumDice = .diceOne + .diceTwo
    End With
End Sub
Sub PrintResults(structDice As structPairOfDice)
    Call RollDice(structDice)
    With structDice
        Debug.Print "Roll #: " & .rollNum
        Debug.Print "Dice: " & .diceOne & ", " & .diceTwo
        Debug.Print "Sum: "; .sumDice
    End With
End Sub
Sub Main()
    Dim structDice As structPairOfDice
    SetThePoint structDice
    'PrintResults structDice
End Sub
Community
  • 1
  • 1
  • I'm not sure what your code is attempting to do when iterating over each item in the empty collection `Rolls`, but I would recommend that you use `Option Explicit` as the first line of every code module so that you don't get caught out by typos such as `intRollNim`. – YowE3K Apr 25 '17 at 05:07
  • Explain what you mean by "index a variable that is defined..." Also, what is SetThePoint? – Rich Holton Apr 25 '17 at 05:34

1 Answers1

0

You can check these two questions and see that you may prefer to use a Class rather than a UDT for this:

You can code a class like this:

clsDice

Option Explicit

Public diceOne As Long
Public diceTwo As Long
Public rollNum As Long
Public sumDice As Long

Public Sub RollDice()
    diceOne = Application.WorksheetFunction.RandBetween(1, 6)
    diceTwo = Application.WorksheetFunction.RandBetween(1, 6)
    sumDice = diceOne + diceTwo
End Sub

Private Sub Class_Initialize()
    RollDice
End Sub

So when you create the class, RollDice is called straight away and rollNum can be set to whatever you need as it is Public.

Then, in a standard module you can do this to 'play the game' and use a Dictionary to store the results of each throw of the dice:

Module1

Option Explicit

Sub PlayGame()

    Dim objDiceDic As Object
    Dim objDice As clsDice
    Dim lngMaxRounds As Long
    Dim lngRound As Long

    ' dictionary for game simulation
    Set objDiceDic = CreateObject("Scripting.Dictionary")

    ' arbitrary number of rounds
    lngMaxRounds = 10
    For lngRound = 1 To lngMaxRounds
        ' creating a new dice class does the rolls for you
        Set objDice = New clsDice
        ' set the rollNum to this round
        objDice.rollNum = lngRound
        ' add the dice object to our throw tracking dictionary
        objDiceDic.Add lngRound, objDice
    Next lngRound

    ' test the simulation output
    For lngRound = 1 To lngMaxRounds
        With objDiceDic(lngRound)
            Debug.Print "Roll #: " & .rollNum
            Debug.Print "Dice: " & .diceOne & ", " & .diceTwo
            Debug.Print "Sum: "; .sumDice
        End With
    Next lngRound

End Sub

So each 'round' becomes the key into the dictionary, where the value is an clsDice object where you have also stored the round (rollNum). So you can easily use the dictionary keys to access the results of a particular round e.g.

Debug.Print objDiceDic(4).sumDice
Community
  • 1
  • 1
Robin Mackenzie
  • 18,801
  • 7
  • 38
  • 56