Programming Assignment
Write a modified version of Blackjack. This modified card game has cards numbered 1-
10. The player and dealer take turns, each receiving a card. The cards dealt should
alternate, between player and dealer, with the player receiving the first card, the dealer
receives the second card, etc. The program should then print out the player’s hand, as
well as only one of the dealer’s cards. The program prompts the user for another card.
The user can choose up to five cards total. After the user has finished “hitting” (the act of
getting another card), then the computer will deal its own hand, until either it arrives at
17, or busts by going over 21. Assume a “bottomless” deck.
If the user busts, display a message saying as much; don’t worry about printing out the
dealer’s hand. Otherwise, print out the dealer’s hand, and look at the possible options:
The dealer goes over 21 – the computer busts, and the player wins.
The player’s cards total more than the dealer’s cards – the player wins.
The dealer’s cards total more than the player’s cards – the dealer wins.
The player’s cards total the same as the dealer’s cards – it is a tie game.
Finally, prompt the user if he/she would like to play again.
Code:
#include <iostream>#include <ctime>
#include <string>
using namespace std;
//prototypes...
void blackjack();
int dealCards(int, string);
void hit(int &);
void determineWinner(int, int);
int Random(int, int);
int main(){
char keepPlaying = 'n'; //loop control variable
do {
blackjack();
//keep playing?
cout << "Would you like to play again (y/n)?";
cin >> keepPlaying;
} while(keepPlaying == 'y');
return 0;
}
void blackjack(){
//play one hand of 21
//randomize the cards
srand((int) time(0));
// deal the cards
int person = dealCards(2, "Your Cards:");
cout << " = " << person << endl;
int house = dealCards(2, "Computers Cards:");
cout << " = " << house << endl;
// Ask if human wants a hit and keep hitting...
hit(person);
cout << endl;
//Determine if computer takes a hit
while ((house < person) && (house <= 21) && (person <= 21))
{
house += dealCards(1,"The Computer takes a card ");
cout << endl;
}
//show who won....
determineWinner(person, house);
}
void determineWinner(int yourScore, int dealerScore)
//Compare the scores to see who won
{
if (yourScore == 21)
cout << "You have 21. You win!" << endl;
else if ((yourScore < 21) && (yourScore > dealerScore))
cout << "Dealer busts; You win!" << endl;
else
cout << "You lose" << endl;
}
int dealCards(int numberOfCards, string message)
//This function deals the cards
{
int return_value = 0;
int value = 0;
for (int b = 0; b <= numberOfCards; b++)
{
int cards = b;
while(cards--)
{
value = Random(0,10);
cout << value << " ";
if(cards)
cout << " , ";
return_value += value;
}
}
return return_value;
}
void hit(int &playerScore)//This function asks you if you want another card -- 'a hit'
{
int cardCount = 0;
char wantCard = "y" || "n";
int cardTotal = 0;
cardTotal = playerScore;
cout << "Would you like another card?";
while (wantCard == 'y')
{
if ((cardTotal > 0 ) && (cardTotal < 21))
;
cout << "Do you want another card?";
cin >> wantCard;
if (wantCard == 'y')
cout << cardTotal + dealCards(1, "You take a card."); // adds yourScore to dealCard()
else
cout << "You decide to stay";
if (cardTotal > 21)
cout << "You have gone over 21, You Lose";
}
}
int Random(int upperbound, int lowerbound)
{
int result;
//returns a random number within the given boundary
result= 1 + rand() % (upperbound - lowerbound + 1);
return result;
}
[code]
my output:
7 7 , 2 = 16
9 8 , 2 = 19
Would you like another card?
You lose
Would you like to play again (y/n)?
[/code}
Code:
supposed to be:
Your hand: 2 5
Dealer's hand: 6
Would you like another card? (y/n) y
Your hand: 2 5 5
Would you like another card? (y/n) y
Your hand: 2 5 5 6
Would you like another card? (y/n) n
Dealer's hand: 6 8 10
Dealer busts; you win
Would you like to play again? (y/n)