Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do i do this?

Posted on 2004-10-17
7
Medium Priority
?
147 Views
Last Modified: 2010-05-02
I have a list box of numbers and letters that are suppose to be cards (BlackJack 21):

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A

J, Q, K = 10
A = 11 or 1

How do i make it auto detect what the ACE should be if totaling the cards dosnt go over 21 and having it read how many aces are in the hand.

The problem I'm having is after totaling up the (ACES already in hand) values for the cards and then another ACE gets drawn the old ace remains a 10 which makes it go over 21. For example:

first calulation would add these cards as 8:
1, A, 6 = 18  

then another card is drawn:
1, A, 6, K = 28
After the King is drawn it automatically brings it over 21 because the previous ACE was a 11.

Lets say 2 more cards were drawn:
1, A, 6, K , A, 1

if added right this would of been a perfect hand
but for me its adding 30 because its only deciding on the last ace.

I need it something like this: (lets pretend this was possible in a hand)
A,A,A = 13
A,A,A,A = 14
A,A,A,A,A = 15..etc
A,A,A,A etc...(21 x A's) = 21

Having it autodetect the best use for A's (1 or 11) for each A in the deck.
How can i make it make the best decision for totaling up the values to have it =21 or <21 or closest to 21 with multiple aces.

0
Comment
Question by:andyakira
[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
  • Learn & ask questions
7 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 12333871

so if ace is drawn get the value

A,A should = 12

a = 11  then ace ir drawn so if next card = Ace then another If > 21 then ace = 1

Just add a condition if ace is the next drawn card
0
 
LVL 76

Accepted Solution

by:
David Lee earned 2000 total points
ID: 12334248
This may not be the shortest or cleanest way to do the calculation, but it does work.  I used an array here instead of a listbox, but you can modify that easily enough.  The key is counting upi the number of aces in the hand while totalling up the rest fo the cards, then taking a look at the number of aces and calculating the total based on the number of aces present.  You can loop through this as each card is added to the hand.

Private Sub Command1_Click()
    Dim aCards(), intTotal, intAceCount
    aCards = Array("3", "A", "5", "A")
    For x = 1 To UBound(aCards)
        Select Case aCards(x)
            Case "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"
                intTotal = intTotal + Val(aCards(x))
            Case "J", "Q", "K"
                intTotal = intTotal + 10
            Case "A"
                intAceCount = intAceCount + 1
        End Select
    Next
    If intAceCount > 0 Then
        Select Case intAceCount
            Case 1
                If (intTotal + 11) > 21 Then
                    intTotal = intTotal + 1
                Else
                    intTotal = intTotal + 11
                End If
            Case 2
                If (intTotal + 12) > 21 Then
                    intTotal = intTotal + 2
                Else
                    intTotal = intTotal + 12
                End If
            Case 3
                If (intTotal + 13) > 21 Then
                    intTotal = intTotal + 3
                Else
                    intTotal = intTotal + 13
                End If
            Case 4
                If (intTotal > 14) > 21 Then
                    intTotal = intTotal + 4
                Else
                    intTotal = intTotal + 14
                End If
        End Select
    End If
    If intTotal > 21 Then
        MsgBox "Bust"
    End If
End Sub
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12335846
just have to ask, which card in the deck is the 1 that you show in your original list:

1,2,3,4,5,6,7,8,9,10,J,Q,K,A ==> 14 cards per suit (there are only 13, 2...10,J,Q,K,A - there is no 1)

AW
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:andyakira
ID: 12337244
sorry for the delayed response,  and thank you BlueDevilFan for showing me a way to do this.
Arthur_Wood, I didnt know that. thank you for informing me.
0
 

Author Comment

by:andyakira
ID: 12337248
I don't play much cards
0
 
LVL 76

Expert Comment

by:David Lee
ID: 12342807
I had to laugh when I read your post, Arthur_Wood.  I do play cards and still didn't notice.  Too fixated on solving the ace issue and didn't notice the 1.  Good catch!

After looking at the code I posted I quickly saw that there's an easy way to cut out a bunch of it.  Here the revised, and much shorter, code.



Option Base 1

Private Sub Command1_Click()
    Dim aCards(), intTotal, intAceCount
    aCards = Array("3", "A", "5", "K")
    For x = 1 To UBound(aCards)
        Select Case aCards(x)
            Case "2", "3", "4", "5", "6", "7", "8", "9", "10"
                intTotal = intTotal + Val(aCards(x))
            Case "J", "Q", "K"
                intTotal = intTotal + 10
            Case "A"
                intAceCount = intAceCount + 1
        End Select
    Next
    If intAceCount > 0 Then
        If (intTotal + 10 + intAceCount) > 21 Then
            intTotal = intTotal + intAceCount
        Else
            intTotal = intTotal + 10 + intAceCount
        End If
    End If
    If intTotal > 21 Then
        MsgBox "Bust"
    End If
End Sub
0
 

Author Comment

by:andyakira
ID: 12344615
Thank you Blue, I'll give this one a try as well. great job
and LOL at the 1card incident.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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

604 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