Question
I need to write a poker game that uses a two-dimensional array... Here is the problem:
A poker hand can be stored in a two-dimensional array. The statement
Dim hand(3, 12) As Integer
declares an array with 52 elements, where the first subscript ranges over the four suits and
the second subscript ranges over the thirteen denominations. A poker hand is specified by
placing 1%u2019s in the elements corresponding to the cards in the hand.
Write a program that requests the five cards as input from the user, creates the related
array, and passes the array to procedures to determine the type of the hand: flush (all cards
have the same suit), straight (cards have consecutive denominations%u2014ace can come either
before 2 or after King), straight flush, four-of-a-kind, full house (three cards of one denom-
ination, two cards of another denomination), three-of-a-kind, two pairs, one pair, or none
of the above.
Explanation / Answer
ForumDevX Developer ForumsVB Classic VB Class Project If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. + Reply to Thread Results 1 to 2 of 2 Thread: VB Class Project Thread Tools Display 05-17-2000 12:30 PM #1 Pamela Guest VB Class Project I got straight and straight flush working. Option Explicit ' Project Presentation ' Chapter 7, Project 6, Pg. 396 ' Date: May 3, 2000 ' Author: Pamela MacKenzie ' A poker hand can be stored in a two dimensional array. ' The statement Dim hand(1 to 4, 1 to 13) As Integer ' declares a 52-element array, where the first dimension ' ranges over the four suits and the second dimension ' ranges over the thirteen denominations. A poker hand ' is specified by placing ones in the elements corresponding ' to the cards in the hand. See Figure 7.17. ' Write a program that requests the five cards as input ' from the user, creates the related array, and passes the ' array to procedures to determine the type of the hand: ' flush (all cards have the same suit), straight (cards ' have consecutive denominations-ace can come either before ' 2 or after King), straight flush, four-of-a-kind, ' full house (3 cards of one denomination, 2 cards of ' another denomination), three-of-a-kind, two pairs, one pair, ' or none of the above. Dim hand(1 To 4, 1 To 13) As Integer Dim suit As Integer Dim denom As Integer Dim suitTotal As Integer Dim denomTotal As Integer Dim cardFlag As Boolean Dim cardHand As String Private Sub cmdAnalysis_Click() picHand.Cls cardFlag = False Call changeData(hand()) Call countData(hand(), suit, denom) Call findFlush(hand(), suit, denom, suitTotal, denomTotal, cardFlag, cardHand) Call findStraight(hand(), suit, denom, suitTotal, denomTotal, cardFlag, cardHand) Call findStraightFlush(hand(), suit, denom, suitTotal, denomTotal, cardFlag, cardHand) Call findFourOfAKind(hand(), suit, denom, suitTotal, denomTotal, cardFlag, cardHand) Call findThreeOfAKind(hand(), suit, denom, suitTotal, denomTotal, cardFlag, cardHand) Call findPairs(hand(), suit, denom, suitTotal, denomTotal, cardFlag, cardHand) Call findfullHouse(hand(), suit, denom, suitTotal, denomTotal, cardFlag, cardHand) Call showData(hand(), suit, denom, suitTotal, denomTotal, cardFlag, cardHand) End Sub Private Sub changeData(hand() As Integer) ' Change strings (suit) and card denominations (denom) to 1s Dim i As Integer For i = 0 To 4 suit = InStr(1, "CDHS", UCase(txtSuit(i).Text)) Select Case UCase(txtDenom(i).Text) Case "A" denom = 1 Case "J" denom = 11 Case "Q" denom = 12 Case "K" denom = 13 Case Else denom = Val(txtDenom(i).Text) End Select If suit > 0 And denom > 0 Then hand(suit, denom) = 1 End If Next i End Sub Private Sub countData(hand() As Integer, suit As Integer, denom As Integer) Dim i As Integer Dim j As Integer Dim m As Integer Dim n As Integer Dim suitCount As Integer Dim denomCount As Integer suitTotal = 0 denomTotal = 0 For i = 1 To 4 ' Count cards in suit suitCount = 0 For j = 1 To 13 suitCount = suitCount + hand(i, j) Next j If suitCount > suitTotal Then suitTotal = suitCount Next i For m = 1 To 13 ' Count card denominations denomCount = 0 For n = 1 To 4 denomCount = denomCount + hand(n, m) Next n If denomCount > denomTotal Then denomTotal = denomCount Next m End Sub Private Sub findFlush(hand() As Integer, suit As Integer, denom As Integer, suitTotal As Integer, denomTotal As Integer, cardFlag As Boolean, cardHand As String) If suitTotal = 5 Then cardFlag = True If cardFlag = True Then cardHand = "a flush." End If End If End Sub Private Sub findStraight(hand() As Integer, suit As Integer, denom As Integer, suitTotal As Integer, denomTotal As Integer, cardFlag As Boolean, cardHand As String) Dim i As Integer Dim j As Integer Dim countCards As Integer Dim foundIt As Boolean foundIt = False For i = 1 To 4 countCards = 0 For j = 1 To 13 If hand(i, j) = 1 Then countCards = countCards + 1 Else If countCards 5 Then countCards = 0 End If End If If countCards = 5 Then foundIt = True End If Next j Next i If foundIt Then cardFlag = True If cardFlag = True Then cardHand = "a straight." End If End If End Sub Private Sub findStraightFlush(hand() As Integer, suit As Integer, denom As Integer, suitTotal As Integer, denomTotal As Integer, cardFlag As Boolean, cardHand As String) Dim i As Integer Dim j As Integer Dim countCards As Integer Dim foundIt As Boolean foundIt = False For i = 1 To 4 countCards = 0 For j = 1 To 13 If hand(i, j) = 1 Then countCards = countCards + 1 Else If countCards 5 Then countCards = 0 End If End If If countCards = 5 Then foundIt = True End If Next j Next i If foundIt Then cardFlag = True If cardFlag = True And suitTotal = 5 Then cardHand = "a straight flush!" End If End If End Sub Private Sub findFourOfAKind(hand() As Integer, suit As Integer, denom As Integer, suitTotal As Integer, denomTotal As Integer, cardFlag As Boolean, cardHand As String) If denomTotal = 4 Then cardFlag = True If cardFlag = True Then cardHand = "four of a kind!" End If End If End Sub Private Sub findThreeOfAKind(hand() As Integer, suit As Integer, denom As Integer, suitTotal As Integer, denomTotal As Integer, cardFlag As Boolean, cardHand As String) If denomTotal = 3 Then cardFlag = True If cardFlag = True Then cardHand = "three of a kind." End If End If End Sub Private Sub findPairs(hand() As Integer, suit As Integer, denom As Integer, suitTotal As Integer, denomTotal As Integer, cardFlag As Boolean, cardHand As String) If denomTotal = 2 Then cardFlag = True If cardFlag = True Then cardHand = "two of a kind." End If End If End Sub Private Sub findfullHouse(hand() As Integer, suit As Integer, denom As Integer, suitTotal As Integer, denomTotal As Integer, cardFlag As Boolean, cardHand As String) If denomTotal = 3 And denomTotal = 2 Then cardFlag = True If cardFlag = True Then cardHand = "full house!" End If End If End Sub Private Sub showData(hand() As Integer, suit As Integer, denom As Integer, suitTotal As Integer, denomTotal As Integer, cardFlag As Boolean, cardHand As String) If cardFlag = True Then picHand.Print "You have "; cardHand Else picHand.Print "Sorry, you have no hand." End If End Sub Reply With Quote 05-17-2000 05:07 PM #2 Patrick Marshall Guest Re: VB Class Project Hi Pamela, >I got straight and straight flush working. Are you sure? I think flush looks like it works, but findStraight will return true too often. You aren't looking for patterns e.g. 4,5,6,7,8 you are just counting cards. Try a lot of test cases which SHOULD fail. Also, in FindFullHouse you have: If denomTotal = 3 And denomTotal = 2 Then which CANNOT ever be true, what you are basically saying above is that one number must be 3 and the same number must also be 2. Nope. denomTotal is really a max the way you are using it (which is ok), so when denomTotal is 2 you have a pair OR you may have 2 pair. When denomTotal is 3 you MAY have 3 of a kind OR you may have a full house. When denomTotal is 4 you have 4 of a kind (this is OK). Please take another look at the May 11, 00:07 posting, you are getting further away from code that worked. EVERYTHING in the may 11 post worked except for (I think) pairs and full house. Also, check the parameters you are passing. You are always passing parameters which are global. You could actually remove the entire parameter list with no effect (subs without parameters, basically procedures), you should at least remove parameters which are not used in the subroutine, it is very confusing to see parameters passed then not used. "Pamela" wrote: > > >Option Explicit >' Project Presentation >' Chapter 7, Project 6, Pg. 396 >' Date: May 3, 2000 >' Author: Pamela MacKenzie >' A poker hand can be stored in a two dimensional array. >' The statement Dim hand(1 to 4, 1 to 13) As Integer >' declares a 52-element array, where the first dimension >' ranges over the four suits and the second dimension >' ranges over the thirteen denominations. A poker hand >' is specified by placing ones in the elements corresponding >' to the cards in the hand. See Figure 7.17. >' Write a program that requests the five cards as input >' from the user, creates the related array, and passes the >' array to procedures to determine the type of the hand: >' flush (all cards have the same suit), straight (cards >' have consecutive denominations-ace can come either before >' 2 or after King), straight flush, four-of-a-kind, >' full house (3 cards of one denomination, 2 cards of >' another denomination), three-of-a-kind, two pairs, one pair, >' or none of the above. > >Dim hand(1 To 4, 1 To 13) As Integer >Dim suit As Integer >Dim denom As Integer >Dim suitTotal As Integer >Dim denomTotal As Integer >Dim cardFlag As Boolean >Dim cardHand As String > >Private Sub cmdAnalysis_Click() > picHand.Cls > cardFlag = False > Call changeData(hand()) > Call countData(hand(), suit, denom) > Call findFlush(hand(), suit, denom, suitTotal, denomTotal, cardFlag, >cardHand) > Call findStraight(hand(), suit, denom, suitTotal, denomTotal, cardFlag, >cardHand) > Call findStraightFlush(hand(), suit, denom, suitTotal, denomTotal, cardFlag, >cardHand) > Call findFourOfAKind(hand(), suit, denom, suitTotal, denomTotal, cardFlag, >cardHand) > Call findThreeOfAKind(hand(), suit, denom, suitTotal, denomTotal, cardFlag, >cardHand) > Call findPairs(hand(), suit, denom, suitTotal, denomTotal, cardFlag, >cardHand) > Call findfullHouse(hand(), suit, denom, suitTotal, denomTotal, cardFlag, >cardHand) > Call showData(hand(), suit, denom, suitTotal, denomTotal, cardFlag, cardHand) >End Sub > >Private Sub changeData(hand() As Integer) > ' Change strings (suit) and card denominations (denom) to 1s > Dim i As Integer > For i = 0 To 4 > suit = InStr(1, "CDHS", UCase(txtSuit(i).Text)) > Select Case UCase(txtDenom(i).Text) > Case "A" > denom = 1 > Case "J" > denom = 11 > Case "Q" > denom = 12 > Case "K" > denom = 13 > Case Else > denom = Val(txtDenom(i).Text) > End Select > If suit > 0 And denom > 0 Then > hand(suit, denom) = 1 > End If > Next i >End Sub > >Private Sub countData(hand() As Integer, suit As Integer, denom As Integer) > Dim i As Integer > Dim j As Integer > Dim m As Integer > Dim n As Integer > Dim suitCount As Integer > Dim denomCount As Integer > suitTotal = 0 > denomTotal = 0 > > For i = 1 To 4 ' Count cards in suit > suitCount = 0 > For j = 1 To 13 > suitCount = suitCount + hand(i, j) > Next j > If suitCount > suitTotal Then suitTotal = suitCount > Next i > > For m = 1 To 13 ' Count card denominations > denomCount = 0 > For n = 1 To 4 > denomCount = denomCount + hand(n, m) > Next n > If denomCount > denomTotal Then denomTotal = denomCount > Next m > >End Sub > >Private Sub findFlush(hand() As Integer, suit As Integer, denom As Integer, >suitTotal As Integer, denomTotal As Integer, cardFlag As Boolean, cardHand >As String) > If suitTotal = 5 Then > cardFlag = True > If cardFlag = True Then > cardHand = "a flush." > End If > End If >End Sub > >Private Sub findStraight(hand() As Integer, suit As Integer, denom As Integer, >suitTotal As Integer, denomTotal As Integer, cardFlag As Boolean, cardHand >As String) > Dim i As Integer > Dim j As Integer > Dim countCards As Integer > Dim foundIt As Boolean > foundIt = False > For i = 1 To 4 > countCards = 0 > For j = 1 To 13 > If hand(i, j) = 1 Then > countCards = countCards + 1 > Else > If countCards 5 Then > countCards = 0 > End If > End If > If countCards = 5 Then > foundIt = True > End If > Next j > Next i > If foundIt Then > cardFlag = True > If cardFlag = True Then > cardHand = "a straight." > End If > End If >End Sub > >Private Sub findStraightFlush(hand() As Integer, suit As Integer, denom As >Integer, suitTotal As Integer, denomTotal As Integer, cardFlag As Boolean, >cardHand As String) > Dim i As Integer > Dim j As Integer > Dim countCards As Integer > Dim foundIt As Boolean > foundIt = False > For i = 1 To 4 > countCards = 0 > For j = 1 To 13 > If hand(i, j) = 1 Then > countCards = countCards + 1 > Else > If countCards 5 Then > countCards = 0 > End If > End If > If countCards = 5 Then > foundIt = True > End If > Next j > Next i > If foundIt Then > cardFlag = True > If cardFlag = True And suitTotal = 5 Then > cardHand = "a straight flush!" > End If > End If >End Sub > >Private Sub findFourOfAKind(hand() As Integer, suit As Integer, denom As >Integer, suitTotal As Integer, denomTotal As Integer, cardFlag As Boolean, >cardHand As String) > If denomTotal = 4 Then > cardFlag = True > If cardFlag = True Then > cardHand = "four of a kind!" > End If > End If >End Sub > >Private Sub findThreeOfAKind(hand() As Integer, suit As Integer, denom As >Integer, suitTotal As Integer, denomTotal As Integer, cardFlag As Boolean, >cardHand As String) > If denomTotal = 3 Then > cardFlag = True > If cardFlag = True Then > cardHand = "three of a kind." > End If > End If >End Sub > >Private Sub findPairs(hand() As Integer, suit As Integer, denom As Integer, >suitTotal As Integer, denomTotal As Integer, cardFlag As Boolean, cardHand >As String) > If denomTotal = 2 Then > cardFlag = True > If cardFlag = True Then > cardHand = "two of a kind." > End If > End If >End Sub > >Private Sub findfullHouse(hand() As Integer, suit As Integer, denom As Integer, >suitTotal As Integer, denomTotal As Integer, cardFlag As Boolean, cardHand >As String) > If denomTotal = 3 And denomTotal = 2 Then > cardFlag = True > If cardFlag = True Then > cardHand = "full house!" > End If > End If >End Sub > >Private Sub showData(hand() As Integer, suit As Integer, denom As Integer, >suitTotal As Integer, denomTotal As Integer, cardFlag As Boolean, cardHand >As String) > If cardFlag = True Then > picHand.Print "You have "; cardHand > Else > picHand.Print "Sorry, you have no hand." > End If >End Sub > >