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.
#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
{
while ((humanScore <= 21) && (humanScore > 0))
{
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
{
for (int a = 0; a <= numberOfCards; a++)
{
int return_value = 0;
int value = 0;
int cards = numberOfCards;
while(cards--)
{
value = Random(0,14);
if (( value >= 10 ) && (value < 14)) value = 10;
if( value = 14 ) value = 11;
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 = 'n';
int cardTotal = 0;
cardTotal = playerScore;
cout << "Would you like another card?";
while (wantCard == 'Y' || 'y')
{
if ((cardTotal > 0 ) && (cardTotal <= 21))
cardCount += 1;
cardTotal += Random(0,14);
cout << " " << cardTotal << endl << "Would you like another card?";
cin >> wantCard;
if (cardTotal > 21)
cout << "You are over 21. You lose!" << endl;
}
}
int Random(int lowerLimit, int upperLimit)
{
//returns a random number within the given boundary
return 1 + rand() % (upperLimit - lowerLimit + 1);
}
When compiled it gives the warning C4715: 'dealCards': not all control paths return a value, but I do not think this is my only problem since when I run the program it continually asks me if I want another card whether I say Y or N and I have to cntr-c out of the program
This post has been edited by mabaton: 26 September 2009 - 05:05 PM

New Topic/Question
Reply




MultiQuote





|