Solved

Help on Loop

Posted on 1998-07-11
6
177 Views
Last Modified: 2010-05-03
I have to figure out how to do this.  This is a similiar situation, if you  can show me how to do this, I can apply it to my situation:
I need to have a subprogram show the results of
Using 5 textboxes and
some string functions.  The output would look like this:

      
A              1  2   3  4  5  6  7  8  9 10 J Q K
club          0  1   1  0  0  0  1  1  2  1  1 0 0
diamond    ect
heart          ect
spade         ect


basically I need to write  program that requests the five cards as input from the user,using textbox, creates the related array, and passes the array to subprograms to determine the type of the hand: flush (all cards have the same suit), 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.                        
  For example, a user inputs C4(four of clubs), C6,
H3(three of hearts), DK(king of diamonds), SQ(queen of spades) into five
textboxes respectively.  Then for each card, identify suit and number by
using, for instance, Left(txtCard1.Text, 1) and Right(txtCard1.Text, 1),
and assign 1 to appropriate element in two dimensional array .  (Of course you need to convert letters such as A, J, Q, and K into
numbers.)

2. Use two arrays of counters: One for numbers and the other for suits.
Of course, the size of array for number counters should be 13, and that of
array for suit counters should be 4.   scan this array of flag by using
nested For Next loop.  While scanning this table count how many 1's for each suit and each number.  With these two arrays of counters, determine the type of the hand.  For example, if you have 5 of counter in any suit, it's flush.  If you have 4 of counter in any number, it's
four-of-a-kind.  And so on.

I don't even know where to start.  I appreciate any help.  I know there are other ways to do this.  but I need to do it the way stated above.

Thanks!
0
Comment
Question by:checkout
6 Comments
 
LVL 6

Expert Comment

by:alamo
ID: 1465495
Hi, before we go much further, please confirm that this is not being used for a class assignment... if it is, and I gave you more than general help, I'd be breaking the Experts-Exchange rules.

One hint I'll say right off: in VB usually the easiest way to convert alphanumeric information into an array index (so you can count cards and suits) is Instr. For example:

strCard  = UCase$(Right(txtCard1.Text, 1))
iCard = Instr("A234567890JQK", strCard)

Let me know...
0
 

Expert Comment

by:peterprice
ID: 1465496
Another Alternative is to assign a unique number to each card 1-52 where 1 = 2 clubs, 2=3 clubs....13 =ace clubs, 14 = 2 diamonds,....52 = ace spades
0
 
LVL 2

Accepted Solution

by:
swilt earned 200 total points
ID: 1465497
5 Text boxes in a control array Text1(0) to Text1(4) and a button (Command1)
Code for Form1;

Option Explicit

Private Const cSuit = "CDHS"
Private Const cCard = "A234567890JQK"

Private bCards(1 To 4, 1 To 13) As Boolean

Private Sub Command1_Click()
    Dim i As Integer, j As Integer, k As Integer
    Dim sCard As String
    Dim bErr As Boolean
   
    bErr = False
    For i = 1 To 4
        For j = 1 To 13
            bCards(i, j) = False
        Next j
    Next i
    For k = 0 To 4
        i = 0: j = 0
        sCard = UCase$(Trim$(Text1(k).Text))
        If Len(sCard) = 3 Then
            If Left$(sCard, 2) = "10" Then
                sCard = "0" & Right$(sCard, 1)
            ElseIf Right$(sCard, 2) = "10" Then
                sCard = Left$(sCard, 1) & "0"
            End If
        End If
        If Len(sCard) = 2 Then
            If InStr(cSuit, Left$(sCard, 1)) > 0 Then
                i = InStr(cSuit, Left$(sCard, 1))
                j = InStr(cCard, Right$(sCard, 1))
            Else
                i = InStr(cSuit, Right$(sCard, 1))
                j = InStr(cCard, Left$(sCard, 1))
            End If
        End If
        If i = 0 Or j = 0 Then
            bErr = True
            MsgBox "Invalid card " & Text1(k).Text
        Else
            If bCards(i, j) Then
                MsgBox "Repeat card " & Text1(k).Text
                bErr = True
            Else
                bCards(i, j) = True
            End If
        End If
    Next k
    MsgBox sShowResult
End Sub

Private Function sShowResult() As String
    Dim i As Integer, j As Integer, nFullH As Integer
    Dim nSuit(1 To 4) As Integer
    Dim nCard(1 To 13) As Integer
    Dim sRet As String
   
    sRet = ""
    For i = 1 To 4
        For j = 1 To 13
            If bCards(i, j) Then
                nSuit(i) = nSuit(i) + 1
                nCard(j) = nCard(j) + 1
            End If
        Next j
    Next i
    For i = 1 To 4
        If nSuit(i) = 5 Then sRet = "Flush"
    Next i
    If sRet = "" Then
        For j = 1 To 13
            If nCard(j) > 1 Then
                If nCard(j) = 2 Then
                    If nFullH = 0 Then nFullH = 3
                    If nFullH = 2 Then nFullH = 99
                ElseIf nCard(j) = 3 Then
                    If nFullH = 0 Then nFullH = 2
                    If nFullH = 3 Then nFullH = 99
                End If
                If nFullH = 99 Then
                    sRet = "Full House"
                    Exit For
                Else
                    If sRet <> "" Then sRet = sRet & vbCr & "and "
                    sRet = sRet & nCard(j) & " of a kind"
                End If
            End If
        Next j
    End If
    If sRet = "" Then sRet = "Nothing"
    sShowResult = sRet
End Function
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:checkout
ID: 1465498
Thanks for your response, I just need clarification,
This line gives me a complile error "wrong number of assignments"
sCard = UCase$(Trim$(Text1(k).Text))
it seems anywhere the text1(k).text) is, it halts on errors,

can you clarify?
thanks

0
 

Author Comment

by:checkout
ID: 1465499
Thanks for your response, I just need clarification,
This line gives me a complile error "wrong number of assignments"
sCard = UCase$(Trim$(Text1(k).Text))
it seems anywhere the text1(k).text) is, it halts on errors,

can you clarify?
thanks

0
 
LVL 2

Expert Comment

by:swilt
ID: 1465500
You need a control array of Text1
Put a text box on the form named Text1, set the index property to 0, copt and paste text1 onto the form 4 times to give Text1(1), Text1(2), Text1(3) and Text1(4) along with Text(0)

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now