Need help on figuring out dice game scoring logic

蹲街弑〆低调 提交于 2019-12-11 23:37:34

问题


So, we are creating a game in VB.net based off the game, Farkle. Basically, you roll an amount of dice and depending on what you keep is your score. We have eight dice. Here's an example, say you roll 3 "3"s and you want to score them, a three of a kind. We want to check all the dice together to see if we do have three 3's. We have figured out the two of a kind, but the three of a kind we cannot.

For l = 0 To 5
            For o = 1 To 6
                For q = 2 To 7
                    If (DieScore(l) = DieScore(o) And DieScore(l) = DieScore(q)) Then
                        PlayerScore = 200 + PlayerScore
                    End If
                Next
            Next
        Next

This is our checking for three of a kind on the dice. If it is true we add 200 to the score. The DieScore(x) refer to the dice. Where are we going wrong?


回答1:


You really just need to loop and count how many times that number of pips (spots) appears in the array.

Dim count As Integer = 0

For pips As Integer = 1 To 6
    count = 0                      ' reset count for each Pip iteration
    For n As Integer = 0 To DieScore.Length - 1

        ' If operator version:
        count += If(DieScore(n) = pips, 1, 0)

        ' If Statement version:
        'If DieScore(n) = pips Then
        '    count += 1
        'End If
    Next

    ' scoring
    Select Case count
        Case 2    ' pair
            playerscore += 50
        Case 3    ' trips
            playerscore += 200
            If pips = 6 Then
                playerscore += 25    ' extra bonus for 666 (example)
            End If
        Case 4    ' quads
            playerscore += 350
        ' etc
    End Select
Next

Not for nothing, but these kinds of logic issues are easy to find using the Debugger. Plus, you will learn a lot about how code executes.




回答2:


For starters, please learn to use different, more descriptive variable names than l and o, that are easily confused with 1 and 0. Some famous bugs have been caused by doing things like that.

One thing you can do is simply count how many dots or pips there are in a roll of the dice and store that in an array.

' index 0 = 1 spot, 5 = 6 spots.
Dim pipsCount(6) as Integer 

' This counts the number of dice for each possible "pips"
For dieIndex as Integer = 0 To DieScore.Length - 1
    ' Increment (the -1 is because index starts at 0)
    pipsCount(DieScore(dieIndex)-1) += 1
Next

Now that you have the number of dice that landed headsup with a given number of pips, you can do a number of different things with that.

' You can easily find out now how many sixes were thrown:
Dim numberOfSixes As Integer = pipsCount(6-1) 

' Or score pairs, trips, quads, ...
For pipsIndex As Integer = 0 To 5
    Select Case pipsCount(pipsIndex)
        Case 2
            PlayerScore += 50
        Case 3
            PlayerScore += 200
        ' ... etc
    End Select
Next

' Or count the length of a straight 
Dim straightLength As Integer = If(pipsCount(0) > 0, 1, 0)
Dim longestStraight As Integer = straightLength 
For pipsIndex As Integer = 1 To 5
    If pipsCount(pipsIndex) > 0 Then
        straightLength += 1
    Else ' straight ended
        If straightLength > longestStraight Then
            longestStraight = straightLength 
        End If
        straightLength = 0
    End If
Next


来源:https://stackoverflow.com/questions/29737168/need-help-on-figuring-out-dice-game-scoring-logic

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!