Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9255863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:48:20+00:00 2026-06-18T11:48:20+00:00

I’m working on a very small blackjack game in PHP. I’m currently writing the

  • 0

I’m working on a very small blackjack game in PHP.

I’m currently writing the function to count the cards, but the aces are kicking my butt.

My cards are all in an array like this:

$card_array = array( "ca", "c10", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9",
    "cj", "ck", "cq", "da", "d10", "d2", "d3", "d4", "d5", "d6", "d7", "d8",
    "d9", "dj", "dk", "dq", "ha", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9",
    "hj", "hk", "hq", "sa", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9",
    "s10", "sj", "sk", "sq");`

Where the first character is the suit and everything after that is the card (j for jack, etc)

Here’s what I have for counting the values up so far:

function bj_calculate_values( $cards ) {
    $card_values = 0;
    foreach ($cards as $card) {
        if (substr($card, 1) == "k" || substr($card, 1) == "q" || substr($card, 1) == "j") {
            $card_values += 10;
        }
        else if (substr($card, 1) != "a") {
            $card_values += substr($card, 1);
        }
    }
}

Originally, I also had the ace in there valued at 11, but obviously that would cause problems. I feel like I need to keep relooping to make sure the aces don’t put us over 21, but I’m not entirely sure of the best way to go about doing that.

I’d appreciate some input, guys.
EDIT:

The best I can think of is adding this to the function

foreach ($cards as $card) {
    if (substr($card, 1) == "a") {
        if ($card_values + 11 <= 21) {
            $card_values += 11;
        }
        else {
            $card_values += 1;
        }
    }
}

Actually I think this might work. Give me a few minutes to test.

EDIT:

Nope, didn’t work. 4, ace, ace, 6 came out to 22 with this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:48:21+00:00Added an answer on June 18, 2026 at 11:48 am

    I do not know much about BlackJack, but this is my try.

        function bj_calculate_values( $cards ) {
            $card_values = 0;
            $ace = 0;
            foreach ($cards as $card) {
                $val = substr($card, 1);
                if (!is_numeric($val))
                {
                        if ('a' == $val)
                        {
                                $val = 0;
                                $ace ++;
                        }
                        else
                                $val = 10;
                }
                $card_values    += $val;
            }
            while ($ace)
                $card_values += ($card_values + 11*$ace-- <= 21) ? 11: 1;
    
    
            return $card_values;
        }
    

    UPDATE

    If I understand your other comment, you want 6 4 A A A to come out 13, 6 4 A A to come out 12, and 6 4 A to come out 21. So the number of remaining aces count. Modified the source accordingly.

    Explanation

    First of all we calculate the value of non-ace cards. These are constant, so we get it over with. And this total is the (preliminary) $card_values.

    Then we may have (or not) some aces. It makes sense to have a while($aces) so that if we have no aces, we do nothing.

    And now the question is, what do we do with those aces? Here is where your requisite comes in: the sum must fit into 21. This means that I can’t just say “I’m at 10, I have an ace, so it fits into 21” because there might be another ace after that. So I have to calculate what would the worst case be if I were to add in all remaining aces; and that’s of course the number of aces, times 11. While the worst case is still good (i.e., $card_values + 11*$ace is less than 21) I can add 11. While it is not, I have to add 1.

    It is a form of “greedy” algorithm: I fit in all the 1’s we can, while ensuring enough 11’s are left to reach the closest approach to 21 that does not exceed.

    I seem to remember (I might be wrong, heh) that there was a nasty bug in this implementation which will come out whenever NumberOfItems * LowestValue >= HighestValue (I’m not too sure about that “equals” though). But in this case, it would require NumberOfItems to be above 11/1 = 11 aces, and there are not so many aces in the deck, so we’re cool. And to be sure, the possible cases are five – from “no” to “four” aces in a hand – and easily tested for all sums of other cards:

        // J Q K are worth 10, so we can use 10 instead.
        // And a fake card with value of 0 stands for "nothing".
        // We use the suit of Klingon, with up to four identical aces :-D
    
        for ($first = 0; $first <= 21; $first++)
        {
            $hand = array("k$first");
            for ($aces = 1; $aces < 5; $aces++)
            {
                $hand[] = "ka";
                $value  = bj_calculate_values($hand);
    
                // Let us ponder the possible values of $value.
    
                if ($value <= 11)
                {
                        // This is an error: we could have gotten closer to 21
                        // by upvaluing an ace. A correct implementation will
                        // never enter here except in the case of a LONE ACE.
                        if (($first != 0) || ($aces != 1))
                            print "ERROR: " . implode(" ", $hand) . " = $value\n";
                }
                // We have a value from 12 to 21.
                // Could we have done better? What cards do we have?
                // If no aces, of course no. All card values are then fixed.
    
                // If at least one ace, interpreted as 11, again not:
                // cannot interpret it above 11, and interpret as 1 would LESSEN the score.
    
                // If at least one ace, interpreted as 1, were we to interpret it
                // as 11, we would be summing 10. And 12 would become 22, and blow.
    
                // So, from 12 to 21, the result MUST be correct.
    
                // Finally, $value more than 21.
                if ($value > 21)
                {
                       // This is admissible ONLY if we could have done no better, which
                       // means $value is given by the fixed cards' value, $first, plus 
                       // ALL ACES COUNTED AS ONES.
                       if ($value != ($first + $aces))
                            print "ERROR: " . implode(" ", $hand) . " = $value\n";
                 }
            }
        }
    

    The output (version 1)

    VERIFY: k0 ka = 11              Correct, we had a lone ace and gave it 11.
    VERIFY: k18 ka ka ka ka = 22    Correct, we had 18 in other cards and all A's to 1's.
    VERIFY: k19 ka ka ka = 22       Ditto, 19 in cards.
    VERIFY: k19 ka ka ka ka = 23    Ditto, 19 in cards.
    ...                             ...
    

    Current output (added code to only print errors):

    -- nothing :-)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.