# Video Poker Game in Excel



## hey yer barred (Dec 22, 2007)

Hi All

Im trying to make a video poker game in Excel using VBA and formulas.

Ive got the random card selection to work, and the betting and holding cards to work like a dream....but I wanted to know if their is a easy of caculating the hands.

The only way I can think of is having a hidden sheet with the 2million possible hands on it...i dont really fancy writting them out to be honest.

I have found one on the net, but I would like to make my own as I learn new things that way...plus the sheet is in some other launguage which doesnt help!


----------



## hey yer barred (Dec 22, 2007)

Sorry, forgot to say I do realise this isnt really what Excel was inveted for...but it is good learning for newbies at VBA like me!

Sorry mod's if this post does break and code's of conduct...please dant ban me


----------



## Gerald Higgins (Dec 22, 2007)

Personally I don't have any problem with using Excel to create a poker game - good luck to you ! But the question of how to calculate the hands is maybe not one for Excel. What I mean is, I'm sure Excel can calculate the value of the hands, but you yourself first need to have some idea of exactly how to do it.

At a very simplistic level, one way might be to attach values to all the cards, and then get Excel to add them all up. Excel can easily do that calculation, but the point is that you need to be able to define that that's the way you want to calculate them.
By the way, I'm sure this WOULDN'T be the way you'd actually want to do it.

If you can come up with a method of calculating the values, you can almost certainly get Excel to do the calculation. If you come up with a method, but have problems getting Excel to do the calculation, post back here, describing your method clearly, and I'm sure there'll be people happy to help.


----------



## mikerickson (Dec 22, 2007)

Here's a notion I had.
Each card in the deck is a two character string. "aC","bC","..."mC" are the clubs. "aH" is the deuce of hearts.
Each hand is a 0 based array.
This doesn't figure out who won between a pair of jacks and a pair of queens, but it can tell two pair from a full house.

The Select Case in RankHands keeps us from being concerned that 3 of  kind is also a pair.

Since each hand is sorted, if handRank(oneHand) = handRand(twoHand) then comparing oneHand(4) to twoHand(4) will find the winner.


```
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
```


----------



## mikerickson (Dec 22, 2007)

This modification to handRank eases hand comparison.
If handRank(oneHand)<handRank(twoHand) then twoHand wins. This is a string comparison.


```
Sub test()
Dim oneHand As Variant, twoHand As Variant, threehand As Variant
oneHand = Array("aH", "bD", "cC", "eS", "dH")
twoHand = Array("fH", "gD", "gH", "mS", "fD")
MsgBox Mid(handRank(oneHand), 2, 14) & vbCrLf & Mid(handRank(twoHand), 2, 14)
Call whoWon(oneHand, twoHand)
End Sub

Sub whoWon(oneHand As Variant, twoHand As Variant)
If handRank(oneHand) > handRank(twoHand) Then
    MsgBox "First Hand won"
Else
    If handRank(oneHand) = handRank(twoHand) Then
        MsgBox "tie"
    Else
        MsgBox "second hand won"
    End If
End If
End Sub
Function handRank(inHand As Variant) As String
Dim i As Integer
inHand = sortHand(inHand)
handRank = Left(inHand(4), 1) & Left(inHand(3), 1) & Left(inHand(2), 1) _
                    & Left(inHand(1), 1) & Left(inHand(0), 1)
Select Case True
    Case isStraightFlush(inHand)
        handRank = "zStraight Flush" & handRank
    Case is4Kind(inHand)
        handRank = "y4 of a Kind   " & handRank
    Case isFullHouse(inHand)
        handRank = "xFull House    " & handRank
    Case isFlush(inHand)
        handRank = "wFlush         " & handRank
    Case isStraight(inHand)
        handRank = "vStraight      " & handRank
    Case is3Kind(inHand)
        handRank = "uThree of Kind " & handRank
    Case is2Pair(inHand)
        handRank = "tTwo Pair      " & handRank
    Case isPair(inHand)
        handRank = "sPair          " & handRank
    Case Else
        handRank = "r              " & handRank
End Select
End Function
```


----------



## PaddyD (Dec 23, 2007)

you might also find this interesting:

http://www.mrexcel.com/forum/showthread.php?t=62453&highlight=poker


----------



## hey yer barred (Dec 24, 2007)

Well...so far i only have the random card selection...which works!

The cards on the screen are in a simple..eg Queen of Hearts is QH and 10 Spades is 10S

So im thinking some sort of left and right formula's and a few if's should...SHOULD do the trick...i'll keep you all posted!


----------



## gavinkelly (Jan 14, 2008)

I am trying to do something similar but i can not see how to generate the cards so that without replacement.

Could you point me in the right direction please thanks.


----------



## hey yer barred (Jan 14, 2008)

i use a random selection tool in excel, then used some IF statments to select cards.

It's really hard for me to explain, but if you want to send me a private message with your email addy, ill send you what i have made tonight and see if thats helps you.


----------

