card game 21

Create a program in which a user plays a simple version of the card ga

Page 1 of 1

9 Replies - 1792 Views - Last Post: 27 September 2009 - 06:01 PM Rate Topic: -----

#1 mabaton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-September 09

card game 21

Posted 26 September 2009 - 05:00 PM

I have seen this assignment posted before but I have tried to do this on my own. My code is posted below but is completely jacked up and I really don't know how to fix it. I have been staring at it and moving things around all day. Any help that is offered is appreciated.

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


Is This A Good Question/Topic? 0
  • +

Replies To: card game 21

#2 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: card game 21

Posted 26 September 2009 - 05:11 PM

There are two of this threads. Are you guyz on the same school?

Here is an error
while (wantCard == 'Y' || 'y')


should be
while (wantCard == 'Y' || wantCard == 'y')


What is the exact problem that you have?

This post has been edited by poncho4all: 26 September 2009 - 05:13 PM

Was This Post Helpful? 0
  • +
  • -

#3 mabaton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-September 09

Re: card game 21

Posted 26 September 2009 - 05:14 PM

View Postponcho4all, on 26 Sep, 2009 - 04:11 PM, said:

Here is an error
while (wantCard == 'Y' || 'y')


should be
while (wantCard == 'Y' || wantCard == 'y')


What is the exact problem that you have?


Yes there are two of these threads. I read the other one and didn't find much help there. I dont know who that person is, but I think the thread is a bit older than this one.

I think I have several problems but the most obvious is when I run the program it continually asks me if I want another card whether I say Y or N and it adds a random number to my cardTotal....it just keeps going and I have to cntr-c out of the program

This post has been edited by mabaton: 26 September 2009 - 05:21 PM

Was This Post Helpful? 0
  • +
  • -

#4 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: card game 21

Posted 26 September 2009 - 05:24 PM

Here is another error that might be affecting the program
  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;
		}
 

This function the while seems to be an infinite loop, and the if statement is wrong
  if( value == 14 ) value = 11;


What erros do you get when you modify this things?
Was This Post Helpful? 0
  • +
  • -

#5 mabaton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-September 09

Re: card game 21

Posted 26 September 2009 - 05:30 PM

View Postponcho4all, on 26 Sep, 2009 - 04:24 PM, said:

Here is another error that might be affecting the program
  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;
		}
 

This function the while seems to be an infinite loop, and the if statement is wrong
  if( value == 14 ) value = 11;


What erros do you get when you modify this things?


I fixed the == and now it just loops continuous with no input and says either "the computer wins sorry try again" or "You have 21. You win!" scrolling across the screen until I hit cntr-c
Was This Post Helpful? 0
  • +
  • -

#6 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: card game 21

Posted 26 September 2009 - 05:56 PM

Thought so what is this
  while(cards--)


and this
if(cards)


supposed to mean?
Was This Post Helpful? 0
  • +
  • -

#7 mabaton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-September 09

Re: card game 21

Posted 27 September 2009 - 04:12 PM

View Postponcho4all, on 26 Sep, 2009 - 04:56 PM, said:

Thought so what is this
  while(cards--)


and this
if(cards)


supposed to mean?

OK so I fixed a lot of things I think. I have changed so many things and have been staring at this all day, I lost my place.
Here's my updated code:

#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 cards = numberOfCards;
	int return_value = 0;
	int value = 0;

	for (int a = 1; a <= numberOfCards; a++)
	{		
		 
		value = Random(1,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'
{
	char hit = 'y';
	

	while (hit == 'y' || hit == 'Y')
	{//player has not busted
		cout<<endl;
		cout << "Do you want to hit?";
		cin>>hit;
		cout<<endl;

		if (hit == 'y')
		{ //player wants hit
			playerScore += dealCards(1,"You take a card "); // adds humanScore to dealCards()
		}
		if (hit == 'n')
		{ //players does not want hit
			cout << "You decide to stand";
		}
		if (playerScore > 21)
		{//player has busted
			cout << "You have gone over 21, You Lose";
			hit = 'n';
		}
	}
}
		


int Random(int lowerLimit, int upperLimit)
{
//returns a random number within the given boundary
	return 1 + rand() % (upperLimit - lowerLimit + 1);
}


and the output looks like this which is still not right:
8 , 6 , = 14
2 , 10 , = 12

Do you want to hit?y

2 ,
Do you want to hit?y

10 , You have gone over 21, You Lose
The computer wins, sorry try again.
Do you want to play another hand (y/n)?y
4 , 4 , = 8
10 , 2 , = 12

Do you want to hit?n

You decide to stand
The computer wins, sorry try again.
Do you want to play another hand (y/n)?
Was This Post Helpful? 0
  • +
  • -

#8 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: card game 21

Posted 27 September 2009 - 05:12 PM

Here is your code with some modification
#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 <= 21) && (person <= 21))
	   {		
			   if(person<house)
			   {break;}
			   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
			if(houseScore>21&&humanScore<=21)
				cout<<"You win!!!!";
			else
				cout << "The computer wins, sorry try again." << endl;
}



int dealCards(int numberOfCards, string message)
//This function deals the cards
{
	int cards = numberOfCards;
	int return_value = 0;
	int value = 0;

	for (int a = 0; a < numberOfCards; a++)
	{		
		 
		value = Random(1,10);
		cout << message << " "<<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'
{
	char hit = 'y';
   

	while (hit == 'y' || hit == 'Y')
	{//player has not busted
		cout<<endl;
		cout << "Do you want to hit?";
		cin>>hit;
		cout<<endl;

		if (hit == 'y')
		{ //player wants hit
			playerScore += dealCards(1,"You take a card "); // adds humanScore to dealCards()
			cout<<"Your score at the moment is: "<<playerScore<<endl;
		}
		if (hit == 'n')
		{ //players does not want hit
			cout << "You decide to stand"<<endl;
			cout<< "Your score is: "<<playerScore;
		}
		if (playerScore > 21)
		{//player has busted
			cout << "You have gone over 21, You Lose";
			hit = 'n';
		}
	}
}
	   


int Random(int lowerLimit, int upperLimit)
{
//returns a random number within the given boundary
	return 1 + rand() % (upperLimit - lowerLimit + 1);
}



Added some things in hit and dealcard to make the game more fair for the computer ;)
Was This Post Helpful? 1
  • +
  • -

#9 mabaton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-September 09

Re: card game 21

Posted 27 September 2009 - 05:22 PM

View Postponcho4all, on 27 Sep, 2009 - 04:12 PM, said:

Here is your code with some modification [code]#include <iostream>

Added some things in hit and dealcard to make the game more fair for the computer ;)


OMG! You are a lifesaver! Thank you so much!
Was This Post Helpful? 0
  • +
  • -

#10 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: card game 21

Posted 27 September 2009 - 06:01 PM

You did all the Job :P

I just modified your last pieces ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1