Home > Mobile >  Why is my math coming out weird in this blackjack function?
Why is my math coming out weird in this blackjack function?

Time:01-10

So I was on Edabit chillin doing so fun little exercises before I went to bed, having a blast then I stated a blackjack challenge. Thinking it would be a fun one to end the night with. That was until i made this monstrosity. The prompt was:

Create a function that takes an array of card numbers and checks if the sum of their value exceeds 21. If the sum exceeds 21, return true and if the sum is under or equal to 21, return false. Values of the cards are as follows:

2-10 are their value. J-K (face cards) count as 10. Aces count either as 1 or 11 - play conservatively, so that if giving an ace a value of 11 causes you to lose and 1 allows you to win, then go with 1.

The code would be tested with these inputs:

    That(overTwentyOne({'A', '2', '3'})
    That(overTwentyOne({'A', 'J', 'K'})
    That(overTwentyOne({'A', 'J', 'K', 'Q'})
    That(overTwentyOne({'5', '3', '6', '6', '7', '9'})

simple enough right??

#include <iostream>
#include <vector>
using namespace std;
bool overTwentyOne(std::vector<char> cards);
int main()
{
    int player_1 = 10;
    int player_2 = 10;
    int player_3 = 10;
    int player_4 = 10;
    
    std::vector<char> player1 = {'A', '2', '3'};
    std::vector<char> player2 = {'A', 'J', 'K'};
    std::vector<char> player3 = {'A', 'J', 'K', 'Q'};
    std::vector<char> player4 = {'5', '3', '6', '6', '7', '9'};
    
    
    cout << "Player 1\n";
    player_1 = overTwentyOne(player1);
    cout << player_1;
    cout << "Player 2\n";
    player_2 = overTwentyOne(player2);
    cout << player_2;
    cout << "Player 3\n";
    player_3 = overTwentyOne(player3);
    cout << player_3;
    cout << "Player 4\n";
    player_4 = overTwentyOne(player4);
    cout << player_4;
}


bool overTwentyOne(std::vector<char> cards) {
    int player_total = 0;
    bool ace_in_play = false;
    
    // iterate thru vector
    for (int i = 0; i < cards.size(); i  ) {
        //check for face card
        if (cards[i] == 'J'|| cards[i] == 'Q' || cards[i] == 'K') {
            player_total = player_total   10;
        }
        //check for ace
        else if (cards[i] == 'A') {
            player_total = player_total   11;
            ace_in_play = true;

        }
        //add number cards
        else 
        {
            player_total = player_total   cards[i];
        }
        
    }
    
    //verifies that the player hand is NOT over 21
    if (player_total <= 21) {
        return false;
    }
    
    //verifies that playe hand is over 21
    else if (player_total > 21) {
        //makes sure that player doesnt have a ace and if not the player will be over 21
        if (ace_in_play == false) {
            return true;
        }
        //checks to see if the ace is worth 1, whether or not the player is still over 21
        else if (ace_in_play == true) {
            player_total -= 10;
            if (player_total <= 21) {
                return false;
            }
            else{ 
            return true;
            }
        }
    }
    return NULL;
}

So everything inside main doesn't matter and is used for troubleshooting, when I was messing around with inputs player2 and player3 were adding up correctly. but player1 and player4 were adding up completely wrong. The function was identifying cards correctly but during the addition the numbers were getting crazy...

player1: identifies card 1 as 11 players total now 11 identifies card 2 as 2 now players total is 61 identifies card 3 as 3 now players total is 112

player4: identifies card 1 as 5 player total is now 53 identifies card 2 as 3 player total is now 104 identifies card 3 as 6 player total is now 158 identifies card 4 as 6 player total is now 212 identifies card 5 as 7 player total is now 267 identifies card 6 as 9 player total is now 324

it is now 3am and I can't figure this out please help lol! also this is my first post on here so idk if I did everything correctly.

CodePudding user response:

The cards are chars, not ints. You're summing the codepage of the characters '0' - '9' instead of the digits they represent. I.e.:

player_total = player_total   cards[i] - '0';
// This was missing -------------------^

CodePudding user response:

Besides the error pointed out in Mureinik's answer, you may have missed a key point in the problem statement:

Create a function that takes an array of card numbers and checks if the sum of their value exceeds 21. If the sum exceeds 21, return true and if the sum is under or equal to 21, return false.

That's the only thing that function has to do, so you don't really need to try all the possible values an ace (or more than one) can have, just use 1.

You aren't asked to check if the maximum value of the cards is less than or equal to 21.

#include <numeric>

bool over_twenty_one(std::vector<char> const& cards)
{
  constexpr auto sum_min_value = [](int sum, char card) -> int
  {
    if ( card == 'J' or card == 'Q' or card == 'K' )
    {
      return sum   10;
    }
    if ( '1' < card  and  card <= '9' )
    {
      return sum   card - '0';
    }
    if ( card == 'A' )
    {
      return sum   1;
    }
    return sum;
  };

  return 21 < std::accumulate(cards.cbegin(), cards.cend(), 0, sum_min_value);
}
  • Related