Blackjack C++ help

Hello, I am currently writing a blackjack game in C++. I don't know how I can incorporate cardName function in my program.

Note: All face cards (Jack, Queen, King) count as 10.


// cardValue
// determines the value of a card for purposes of totaling a hand
// all face cards count as 10; aces count here as 1
// Parameter:
// card (input int) encoded card value 0-51
// Returns:
// an integer value in the range 1-10

// cardName
// Determines the name of a card, such as "Three of Spades"
// Parameter:
// card (input int) encoded card value 0-51
// Return Value
// a string value, obtained with the help of the assign() and
// and append() methods described in <string>

1
2
3
4
5
6
7
8
9
10
  int cardValue(int card)
{
  if (card < 9)
       return card;
  else if (card > 9)
       return 10;

}

// Implement cardName 
Last edited on
What a card's number is and what its value is are two separate things. (As you have figured out.)

Since the function returns an integer, you cannot return a string.

You need to write a function that takes a whole hand and returns its value. That way you can handle the soft ace.

Remember your modular arithmetic:

n / 13 = suit
n % 13 = rank

You need an array of names for each.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const char* suits[] =
{
  "Clubs",
  "Diamonds",
  "Hearts",
  "Spades"
};

const char* ranks[] =
{
  "Ace",
  "Two",
  "Three",
  ...
};

You can easily assemble a name:

1
2
3
4
5
6
7
8
9
10
string name_of( int card )
{
  if (card < 0 || card > 51) return "Joker";  // or "error" or something

  string result;
  result += ranks[ card % 13 ];
  result += " of ";
  result += suits[ card / 13 ];
  return result;
}

Hope this helps.
Are const char* suits [] and char* ranks[] are global constants?
Sure, if that works for you. They don't have to be.
Hi @jshm415,

I have a code
of a BlackJack
game simulation but
is so big to be
posted here.

Send me a PM
or email me if
you are interested.

Topic archived. No new replies allowed.