4d6下降最低VBA

4d6下降最低VBA,vba,ms-access,Vba,Ms Access,我正在尝试在Access中构建一个简单的掷骰子程序,用于在D&D中生成字符。我不知道如何编写它来保持三个最高的数字 这是我自己能得到的,除非两个数字相同,否则效果很好 'compare and keep best three If Dice1 < Dice2 And Dice1 < Dice3 And Dice1 < Dice4 Then FourdSix = Dice2 + Dice3 + Dice4 ElseIf Dice2 < Dice1 And Dice2

我正在尝试在Access中构建一个简单的掷骰子程序,用于在D&D中生成字符。我不知道如何编写它来保持三个最高的数字

这是我自己能得到的,除非两个数字相同,否则效果很好

'compare and keep best three
If Dice1 < Dice2 And Dice1 < Dice3 And Dice1 < Dice4 Then
    FourdSix = Dice2 + Dice3 + Dice4
ElseIf Dice2 < Dice1 And Dice2 < Dice3 And Dice2 < Dice4 Then
    FourdSix = Dice1 + Dice3 + Dice4
ElseIf Dice3 < Dice1 And Dice3 < Dice2 And Dice3 < Dice4 Then
    FourdSix = Dice1 + Dice2 + Dice4
ElseIf Dice4 < Dice1 And Dice4 < Dice2 And Dice4 < Dice3 Then
    FourdSix = Dice1 + Dice2 + Dice3
Else
    MsgBox "Error, didn't work."
End If
比较并保留最好的三个 如果Dice1我写了一个方法,你可以在以后使用更多的骰子或者你想要保留的不同数量的骰子。我包括了一个概念证明,这样你就可以看到它的工作

Sub Dices()
    'Some random numbers
    Dim Dice1, Dice2, Dice3, Dice4 As Integer
    Dice1 = Int(6 * Rnd + 1)
    Dice2 = Int(6 * Rnd + 1)
    Dice3 = Int(6 * Rnd + 1)
    Dice4 = Int(6 * Rnd + 1)
    
    'The rest is coded as if I didn't knew that you're throwing 4 dices but want to keep only 3, so it can be used in another context for future needs
    'Compare and keep best three
    Dim numberOfDicesToKeep As Integer
    numberOfDicesToKeep = 3
    ReDim highestDicesArray(numberOfDicesToKeep) As Integer ' allocating the exact memory for the array
    
    highestDicesArray = KeepDices(numberOfDicesToKeep, Dice1, Dice2, Dice3, Dice4)
    
    ' Proof of concept, not to paste in your solution
    ' You can use it at first so you can see what happens under the hood, what dices were kept from what selection
    Dim values As String
    Dim i As Integer
    
    values = "Dices : " & CStr(Dice1) & " " & CStr(Dice2) & " " & CStr(Dice3) & " " & CStr(Dice4) & vbCrLf & "Kept  : "
    For i = 0 To numberOfDicesToKeep - 1
        values = values & highestDicesArray(i) & " "
    Next i
    
    MsgBox (values)
End Sub

' This method will compare the numbers you gave it and keep only the biggest ones
' "ParamArray" means that you can put as many parameters as you want here, like 20 dices if you wish
Function KeepDices(numberOfDicesToKeep As Integer, ParamArray dices() As Variant) As Variant
    ReDim result(numberOfDicesToKeep - 1) As Integer
    Dim i, j, lowestDiceIndex As Integer

    For i = 0 To UBound(dices)
        If i < numberOfDicesToKeep Then
            result(i) = dices(i)
        Else
            lowestDiceIndex = -1
            
            For j = 0 To numberOfDicesToKeep - 1
                If dices(i) > result(j) Then
                    If lowestDiceIndex = -1 Then
                        lowestDiceIndex = j
                    Else
                        If result(lowestDiceIndex) > result(j) Then
                            lowestDiceIndex = j
                        End If
                    End If
                End If
            Next j
            
            If lowestDiceIndex <> -1 Then
                result(lowestDiceIndex) = dices(i)
            End If
        End If
    Next i
    
    KeepDices = result
End Function
子骰子()
“一些随机数
将骰子1、骰子2、骰子3、骰子4变为整数
骰子1=整数(6*Rnd+1)
骰子2=整数(6*Rnd+1)
骰子3=整数(6*Rnd+1)
骰子4=整数(6*Rnd+1)
其余的代码就好像我不知道你在掷4个骰子,但我只想保留3个,这样就可以在另一个环境中使用,以满足未来的需要
“比较并保留最好的三个
Dim NumberOfDiceP作为整数
NumberOfDicep=3
将highestDicesArray(numberOfDicesToKeep)重拨为整数“为数组分配精确内存”
最高骰子数=保留的骰子数(骰子数、骰子1、骰子2、骰子3、骰子4)
“概念验证,而不是粘贴到您的解决方案中
“你可以先使用它,这样你就可以看到引擎盖下发生了什么,从什么选择中保留了什么骰子
将值设置为字符串
作为整数的Dim i
values=“Dices:&CStr(Dice1)&&CStr(Dice2)&&CStr(Dice3)&&CStr(Dice4)&vbCrLf&”保留:
对于i=0到numberofdicep-1
值=值和最高值(i)和“”
接下来我
MsgBox(值)
端接头
这个方法会比较你给它的数字,只保留最大的数字
““ParamArray”意味着您可以在此处放置任意数量的参数,如果您愿意,可以放置20个骰子
函数KeepDices(numberOfDicesToKeep为整数,ParamArray dices()为变量)为变量
重拨结果(numberOfDiceP-1)为整数
Dim i,j,作为整数的最低位索引
对于i=0到UBound(骰子)
如果我结果(j),则
如果lowestDiceIndex=-1,则
最低指数=j
其他的
如果结果(最低索引)>结果(j),则
最低指数=j
如果结束
如果结束
如果结束
下一个j
如果最低索引为-1,则
结果(最低骰子指数)=骰子(i)
如果结束
如果结束
接下来我
KeepDices=结果
端函数

