索引随机骰子卷(VBA)

时间:2017-04-26 22:00:00

标签: arrays vba excel-vba indexing excel

我正在为掷骰子游戏构建一个模拟,下面是我的代码来完成滚动过程,以及执行随机滚动的骰子类。我希望能够绘制卷的分布图。为此,我相信我需要随机滚动到阵列中,任何人都可以帮助我被困住一段时间。

Sub SetThePoint()
    Dim lngRoll As Long
    Dim lngNextRoll As Long
    Dim lngThePoint As Long
    Dim lngCounter As Long
    Dim boolPointSet As Boolean
    Dim boolResolved As Boolean

    Set objDiceDict = CreateObject("Scripting.Dictionary")
    Set objDice = New clsDice
    'this procedure takes the value the roll dice function and conintiously rolls until a point is set
'StartGame:
    Do
        objDice.RollDice
        lngRoll = objDice.sumDice
        'Debug.Print lngRoll
        Select Case lngRoll
            Case Is = 2
                boolPointSet = False
            Case Is = 3
                boolPointSet = False
            Case Is = 4
                boolPointSet = True
                lngThePoint = 4
            Case Is = 5
                boolPointSet = True
                lngThePoint = 5
            Case Is = 6
                boolPointSet = True
                lngThePoint = 6
            Case Is = 7
                boolPointSet = False
            Case Is = 8
                boolPointSet = True
                lngThePoint = 8
            Case Is = 9
                boolPointSet = True
                lngThePoint = 9
            Case Is = 10
                boolPointSet = True
                lngThePoint = 10
            Case Is = 11
                boolPointSet = False
            Case Is = 12
                boolPointSet = False
            Case Else
                boolPointSet = False
        End Select
        'Call PassLineBet(lngRoll)
    Loop While boolPointSet = False


    If boolPointSet = True Then
        Do Until boolResolved = True
            Debug.Print "The current point is a " & lngThePoint
            objDice.RollDice
            lngNextRoll = objDice.sumDice
            'Debug.Print lngCounter
            Debug.Print "You Rolled a " & lngNextRoll
                If lngNextRoll = lngThePoint Then
                    'boolResolved = True
                    Debug.Print "You win"
                    GoTo StartGame:
                ElseIf lngNextRoll = 7 Then
                    boolResolved = True
                    Debug.Print "You Lose"
                    'Debug.Print "Roll #: " & lngCounter
                    Exit Sub
                Else
                    boolResolved = False
                End If
        Loop
    End If
End Sub

休息,上课:

Option Explicit
Public diceOne As Long
Public diceTwo As Long
Public rollNum As Long
Public lngRollCounter As Long
Public sumDice As Long
Public lngPointsMade As Long
Public lSum As Long



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

Private Sub Class_Initialize()
End Sub
Public Function Counter() As Long
    Dim i As Long

    i = i + 1
    Counter = i


End Function

0 个答案:

没有答案