Create a program in which a user plays a simple version of the card game 21 against the computer. Each player is initially dealt two cards from an unlimited deck. Random numbers will represent the cards from 1 to 10. After seeing their hand the user then the computer are given the opportunity to take additional cards. The hand that comes the closest to 21 without exceeding 21 wins the game. A draw results if both players have the same score.
Here is the sample output from the program:
Your Cards: 5 6 = 11
Computers Cards : 4 5 = 9
Do you want another card (y/n)? y
Hit: 5 Your total is 16
Do you want another card (y/n)? y
Hit: 4 Your total is 20
Do you want another card (y/n)? n
The computer takes a card: 9
The computer takes a card: 5
Your score: 20
Computer score: 23
You Won!
To complete this exercises, provide the required code for the following functions: dealCards, hit, and determineWinner.
Here is what I have and the output looks nothing like it should but I do not know which function to look at first or where exactly my problem is.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
//prototypes...
void play21(void);
int dealCards(int, string);
void hit(int &);
void determineWinner(int, int);
int Random(int, int);
void main(){
char keepPlaying = 'n'; //loop control variable
do {
play21();
//keep playing?
cout << "Do you want to play another hand (y/n)?";
cin >> keepPlaying;
} while(keepPlaying == 'Y' || keepPlaying == 'y');
}
void play21(void){
//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 humanScore, int houseScore)
//Compare the scores to see who won
{
if (humanScore == 21)
cout << "You have 21. You win!" << endl;
else if ((humanScore < 21) && (humanScore > houseScore))
cout << "You have the closer hand to 21. You win!" << endl;
else
cout << "The computer wins, sorry try again." << endl;
}
int dealCards(int numberOfCards, string message)
//This function deals the cards
{
int return_value = 0;
int value = 0;
for (int a = 0; a <= numberOfCards; a++)
{
int cards = a;
while(cards--)
{
value = Random(0,10);
cout << value << " ";
if(cards)
cout << " , ";
return_value += value;
}
}
return return_value;
}
void hit(int &playerScore)//This function asks the human if they 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' || wantCard == 'y')
{
if ((cardTotal > 0 ) && (cardTotal < 21))
cardCount += 1;
cardTotal += Random(0,10);
cout << "Your total is: " << cardTotal;
cout << "Do you want another card?";
cin >> wantCard;
if (wantCard == 'Y' || wantCard == 'y')
cout << cardTotal + dealCards(1, "You take a card."); // adds humanScore to dealCard()
else
cout << "You decide to stand";
if (cardTotal > 21)
cout << "You have gone over 21, You Lose";
}
}
int Random(int lowerLimit, int upperLimit)
{
//returns a random number within the given boundary
return 1 + rand() % (upperLimit - lowerLimit + 1);
}
| |
and here is the output:
1 10 , 8 = 19
7 10 , 3 = 20
Would you like another card?
The computer wins, sorry try again.
Do you want to play another hand (y/n)?y
11 4 , 11 = 26
5 1 , 2 = 8
Would you like another card?
The computer wins, sorry try again.
Do you want to play another hand (y/n)?n
Press any key to continue . . .
I do not get to answer the question Would you like another card.