Question: Design a program to calculate the current value of a blackjack hand. The program should read numbers from the user representing each card being added
Design a program to calculate the current value of a blackjack hand.
The program should read numbers from the user representing each card being added to the hand. Use A for Ace, 2 for Two, 10 for 10, J for Jack, Q for Queen, K for King.
After you read a card, display the best total for the hand.
Stop reading cards if the user enters S or the value of the hand exceeds 21.
A few notes
An ace can be worth 1 or 11.
A two is worth 2, a three is worth 3, a ten is worth 10
A jack, queen, or king is worth 10
When you are totaling the cards, you need to consider whether each ace should be counted as 1 or 11. You want the hand to total the largest value that is under 21. For example an ace and a three should be 14 not 4. But an ace, a five, and a queen should be 16 not 26.
Basically I have this psuedocode and I am having trouble turning it into a C++ program:
Declare string userInput. Its function should be obvious from the name alone. I am a genius with variable names I know, but more power to you if you think of better ones.
Make an integer variable called endGameStatus.
Initialize two arrays. One being a string array with some size it should never reach (lets say 25 cards, since you can never get that many cards and still be under 21). Lets name it cardArray.
cardArray = { , , , , , , , , , }
The other array is named arrayWhoAreYou which consists of strings:
arrayWhoAreYou = {A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K,}
Proclaim some other variables such as i, cardCount, tempSlot, patienceInt and tempI
Set up 3 integer variables that will hold a total value. Lets call them totalA, totalB, and totalC for simplicity. Why three of them? Itll make sense later, and its the lazy solution to a later problem.
Lastly, avow a Boolean called firstTurn as true, just so we can deal cards at the start of the game, and not worry about that later.
Write a Boolean controlled loop (which will be massive with multiple other loops inside of it.) Make this either a do-while or while loop. Basically this loop does all the work, and only stops when some checks set the Boolean to false. (Lets call it boolCont for now.)
While boolCont is true (I prefer while loops, but do what you prefer.)
{
If firstTurn is true
{
Set tempSlot to a randomly generated number from 1-13
Set cardArray[i] to arrayWhoAreYou[tempSlot]
Increment i
Set tempSlot to a randomly generated number from 1-13
Set cardArray[i] to arrayWhoAreYou[tempSlot]
Set firstTurn to false
Increment i
}
Set tempI to i
Reset i to 0, print the line, and cycle through the array to print out the players current hand.
Print Your hand is:
For i = 0, i less than or equal to cardCount
{
Print cardArray[i]
Increment i
}
Reset i to tempI, and end the print line
Prompt for user input
Input userInput
If userInput = H
{
Set tempSlot to a randomly generated number from 1-13
Set cardArray[i] to arrayWhoAreYou[tempSlot]
Increment i
}
Set tempI to i then set i back to 0
While doMath is true
{
If cardArray[i] is
{
Set doMath to false
}
Else
{
If cardArray[i] is 2
{
totalA = totalA + 2
}
If cardArray[i] is 10
{
totalA = totalA + 10
}
If cardArray[i] is J
{
totalA = totalA + 10
}
If cardArray[i] is Q
{
totalA = totalA + 10
}
If cardArray[i] is K
{
totalA = totalA + 10
}
If cardArray[i] is A
{
If patienceInt less than or equal to 2
{
totalA = totalA + 11
Increment patienceInt
}
Else
{
totalA = totalA + 1
}
}
}
}
Set doMath to true
Reset i and patienceInt to 0
Reset i to tempI
if totalA is 21
{
boolCont to false
}
if totalB is 21
{
boolCont to false
}
if totalC is 21
{
boolCont to false
}
if userInput is S
{
boolCont to false
break
}
}
If totalA is 21 AND greater than or equal to totalB AND greater than or equal to totalC
{
Set endGameStatus to 1
}
If totalB is 21 AND greater than or equal to totalA AND greater than or equal to totalC
{
Set endGameStatus to 1
}
If totalC is 21 AND greater than or equal to totalA AND greater than or equal to totalB
{
Set endGameStatus to 1
}
Now that all those end of game checks have been tested, just make a switch statement to check and pass whatever endGameStatus is now. If its a successful game with one of the totals being 21, then print out a win statement (or ASCII if you want to be fancy.)
A (Ace) = 1 or 11
2 (Two) = 2
3 (Three) = 3
4 (Four) = 4
5 (Five) = 5
6 (Six) = 6
7 (Seven) = 7
8 (Eight) = 8
9 (Nine) = 9
10 (Ten) = 10
J (Jack), Q (Queen), K (King) = 10
S = Hold/End the Game
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
