Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,545 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,237 people online right now. Registration is fast and FREE... Join Now!




Simple C++ code for a game of blackjack

 
Reply to this topicStart new topic

Simple C++ code for a game of blackjack, Need help writing the code for the dealCards, hit, and determineWinner

Rating  5
izm94303
post 1 Aug, 2008 - 05:13 PM
Post #1


New D.I.C Head

*
Joined: 1 Aug, 2008
Posts: 16


My Contributions


I need help providing the code for the dealCards, hit, and determineWinner functions ASAP. I've coded most of the program, but I'm really having a hard time writing the function codes for the listed items; Please assist this paper is due Sunday 8-3-08.

cpp
#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);
}

Mod edit: Please code.gif
Thanks, gabehabe smile.gif
User is offlineProfile CardPM

Go to the top of the page


jwwicks
post 2 Aug, 2008 - 10:06 PM
Post #2


D.I.C Head

Group Icon
Joined: 31 Jul, 2008
Posts: 59



Thanked 6 times

Dream Kudos: 200
My Contributions


Hello izm,

QUOTE(izm94303 @ 1 Aug, 2008 - 05:13 PM) *
I need help providing the code for the dealCards, hit, and determineWinner functions ASAP. I've coded most of the program, but I'm really having a hard time writing the function codes for the listed items; Please assist this paper is due Sunday 8-3-08.


Here's dealCards to get you started....

cpp

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--){
// Values from 1 to K
val = Random(0,14);
if( val > 10 ) val = 10;
if( val == 1 ) val = 11;
cout << val;
if(cards)
cout << ",";
ret_val+=val;
}
return ret_val;
}


One caveat is that 1, an Ace, can be considered either 1 or 11. This function just assigns 11 by default. Didn't see anyway to easily differentiate the value after the function call...

JW

This post has been edited by jwwicks: 2 Aug, 2008 - 10:08 PM
User is offlineProfile CardPM

Go to the top of the page

izm94303
post 3 Aug, 2008 - 07:15 AM
Post #3


New D.I.C Head

*
Joined: 1 Aug, 2008
Posts: 16


My Contributions


QUOTE(jwwicks @ 2 Aug, 2008 - 11:06 PM) *

Hello izm,

QUOTE(izm94303 @ 1 Aug, 2008 - 05:13 PM) *
I need help providing the code for the dealCards, hit, and determineWinner functions ASAP. I've coded most of the program, but I'm really having a hard time writing the function codes for the listed items; Please assist this paper is due Sunday 8-3-08.


Here's dealCards to get you started....

cpp

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--){
// Values from 1 to K
val = Random(0,14);
if( val > 10 ) val = 10;
if( val == 1 ) val = 11;
cout << val;
if(cards)
cout << ",";
ret_val+=val;
}
return ret_val;
}


One caveat is that 1, an Ace, can be considered either 1 or 11. This function just assigns 11 by default. Didn't see anyway to easily differentiate the value after the function call...

JW



Thanks a million JW
User is offlineProfile CardPM

Go to the top of the page

izm94303
post 3 Aug, 2008 - 07:36 AM
Post #4


New D.I.C Head

*
Joined: 1 Aug, 2008
Posts: 16


My Contributions


cpp
//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

}

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


This post has been edited by gabehabe: 3 Aug, 2008 - 09:57 AM
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 3 Aug, 2008 - 09:55 AM
Post #5


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,856



Thanked 46 times

Dream Kudos: 550
My Contributions


Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: code.gif

Thanks.

First you double post. Then you still have not learned to use code tags. and you have shown NO effort to solve the problem. Please ensure that you read the forum rules.
User is offlineProfile CardPM

Go to the top of the page

izm94303
post 3 Aug, 2008 - 11:14 AM
Post #6


New D.I.C Head

*
Joined: 1 Aug, 2008
Posts: 16


My Contributions


QUOTE(NickDMax @ 3 Aug, 2008 - 10:55 AM) *

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: code.gif

Thanks.

First you double post. Then you still have not learned to use code tags. and you have shown NO effort to solve the problem. Please ensure that you read the forum rules.


My apologies. I'm still trying to figure my way around this site. I haven't used a forum before so this is new to me. The double post was my mistake. I was trying to reply to the guy that was nice enough to help. Which by the way, I've been working on since 4 A.M. I'll update the problem when I figure out the remaining items.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 02:07AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month