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.
and here is the starting code:
//Specification: This program plays a version of
//the card game of 21.
//A human player is pitted against the computer.
//The player who is the closest to 21 without
//going over wins the hand.
#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 anouther 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
//Both the human and the house score totals are provided as arguments
//Display total scores and indicate winner
//possible outcomes: human wins, computer wins, tie
}
int dealCards(int numberOfCards, string message){
//This function deals the cards
//The number of cards to be dealt is provided as an argument
//A message indicating which player is receiving the cards is also
//given as an argument
//The player message and the cards dealt is displayed to the screen
//the total value of the dealt cards is returned
}
void hit(int &playerScore){
//This function asks the human if they want another card -- 'a hit'
//the player's score total is accumulated as they take cards
//the player can continue taking cards until they wish to stop or they exceed 21
//After a card is taken (use the dealCards function) the user's current total is displayed
//If the user goes over 21 'busted' is displayed
}
int Random(int lowerLimit, int upperLimit) {
//returns a random number within the given boundary
return 1 + rand() % (upperLimit - lowerLimit + 1);
}
the pseudocode:
Start
hit = yes
while loop (until hit)
If the player has not busted
Does the player want a hit?
If players wants hit
Add playerscores to dealcard function output
If players does not want hit
Display
If the player has busted
Display
hit = No
End of while loop
End
and what I have so far
//Specification: This program plays a version of
//the card game of 21.
//A human player is pitted against the computer.
//The player who is the closest to 21 without
//going over wins the hand.
#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(){ //line 17
char keepPlaying = 'n'; //loop control variable
do {
play21();
//keep playing?
cout << "\nDo 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;
// line 41
// 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);
} // line 56
void determineWinner(int humanScore, int houseScore) {
//Compare the scores to see who won
while ((humanScore <= 21) && (humanScore < 0))
if (humanScore == 21) {
cout << "you have 21, You win!" << endl;
}
else if((humanScore < 21) && (humanScore > houseScore)) {
cout << "You are closest to 21, you win!" << endl;
}
//Both the human and the house score totals are provided as arguments
cout << "here are your scores: ";
cout << "your score is: " << humanScore << endl;
cout << "the house's score is: " << houseScore << endl;
//Display total scores and indicate winner
if(humanScore <= 21)
cout << "Congratulations, You win!";
else
cout << "Sorry, the house wins!";
if((humanScore > 21) && (houseScore > 21))
cout << "No-one wins, it is a tie!" << endl;
//possible outcomes: human wins, computer wins, tie
// line 81
}
int dealCards(int numberOfCards, string message){
//This function deals the cards
//The number of cards to be dealt is provided as an argument
//A message indicating which player is receiving the cards is also
//given as an argument
//The player message and the cards dealt is displayed to the screen
//the total value of the dealt cards is returned
int ret_val = 0, val;
int cards = numberOfCards;
cout << message;
while(cards--){
val = Random(0,14);
if(val > 10) val = 10;
if(val == 1) val = 11;
cout << val;
if(cards)
cout << ",";
ret_val+=val;
} // line 104
return ret_val;
}
void hit(int &playerScore){
//This function asks the human if they want another card -- 'a hit'
//the player's score total is accumulated as they take cards
//the player can continue taking cards until they wish to stop or they exceed 21
//After a card is taken (use the dealCards function) the user's //current total is displayed
//If the user goes over 21 'busted' is displayed
}
int Random(int lowerLimit, int upperLimit) {
//returns a random number within the given boundary
return 1 + rand() % (upperLimit - lowerLimit + 1);
}
I have the line numbers written in for my own use since my compiler doesn't display them. What I need is help determining where to go from here, and how to get it going. I think (not positive, but I think, that I just need the hit function at the bottom. Can anyone please help me get that going, please? Thanks guys! ~God bless!

New Topic/Question
Reply




MultiQuote






|