Solved

Help on Loop

Posted on 1998-07-11
198 Views
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

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
Question by:checkout
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 6

Expert Comment

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

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

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

Author Comment

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

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

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month6 days, 10 hours left to enroll