Sub test()
Dim oneHand As Variant, bHand As Variant
oneHand = Array("aH", "bd", "cC", "dS", "eH")
bHand = Array("fH", "gD", "mH", "mS", "FD")
MsgBox handRank(oneHand) & vbCrLf & handRank(bHand)
Call whoWon(oneHand, bHand)
End Sub
Sub whoWon(oneHand As Variant, twoHand As Variant)
Select Case Asc(handRank(oneHand)) - Asc(handRank(twoHand))
Case Is = 0
MsgBox "tie"
Case Is < 0
MsgBox "two won"
Case Else
MsgBox "one won"
End Select
End Sub
Function handRank(inhand As Variant) As String
Select Case True
Case isStraightFlush(inhand)
handRank = "zStraight Flush"
Case is4Kind(inhand)
handRank = "y4 of a Kind"
Case isFullHouse(inhand)
handRank = "xFull House"
Case isFlush(inhand)
handRank = "wFlush"
Case isStraight(inhand)
handRank = "vStraight"
Case is3Kind(inhand)
handRank = "uThree of Kind"
Case is2Pair(inhand)
handRank = "tTwo Pair"
Case isPair(inhand)
handRank = "sPair"
Case Else
handRank = inhand(4) & " high"
End Select
End Function
Function isStraightFlush(ByVal inhand As Variant) As Boolean
isStraightFlush = isStraight(inhand) And isFlush(inhand)
End Function
Function is4Kind(ByVal inhand As Variant) As Boolean
inhand = sortHand(inhand)
is4Kind = ((Left(inhand(1), 1) = Left(inhand(0), 1) And Left(inhand(2), 1) = Left(inhand(1), 1) _
And Left(inhand(3), 1) = Left(inhand(2), 1)))
is4Kind = ((Left(inhand(1), 1) = Left(inhand(2), 1) And Left(inhand(2), 1) = Left(inhand(3), 1) _
And Left(inhand(3), 1) = Left(inhand(4), 1))) Or is4Kind
End Function
Function isFlush(ByVal inhand As Variant) As Boolean
Dim i As Integer
inhand = sortHand(inhand)
For i = 0 To 3
If Right(inhand(i), 1) <> Right(inhand(i + 1), 1) Then Exit Function
Next i
isFlush = True
End Function
Function isFullHouse(ByVal inhand As Variant) As Boolean
Dim i As Integer
inhand = sortHand(inhand)
For i = 0 To 2
If (Left(inhand(i), 1) = Left(inhand(i + 1), 1) _
And (Left(inhand(i + 1), 1) = Left(inhand(i + 2), 1))) Then
inhand(i) = "xxx"
inhand(i + 1) = "yyy"
inhand(i + 2) = "zzz"
isFullHouse = True
Exit For
End If
Next i
isFullHouse = isFullHouse And isPair(inhand)
End Function
Function isStraight(ByVal inhand As Variant) As Boolean
Dim i As Integer
inhand = sortHand(inhand)
For i = 0 To 3
If Asc(inhand(i)) <> Asc(inhand(i + 1)) - 1 Then Exit Function
Next i
isStraight = True
End Function
Function isPair(ByVal inhand As Variant) As Boolean
Dim i As Integer
inhand = sortHand(inhand)
For i = 0 To 3
isPair = (Left(inhand(i), 1) = Left(inhand(i + 1), 1) Or isPair)
Next i
End Function
Function is2Pair(ByVal inhand As Variant) As Boolean
Dim i As Integer
inhand = sortHand(inhand)
For i = 0 To 3
If Left(inhand(i), 1) = Left(inhand(i + 1), 1) Then
inhand(i) = "xxx"
inhand(i + 1) = "yyy"
Exit For
End If
Next i
is2Pair = isPair(inhand)
End Function
Function is3Kind(ByVal inhand As Variant) As Boolean
Dim i As Integer
inhand = sortHand(inhand)
For i = 0 To 2
is3Kind = (Left(inhand(i), 1) = Left(inhand(i + 1), 1) _
And (Left(inhand(i + 1), 1) = Left(inhand(i + 2), 1))) Or is3Kind
Next i
End Function
Function sortHand(ByVal inhand As Variant) As Variant
Dim i As Integer, j As Integer, temp As String
For i = 0 To 3
For j = i + 1 To 4
If inhand(j) < inhand(i) Then
temp = inhand(i)
inhand(i) = inhand(j)
inhand(j) = temp
End If
Next j
Next i
sortHand = inhand
End Function