A Game of Twenty-One

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

Page 1 of 1

10 Replies - 2876 Views - Last Post: 27 September 2009 - 02:15 PM Rate Topic: -----

#1 LadyRae  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 19-September 09

A Game of Twenty-One

Posted 26 September 2009 - 12:56 AM

I would like a little help in getting my code moving on, if anyone would be willing.. Our instructions are this:
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!

Is This A Good Question/Topic? 0
  • +

Replies To: A Game of Twenty-One

#2 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: A Game of Twenty-One

Posted 26 September 2009 - 01:12 AM

View PostLadyRae, on 26 Sep, 2009 - 06:56 AM, said:

void main(){


Seriously? Your teacher gave you this code? main should always return int, not void. Tell them off for me, will you?

I've only taken a look at one random function, but it's got a couple of problems already:

View PostLadyRae, on 26 Sep, 2009 - 06:56 AM, said:

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;
}


What's the while condition for?
if humanScore < 0, then it will execute the else infinitely.
if humanScore >= 0, then it will do nothing and return.
Get rid of the while line, it's frightening me. Also, there are three conditional messages you have to print:
else {
	cout << "The computer won. How about a nice game of chess?" << endl;
}


Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: A Game of Twenty-One

Posted 26 September 2009 - 01:29 AM

View Postmoopet, on 26 Sep, 2009 - 12:12 AM, said:

View PostLadyRae, on 26 Sep, 2009 - 06:56 AM, said:

void main(){


Seriously? Your teacher gave you this code? main should always return int, not void. Tell them off for me, will you?

I've only taken a look at one random function, but it's got a couple of problems already:

View PostLadyRae, on 26 Sep, 2009 - 06:56 AM, said:

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;
}


What's the while condition for?
if humanScore < 0, then it will execute the else infinitely.
if humanScore >= 0, then it will do nothing and return.
Get rid of the while line, it's frightening me. Also, there are three conditional messages you have to print:
else {
	cout << "The computer won. How about a nice game of chess?" << endl;
}



void doesnt return anything because its VOID.

Void main is only exceptable in some instances, such as opengl since the code goes into a infinite loop before it can return anything
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6051
  • View blog
  • Posts: 23,483
  • Joined: 23-August 08

Re: A Game of Twenty-One

Posted 26 September 2009 - 08:08 AM

void main is never correct if you want to follow the C/C++ standards -- and you should.

See the FAQ and here for further explanation.
Was This Post Helpful? 0
  • +
  • -

#5 LadyRae  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 19-September 09

Re: A Game of Twenty-One

Posted 26 September 2009 - 03:08 PM

Well now I am even more confused. Yes this is the code that the teacher gave us to work with, honestly I don't really know how to do much with C++ yet, but I'm trying really hard to learn. This is part of the reason that I'm here, because the class doesn't really teach much, it just basically tells us what to do and then expects us to do it. Thanks to all of you that have helped so far. ~God bless!
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: A Game of Twenty-One

Posted 26 September 2009 - 03:25 PM

View PostJackOfAllTrades, on 26 Sep, 2009 - 07:08 AM, said:

void main is never correct if you want to follow the C/C++ standards -- and you should.

See the FAQ and here for further explanation.



True, i think void main should be avoid as much as possible, but I think most programmers these days reach outside the standards of C++. Look at the Boost libraries for example, almost everybody uses them but there not statndard.....yet
Was This Post Helpful? 1
  • +
  • -

#7 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

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

Re: A Game of Twenty-One

Posted 26 September 2009 - 03:47 PM

I think that the void main() is a common way to teach on schools my teacher said the same thing to me when i started, he still says USE VOID MAIN and i say I DONT WANT TO! almost ever lab class :(

OT: hit function if user asks for a card call the random function and give him another card then add this new card to the score and checkwinner again.
Was This Post Helpful? 1
  • +
  • -

#8 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: A Game of Twenty-One

Posted 27 September 2009 - 12:50 AM

Not wishing to stray too far off-topic, but bad habits are best stopped early!

View Postponcho4all, on 26 Sep, 2009 - 09:47 PM, said:

I think that the void main() is a common way to teach on schools my teacher said the same thing to me when i started, he still says USE VOID MAIN and i say I DONT WANT TO! almost ever lab class :(


Your teacher is wrong.

View PostImaSexy, on 26 Sep, 2009 - 09:25 PM, said:

True, i think void main should be avoid as much as possible, but I think most programmers these days reach outside the standards of C++. Look at the Boost libraries for example, almost everybody uses them but there not statndard.....yet


There is a difference between using code which is correct and using code which is not correct. You're confusing this issue with using a commonly-available library which is (presumably, I've never used it) written correctly or not using such a library. Does Boost use standard-compliant C++? If it didn't, it probably wouldn't work with different compilers.

This is not a major issue, but it's important to have an understanding of why something is the way it is.
Was This Post Helpful? 0
  • +
  • -

#9 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 257
  • Joined: 19-September 09

Re: A Game of Twenty-One

Posted 27 September 2009 - 01:26 AM

LadyRae looks like we are doing the same thing here is how I believe the last function should work: and yes I was told abbout the void main(){ as well give me a sec an I'll post what I have up for ya:)

code for hit I believe should go something like this:


char hit = y||n
// Begin while loop
while (char hit == y){
if (humanScore <= 21) {		 //player has not busted
cout << "Do you want to take a hit?";
}
if (char hit == y){					 //player wants hit
cout << humanScore + dealCard() // adds humanScore to dealCard()
else{										   //players does not want hit
cout << "You decide to stand";
if (humanScore > 21){					//player has busted
cout << "You have gone over 21, You Lose";
}   //end if
}  //end else
} // end while




I'll let you know if I get any more help do you have aim?
Was This Post Helpful? 1
  • +
  • -

#10 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 257
  • Joined: 19-September 09

Re: A Game of Twenty-One

Posted 27 September 2009 - 12:40 PM

your welcome:) Did you get it to compile yet?, If yes awesome, if no at 1130 p.m. I will be home and I can message you to make sure my code is compiling correctly before I assist you, let me know, cout;)
Was This Post Helpful? 0
  • +
  • -

#11 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: A Game of Twenty-One

Posted 27 September 2009 - 02:15 PM

View Postponcho4all, on 26 Sep, 2009 - 02:47 PM, said:

I think that the void main() is a common way to teach on schools my teacher said the same thing to me when i started, he still says USE VOID MAIN and i say I DONT WANT TO! almost ever lab class :(

OT: hit function if user asks for a card call the random function and give him another card then add this new card to the score and checkwinner again.


arrrrrrr :crazy: These are the kind of teacher i wanna slap. They have one single job and that is to teach standard c++ and they cant even get past declaring main without fucking up
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1