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

  • Context: Comp Sci 
  • Thread starter Thread starter wahaj
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around handling the scoring of Aces in a Blackjack program written in C++. Participants explore how to manage the value of Aces when multiple Aces are present among the cards inputted by the user, as well as general programming practices related to the implementation of the scoring system.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • The original poster describes a method for scoring cards but is uncertain about how to handle multiple Aces, questioning the need for brute force solutions.
  • One participant suggests counting each Ace as 1 point and adding 10 points at the end if the score is 11 or lower and at least one Ace is present.
  • The original poster expresses interest in a more efficient solution to the switch statements used for scoring.
  • Another participant recommends using an array to store card values and iterating over them with a for-loop to simplify the scoring process.
  • The original poster notes their current knowledge limitations, indicating that arrays are not yet covered in their studies.
  • A later reply acknowledges the timing of discussing programming practices given the original poster's current skill level.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to handle multiple Aces, and there are varying suggestions on programming practices. The discussion remains unresolved regarding the optimal solution for scoring Aces.

Contextual Notes

The original poster's understanding of programming concepts is limited to loops, if-else statements, and variables, which may affect their ability to implement suggested solutions involving arrays.

wahaj
Messages
154
Reaction score
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
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.
 
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.
 
Put the cards in an array, and use a for-loop to iterate over them.
Then you can use card in the switch-statement.
 
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.
 
Ah, I guess it's a little early to talk about good programming practices then.

Good luck!
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 13 ·
Replies
13
Views
10K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K