玩得开心

我写了一个方法,你可以在以后使用更多的骰子或者你想要保留的不同数量的骰子。我包括了一个概念证明,这样你就可以看到它的工作

Sub Dices()
    'Some random numbers
    Dim Dice1, Dice2, Dice3, Dice4 As Integer
    Dice1 = Int(6 * Rnd + 1)
    Dice2 = Int(6 * Rnd + 1)
    Dice3 = Int(6 * Rnd + 1)
    Dice4 = Int(6 * Rnd + 1)
    
    'The rest is coded as if I didn't knew that you're throwing 4 dices but want to keep only 3, so it can be used in another context for future needs
    'Compare and keep best three
    Dim numberOfDicesToKeep As Integer
    numberOfDicesToKeep = 3
    ReDim highestDicesArray(numberOfDicesToKeep) As Integer ' allocating the exact memory for the array
    
    highestDicesArray = KeepDices(numberOfDicesToKeep, Dice1, Dice2, Dice3, Dice4)
    
    ' Proof of concept, not to paste in your solution
    ' You can use it at first so you can see what happens under the hood, what dices were kept from what selection
    Dim values As String
    Dim i As Integer
    
    values = "Dices : " & CStr(Dice1) & " " & CStr(Dice2) & " " & CStr(Dice3) & " " & CStr(Dice4) & vbCrLf & "Kept  : "
    For i = 0 To numberOfDicesToKeep - 1
        values = values & highestDicesArray(i) & " "
    Next i
    
    MsgBox (values)
End Sub

' This method will compare the numbers you gave it and keep only the biggest ones
' "ParamArray" means that you can put as many parameters as you want here, like 20 dices if you wish
Function KeepDices(numberOfDicesToKeep As Integer, ParamArray dices() As Variant) As Variant
    ReDim result(numberOfDicesToKeep - 1) As Integer
    Dim i, j, lowestDiceIndex As Integer

    For i = 0 To UBound(dices)
        If i < numberOfDicesToKeep Then
            result(i) = dices(i)
        Else
            lowestDiceIndex = -1
            
            For j = 0 To numberOfDicesToKeep - 1
                If dices(i) > result(j) Then
                    If lowestDiceIndex = -1 Then
                        lowestDiceIndex = j
                    Else
                        If result(lowestDiceIndex) > result(j) Then
                            lowestDiceIndex = j
                        End If
                    End If
                End If
            Next j
            
            If lowestDiceIndex <> -1 Then
                result(lowestDiceIndex) = dices(i)
            End If
        End If
    Next i
    
    KeepDices = result
End Function
子骰子()
“一些随机数
将骰子1、骰子2、骰子3、骰子4变为整数
骰子1=整数(6*Rnd+1)
骰子2=整数(6*Rnd+1)
骰子3=整数(6*Rnd+1)
骰子4=整数(6*Rnd+1)
其余的代码就好像我不知道你在掷4个骰子,但我只想保留3个,这样就可以在另一个环境中使用,以满足未来的需要
“比较并保留最好的三个
Dim NumberOfDiceP作为整数
NumberOfDicep=3
将highestDicesArray(numberOfDicesToKeep)重拨为整数“为数组分配精确内存”
最高骰子数=保留的骰子数(骰子数、骰子1、骰子2、骰子3、骰子4)
“概念验证,而不是粘贴到您的解决方案中
“你可以先使用它,这样你就可以看到引擎盖下发生了什么,从什么选择中保留了什么骰子
将值设置为字符串
作为整数的Dim i
values=“Dices:&CStr(Dice1)&&CStr(Dice2)&&CStr(Dice3)&&CStr(Dice4)&vbCrLf&”保留:
对于i=0到numberofdicep-1
值=值和最高值(i)和“”
接下来我
MsgBox(值)
端接头
这个方法会比较你给它的数字,只保留最大的数字
““ParamArray”意味着您可以在此处放置任意数量的参数,如果您愿意,可以放置20个骰子
函数KeepDices(numberOfDicesToKeep为整数,ParamArray dices()为变量)为变量
重拨结果(numberOfDiceP-1)为整数
Dim i,j,作为整数的最低位索引
对于i=0到UBound(骰子)
如果我结果(j),则
如果lowestDiceIndex=-1,则
最低指数=j
其他的
如果结果(最低索引)>结果(j),则
最低指数=j
如果结束
如果结束
如果结束
下一个j
如果最低索引为-1,则
结果(最低骰子指数)=骰子(i)
如果结束
如果结束
接下来我
KeepDices=结果
端函数

玩得开心

为了好玩,如果您希望进行真正的随机计数,您可以使用我的项目中的
ThrowDice
演示功能和位于的ArraySorting模块中的
QuickSort
功能,并将它们结合起来: