Scoring Blackjack in C++: Handling Aces in a Card Game Program

  • Comp Sci
  • Thread starter wahaj
  • Start date
  • Tags
    C++
In summary, the program inputs ten, kind, queen, jack values and a. If the user inputs a number that is not one of these values, the program calculates the score and updates the corresponding variable. If the user inputs a number that is one of these values, the program calculates the score and updates the corresponding variable if the number is a number between 1 and 9, a number between 10 and 19, or an ace. If the user inputs a number that is not one of these values, the program prints an error message and exits.
  • #1
wahaj
156
2

Homework Statement



I am writing a program in c++ where it inputs card values from the user and stores it in a char type variable. valid inputs are numbers 1-9, t,k,q,j for ten, kind, queen, jack respectively and a for ace. after inputting all the values the program goes like this
Code:
if (!(card1 == 'a'))
	{
		switch (card1)
		{
		case '1':
			score = score + 1;
			break;
		case '2':
			score = score + 2;
			break;
		case '3':
			score = score + 3;
			break;
		case '4':
			score = score +4;
			break;
		case '5':
			score = score + 5;
			break;
		case '6':
			score = score + 6;
			break;
		case '7':
			score = score + 7;
			break;
		case '8':
			score = score + 8;
			break;
		case '9':
			score = score + 9;
			break;
		case 't':
		case 'j':
		case 'k':
		case 'q':
			score = score + 10;
			break;
		default:
			break;
		}
the code is the same for card 2,3,4, and 5. The problem I am having is that I don't know how to deal with aces. if there is only one ace I can simply go like this
Code:
if (card1 == 'a' || card2 == 'a' || card3 == 'a' || card 4 == 'a' || card5 = 'a')
{
      if (21-score <= 11)
         {
              score = score + 11;
          }
       else 
            {
               score = score +1;
             }
}
but what if there are multiple aces? then I have to decide which ace counts as 1 and which 11. I can do that by reiterating the above statement. but I don't know how to find out how many aces I have aside from using brute force where I list 20 cases for every combination of cards that might end up being aces.
On a side note I have never played blackjack before so I am just going by the brief description of the game given in the question. Also this is my first time using switch statement so I don't know I did used it right.
 
Physics news on Phys.org
  • #2
Hi wahaj! :smile:

Why don't you count each ace as 1 point?
But do keep track if you have at least one ace.
At the end, if the score is 11 or lower, and you have at least one ace, add 10 points.

Your switch statement is fine.
However, duplicating it 5 times is bad programming practice.
 
  • #3
hmm that might work. Also do you by any chance have a better solution to the switch statements. After all no body wants to read 300 lines of switch statements.
 
  • #4
Put the cards in an array, and use a for-loop to iterate over them.
Then you can use card in the switch-statement.
 
  • #5
Right, except arrays are still 3 chapters away . I probably should have mentioned that I only know loops and if else statements and variables so far. Thanks anyways.
 
  • #6
Ah, I guess it's a little early to talk about good programming practices then.

Good luck!
 

Related to Scoring Blackjack in C++: Handling Aces in a Card Game Program

1. How do I calculate the score in blackjack using C++?

The score in blackjack is calculated by adding up the value of all the cards in a player's hand. In C++, you can assign a numerical value to each card (e.g. A = 1 or 11, J/Q/K = 10) and use a loop to iterate through the player's hand and add up the values. Remember to account for the special case of the Ace card having a value of either 1 or 11.

2. What happens if the score in blackjack exceeds 21?

If the score in blackjack exceeds 21, it is considered a "bust" and the player automatically loses the round. In C++, you can use an "if" statement to check if the score exceeds 21 and handle the bust accordingly.

3. How do I handle the dealer's hand in blackjack scoring?

The dealer's hand in blackjack follows a set of rules, so you can use conditional statements in C++ to determine whether the dealer should hit or stand based on their current score. For example, if the dealer's hand is 16 or less, they must hit, but if their hand is 17 or more, they must stand. You can also use a loop to keep dealing cards to the dealer until they reach a score of 17 or higher.

4. Is there a way to account for the face cards in blackjack scoring?

Yes, in C++ you can assign a numerical value to each face card (e.g. J/Q/K = 10) and use a conditional statement to check if the card is a face card. If it is, you can add 10 to the player's score instead of the face card's default value of 1.

5. Can I implement a betting system in my blackjack program using C++?

Yes, you can implement a betting system in your C++ blackjack program by using variables to keep track of the player's current balance and the amount they bet on each round. You can then use conditional statements to determine if the player wins or loses the bet based on the outcome of the round.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
Replies
4
Views
899
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
979
  • Engineering and Comp Sci Homework Help
Replies
13
Views
9K
  • Programming and Computer Science
Replies
1
Views
4K
Back
Top