Excel子程序

Excel子程序,excel,vba,Excel,Vba,考虑一个三人的游戏。在游戏中,每位玩家将掷两个骰子,并记录其总数。然后将根据以下规则授予分数: 总得分最高的玩家将获得3分,总得分第二高的玩家将获得1分,最后一名玩家将一无所获 如果这是三名球员之间的平局,那么每位球员将获得1分 如果是前两名球员之间的平局,那么他们每个人将获得2分,而最后一名球员将一无所获 如果最后两名球员打成平局,那么他们每个人都将一无所获,而排名靠前的球员将获得3分 假设骰子是规则的六面公平骰子 让我们分别调用参与者P1、P2和P3。编写一个Excel子程序,模拟玩游戏

考虑一个三人的游戏。在游戏中,每位玩家将掷两个骰子,并记录其总数。然后将根据以下规则授予分数:

  • 总得分最高的玩家将获得3分,总得分第二高的玩家将获得1分,最后一名玩家将一无所获

  • 如果这是三名球员之间的平局,那么每位球员将获得1分

  • 如果是前两名球员之间的平局,那么他们每个人将获得2分,而最后一名球员将一无所获

  • 如果最后两名球员打成平局,那么他们每个人都将一无所获,而排名靠前的球员将获得3分

  • 假设骰子是规则的六面公平骰子

让我们分别调用参与者P1、P2和P3。编写一个Excel子程序,模拟玩游戏1000次

Sub Sim()

Call VBA.Randomize

    For i = 1 To 1000
        Cells(i + 1, 1) = i
        For j = 2 To 4
            x = Int(1 + (Rnd * 6))
            y = Int(1 + (Rnd * 6))
            Cells(i + 1, j) = "(" & x & " , " & y & ")"
            Cells(i + 1, j + 3) = x + y
            If Cells(i + 1, j + 3) > Max Then
              Cells(i + 1, j + 3) = "3"
            ElseIf Cells(i + 1, j + 3) < Min Then
              Cells(i + 1, j + 3) = "0"
            ElseIf Cells(i + 1, j + 3) = Min Then
              Cells(i + 1, j + 3) = "1"
            End If
        Next
Sub-Sim()
调用VBA。随机化
对于i=1到1000
单元(i+1,1)=i
对于j=2到4
x=Int(1+(Rnd*6))
y=Int(1+(Rnd*6))
单元格(i+1,j)=“(&x&“,&y&“)
单元(i+1,j+3)=x+y
如果单元(i+1,j+3)>最大值,则
单元(i+1,j+3)=“3”
ElseIf细胞(i+1,j+3)
??????如何继续以获得以下结果:

预期结果:

我建议使用一个数组来保存结果,然后使用一整套比较来决定每个玩家的分数。下面是一段代码,可以处理不同的可能结果。我相信它可以简化一点

Dim objXLSheet As Worksheet
Dim astrOutput(1 To 3) As String
Dim aintDice(1 To 3) As Integer
Dim aintScore(1 To 3) As Integer
Dim intLoop1 As Integer
Dim intTemp1 As Integer
Dim intTemp2 As Integer

Set objXLSheet = ActiveSheet
Randomize
For intLoop1 = 1 To UBound(aintDice)
    intTemp1 = Int(1 + (Rnd * 6))
    intTemp2 = Int(1 + (Rnd * 6))
    astrOutput(intLoop1) = "(" & intTemp1 & "," & intTemp2 & ")"
    aintDice(intLoop1) = intTemp1 + intTemp2
Next intLoop1
If (aintDice(1) = aintDice(2)) And (aintDice(1) = aintDice(3)) Then '   all three scores are the same
    aintScore(1) = 1: aintScore(2) = 1: aintScore(3) = 1
ElseIf (aintDice(1) > aintDice(2)) And (aintDice(1) > aintDice(3)) Then '   player 1 wins outright
    If aintDice(2) = aintDice(3) Then
        aintScore(1) = 3: aintScore(2) = 0: aintScore(3) = 0
    ElseIf aintDice(2) > aintDice(3) Then
        aintScore(1) = 3: aintScore(2) = 1: aintScore(3) = 0
    ElseIf aintDice(3) > aintDice(2) Then
        aintScore(1) = 3: aintScore(2) = 0: aintScore(3) = 1
    End If
ElseIf (aintDice(2) > aintDice(1)) And (aintDice(2) > aintDice(3)) Then '   player 2 wins outright
    If aintDice(1) = aintDice(3) Then
        aintScore(1) = 0: aintScore(2) = 3: aintScore(3) = 0
    ElseIf aintDice(1) > aintDice(3) Then
        aintScore(1) = 1: aintScore(2) = 3: aintScore(3) = 0
    ElseIf aintDice(3) > aintDice(1) Then
        aintScore(1) = 0: aintScore(2) = 3: aintScore(3) = 1
    End If
ElseIf (aintDice(3) > aintDice(1)) And (aintDice(3) > aintDice(2)) Then '   player 3 wins outright
    If aintDice(1) = aintDice(2) Then
        aintScore(1) = 0: aintScore(2) = 0: aintScore(3) = 3
    ElseIf aintDice(1) > aintDice(2) Then
        aintScore(1) = 1: aintScore(2) = 0: aintScore(3) = 3
    ElseIf aintDice(2) > aintDice(1) Then
        aintScore(1) = 0: aintScore(2) = 1: aintScore(3) = 3
    End If
ElseIf aintDice(1) = aintDice(2) Then   '   players 1 and 2 tie for the win
    aintScore(1) = 2: aintScore(2) = 2: aintScore(3) = 0
ElseIf aintDice(1) = aintDice(3) Then   '   players 1 and 3 tie for the win
    aintScore(1) = 2: aintScore(2) = 0: aintScore(3) = 2
ElseIf aintDice(2) = aintDice(3) Then   '   players 2 and 3 tie for the win
    aintScore(1) = 0: aintScore(2) = 2: aintScore(3) = 2
End If

objXLSheet.Cells(lngGame + 1, 1) = lngGame
For intLoop1 = 1 To UBound(aintDice)
    objXLSheet.Cells(lngGame + 1, 1 + intLoop1) = astrOutput(intLoop1)
    objXLSheet.Cells(lngGame + 1, 4 + intLoop1) = aintScore(intLoop1)
    objXLSheet.Cells(lngGame + 1, 7 + intLoop1) = aintDice(intLoop1)
Next intLoop1

关于,

这是家庭作业吗?只是office.Nah中用于民意测验的一个小vba程序。这是家庭作业。看起来你已经完成了困难的部分,你到底在做什么?是的。通过运行scrip,我可以得到结果,然后我的问题是如何将这个结果转换为最大值=3,最小值=0,最大值和最小值之间的范围为1