0

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.

JerRat
  • 1
  • 3
    '0' is 48, '1' is 49... –  Jan 10 '23 at 08:57
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Jan 10 '23 at 10:48

2 Answers2

2

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 -------------------^
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • thank you, can you explain what exactly is changing when we subtract '0' from cards[i]? – JerRat Jan 10 '23 at 18:17
  • @JerRat `'0'` is the character `'0'`. If you subtract `'0'` from it, you'll get the actual number `0`. Since the codes for the digit characters are sequential, you can use this method to convert the character representing a digit to the digit itself. – Mureinik Jan 10 '23 at 18:19
0

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);
}
Bob__
  • 12,361
  • 3
  • 28
  • 42
  • Right but in the game of blackjack it is helpful sometimes to play an ace as a 11 over a one and vice versa. So yes I did go out of my way to implement the ace being a 1 or 11. Thank you though!! – JerRat Jan 10 '23 at 18:21
  • @JerRat True, then you just have to [refactor the function](https://godbolt.org/z/eaqaeWbbn), as always ;) – Bob__ Jan 10 '23 at 20:04