bcil
bcil

Reputation: 3

Choosing the two biggest pairs in a yatzhee game

So hello again, I were wondering how we could get 2 of the biggest pairs in a yatzee game. From a previous question i got help by finding one pair but now i dont even know. It would seem we just need to double the amount in the 1 pair code, but doing that i just got no result or just x4 of the dice

   Public Function parVerdier1(ByVal regel As Integer, tall As Object)
    Dim sum As Integer = 0

    For i As Integer = 0 To 4
        For j As Integer = (i + 1) To 4
            If tall(i) = tall(j) Then
                If tall(i) + tall(j) > sum Then
                    sum = tall(i) + tall(j)
                End If
            End If
        Next
    Next
    Return sum
End Function

This is the 1 pair code if ayone were wondering, help pls and thank you :) Also the regel is used futher down the rest of the code.

yatzee is a game where you throw 5 dices, the eyes = points (you can combo it for more points etc). lets pretend you threw (3 3 4 4 5), by selecting it as a pair you get 8points (4+4), if you select it as two pairs you get 14points (4+4+3+3) you can read more here if you want to learn all the rules http://en.wikipedia.org/wiki/Yahtzee its a pretty easy game and fun to play if you're bored.

My lates code with a lot of help thank you :) still got some problems like http://imgur.com/ExpBb2Q when i get these dices i get 40 points...

    Public Function parVerdier2(ByVal regel As Integer, tall As Object)

    Dim sum As Integer = 0
    Dim sum2 As Integer = 0
    For o As Integer = 0 To 4
        For l As Integer = (o + 1) To 4
            For i As Integer = (l + 1) To 4
                For j As Integer = (i + 1) To 4
                    If tall(i) = tall(j) And tall(o) = tall(l) Or tall(i) = tall(o) And tall(j) = tall(l) Or tall(i) = tall(l) And tall(j) = tall(o) Then
                        If tall(i) + tall(j) + tall(o) + tall(l) > sum Then
                            sum2 = sum
                            sum = tall(i) + tall(j) + tall(o) + tall(l)
                        ElseIf tall(i) + tall(j) + tall(o) + tall(l) > sum2 Then
                            sum2 = tall(i) + tall(j) + tall(o) + tall(l)
                        End If
                    End If
                Next
            Next
        Next
    Next
    sum += sum2
    Return sum
 End Function

Upvotes: 0

Views: 86

Answers (2)

deme72
deme72

Reputation: 1153

you would just need another variable for the second biggest sum:

Public Function parVerdier1(ByVal regel As Integer, tall As Object)

    Dim sum As Integer = 0
    Dim sum2 As Integer = 0

    For i As Integer = 0 To 4
        For j As Integer = (i + 1) To 4
            If tall(i) = tall(j) Then
                If tall(i) + tall(j) > sum Then
                    sum2 = sum
                    sum = tall(i) + tall(j)
                Else If tall(i) + tall(j) > sum2 Then
                    sum2 = tall(i) + tall(j)
                End If 

            End If
        Next
    Next
    Return sum + sum2
End Function

Upvotes: 1

Chris Dunaway
Chris Dunaway

Reputation: 11216

You can use Linq to get the two pairs with the greatest sum:

Public Shared Sub Main()
    Dim dieValues() As Integer = {3, 3, 4, 4, 5}

    Dim pairs = dieValues.GroupBy(Function(i) i).
                          Where(Function(g) g.Count() = 2).
                          OrderByDescending(Function(g) g.Sum()).
                          Select(Function(g) New With {.Value = g.Key, .Sum = g.Sum()}).
                          Take(2)


    For Each g In pairs
        Console.WriteLine("{0}: {1}", g.Key, g.Sum)
    Next

    Console.ReadLine()

End Sub

Output:

4: 8
3: 6

Upvotes: 0

Related Questions