Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 09-15-2005, 02:27 AM   #1
Member (7 bit)
 
Join Date: Jun 2004
Location: central california
Posts: 113
Send a message via AIM to RoyalT
HELP::acehigh game problem in cpp

hello all, i am working on a really simple game that doesn't require any input from the user, except to run or end the program, and i am completely stuck. i could really use some help here.
i have to use user-defined functions and I can't figure out how to call each of them in main().
the program needs to display two cards in numerical form from 2 to 11 for the player and the dealer, then total the two cards. If the player is over 21 he loses, and same for the dealer; but if the dealer is under 17 he must draw again.
the code is below, let me know if i need to give more info
Code:
#include
#include
#include
using namespace std;

int randomDraw(int card);

bool isBust(int hand);

void playerWins(int handP);

void dealerWins(int handD);

bool mustDraw(int dealer);

int drawoneCard(char[], int, bool);

int main()
{
	cout << "WELCOME TO MY ACE-HIGH GAME " << endl;

	//randomDraw(int card);

	//bool isBust(int hand);
	
	//void playerWins(int handP);

	//void dealerWins(int handD);

	//bool mustDraw(int dealer);

	//int drawoneCard(char[], int, bool);
	
	return 0;
}

int randomDraw(int card)
{
	srand (time(NULL)); 
	
	card = (rand()%10+2);
	return(card);
}

bool isBust(int handP)
{
	if(handP>21)
		return true;
	else
		return false;
}

void playerWins(int handP)
{
	cout << "Player Wins " << endl;
}

void dealerWins(int handD)
{ 
	cout << "Dealer Wins " << endl;
}

bool must_draw(int dealer)
{	
	if(dealer<17)
		return true;
	else
		return false;
}

//int drawoneCard(char[], int , bool)
//{
//	return;
//}
RoyalT is offline   Reply With Quote
Old 09-15-2005, 07:42 AM   #2
Member (7 bit)
 
shaticus's Avatar
 
Join Date: May 2004
Posts: 103
You probably want to uncomment these:
//randomDraw(int card);

//bool isBust(int hand);

//void playerWins(int handP);

//void dealerWins(int handD);

//bool mustDraw(int dealer);

//int drawoneCard(char[], int, bool);
Otherwise the compiler will ignore these line making your program print the welcome message and end. You need to declare variables for the hand. In example randomCard(int card) will generate an error if the compiler does not know what the variable card is. You have to tell it that it is a variable by declaring like the following: int card;
When you give a function a return type(int randomCard(int card) you usually have to do something with the data returned(every compiler is different but the compiler will usually warn you about not using the value.)
Also you do not need to include the return type within main. In example, void dealerWins(int handD); should be dealerWins(int handD); the program will know everything about the function to run it.

I think the following should work for drawing one card for the player and one card for the dealer. Since the dealer will always have less than 17 drawOneCard will always run.(because he only has one card and no card is greater than 17) I will leave drawing the second card up to you. It should be rather simple and involve modifying the randomDraw() function.

HTH,
Shaticus

int randomDraw();

bool isBust(int hand);

void playerWins(int handP);

void dealerWins(int handD);

bool mustDraw(int dealer);

int drawoneCard(int handIncomming);

int main()
{
int hand;
int dealerHand;

cout << "WELCOME TO MY ACE-HIGH GAME " << endl;

hand = randomDraw();
dealerhand = randomDraw();

if(isBust( hand) == TRUE)
dealerWins(dealerhand);

if(mustDraw( dealerHand) == TRUE)
drawoneCard(char[], int, bool);

if(isBust(dealerhand) == TRUE)
playerWins(hand);

return 0;
}

int randomDraw(int card)
{
srand (time(NULL));

card = (rand()%10+2);
return(card);
}

bool isBust(int handP)
{
if(handP>21)
return true;
else
return false;
}

void playerWins(int handP)
{
cout << "Player Wins " << endl;
}

void dealerWins(int handD)
{
cout << "Dealer Wins " << endl;
}

bool must_draw(int dealer)
{
if(dealer<17)
return true;
else
return false;
}

int drawoneCard(int handIncomming)
{
srand (time(NULL));

return (handIncomming + (rand()%10+2));
}
shaticus is offline   Reply With Quote
Old 09-15-2005, 06:14 PM   #3
Member (7 bit)
 
Join Date: Jun 2004
Location: central california
Posts: 113
Send a message via AIM to RoyalT
thanks shaticus, i understand more about calling functions now.
i kind of just did a layout of the program instead of posting my practice runs, but I am using your posted code now to finish which helps me a lot.
only thing is, i am still having trouble compiling and i don't know if it is my compiler or if we left out something?
RoyalT is offline   Reply With Quote
Old 09-15-2005, 08:53 PM   #4
Member (7 bit)
 
shaticus's Avatar
 
Join Date: May 2004
Posts: 103
sorry about that, i finally got a few minutes to sit down and look over the code again. This compiled fine on my machine. I modified the random draw function, removing the variable it took as an argument. I wasnt sure how you wanted to use this function and this seemed better than leaving "magic numbers" in the code. There is still a little bit of work to be done such as outputting the cards the player and computer have.

Let me know if you need any help or have any questions.

Shaticus

Edit: I include cstdlib and iostream. The forum seems to cut that off the post.

#include
#include

int randomDraw();

bool isBust(int hand);

void playerWins(int handP);

void dealerWins(int handD);

bool must_draw(int dealer);

int drawoneCard(int handIncomming);
using namespace std;
int main()
{
int hand;
int dealerHand;

cout << "WELCOME TO MY ACE-HIGH GAME " << endl;

hand = randomDraw();
dealerHand = randomDraw();

if(isBust(hand) == true)
dealerWins(dealerHand);

if(must_draw(dealerHand) == true)
drawoneCard(hand);

if(isBust(dealerHand) == true)
playerWins(hand);

return 0;
}

int randomDraw()
{
srand (time(NULL));

return ((rand()%10+2));

}

bool isBust(int handP)
{
if(handP>21)
return true;
else
return false;
}

void playerWins(int handP)
{
cout << "Player Wins " << endl;
}

void dealerWins(int handD)
{
cout << "Dealer Wins " << endl;
}

bool must_draw(int dealer)
{
if(dealer<17)
return true;
else
return false;
}

int drawoneCard(int handIncomming)
{
srand (time(NULL));

return (handIncomming + (rand()%10+2));
}
shaticus is offline   Reply With Quote
Old 09-15-2005, 11:04 PM   #5
Member (7 bit)
 
Join Date: Jun 2004
Location: central california
Posts: 113
Send a message via AIM to RoyalT
alright it compiled fine for me. thanks for getting me this far, i forgot to put in comments so i'm glad you figured out what i was trying to do. i'm still working on it, but when i get some outputs to the screen to work i will post my code.
regards,
Trace
RoyalT is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 04:57 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2