Ace-High game::user-defined function problems

Started by
3 comments, last by TommySauder 18 years, 7 months ago
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(also how do I wrap my code?).

#include<iostream>
#include<cstdlib>
#include<time.h>
using namespace std;

int randomDraw(int card);
//returns random number from 2 to 11
bool isBust(int hand);
//returns true if the value of hand exceeds 21.
void playerWins(int handP);
//displays the fact player won.
void dealerWins(int handD);
//displays the fact dealer won
bool mustDraw(int dealer);
//returns true if the value of dealers hand is less than 17.
int drawoneCard(char[], int, bool);
//returns true if the value of dealers hand is less than 17.

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

//returns randomly generated card value from 2 to 11.
int randomDraw(int card)
{
	srand (time(NULL)); 
	
	card = (rand()%10+2);
	return(card);
}

//returns true if the value of hand exceeds 21.
bool isBust(int handP)
{
	if(handP>21)
		return true;
	else
		return false;
}

//displays the fact player won.
void playerWins(int handP)
{
	cout << "Player Wins " << endl;
}

//displays the fact dealer won
void dealerWins(int handD)
{ 
	cout << "Dealer Wins " << endl;
}

//returns true if the value of dealers hand is less than 17.
bool must_draw(int dealer)
{	
	if(dealer<17)
		return true;
	else
		return false;
}

//returns true if the value of dealers hand is less than 17.
//int drawoneCard(char[], int , bool)
//{
//	return;
//}
[Edited by - RoyalT on September 15, 2005 2:42:11 AM]
Advertisement
i suggest you read up on functions a little, to call them functions you need to pass values to some of them for example

bool isBust(int hand);

to call this in main you use:

isBust(hand);

where hand would be your stored integer hand value which you would have declared somewhere and set up.

have a look at this:
Function tutorial

hope this helps
si
I made the game without using anyy functions other than that rand() function you typed.

To practice functions, take the code there and put it into functions and call them when necessary.

(there are a lot of break; calls because without functions, i need to break 3 different loops to exit)

#include <iostream>#include <time.h>#include <cstdlib>#include <iomanip>using namespace std;int randomDraw();int main(){		cout << "Greetings here" << endl;	int option = 0;				int pCard = 0;	int dCard = 0;	int pHand = 0;	int dHand = 0;	// infinite loop so player can choose to play again	for (;;) {		for (;;) { // this additional one is so rest of the code below doesn't rerun	// option to play game or not	for (;;) {				cout << "are you ready to play - game name here - " << endl;		cout << setw(5) << "(1)Yes" << setw(8) << "(2)No" << endl;		cin >> option;		if (option > 2 || option < 1) {			cout << "Wrong option, please choose a correct option (1 or 2)" << endl;		}		else {			break;		}	}	if (option == 1)		cout << "Get ready to rumble!!!!!!" << endl;	else		return 1;	// game code starts here .. 	for (;;) {		pCard = randomDraw();		dCard = randomDraw();		pHand = randomDraw() + pCard;		dHand = randomDraw() + dCard;		int hitOrNot = 0;		for (;;) {						cout << "Your hand is: " << pHand << endl;			cout << "Would you like another card?" << endl;			cout << setw(5) << "(1)Yes" << setw(8) << "(2)No" << endl;			cin >> hitOrNot;			if (hitOrNot == 1)				pHand = pHand + randomDraw();						if (hitOrNot == 2)				break;			if (pHand > 21)				break;		}				int choice;		if (pHand > 21) {			cout << "Bust!, Would you like to play again?" << endl;			for (;;) {				cout << setw(5) << "(1)Yes" << setw(8) << "(2)No" << endl;				cin >> choice;				if (choice > 2 || choice < 1)					cout << "Incorrect choice please choose again" << endl;				else					break;			}			if (choice == 1) {				cout << "Lets play again!" << endl;				break;			} else {				cout << "Good Bye" << endl;				break;				break;				break;			}		}		for (;;){			if (dHand < 17)				dHand = dHand + randomDraw();			else				break;		}		if (dHand > 21) {			cout << "Dealer Busted! " << endl;			for (;;) { // just copied code from pHand > 21				cout << setw(5) << "(1)Yes" << setw(8) << "(2)No";				int choice;				cin >> choice;				if (choice > 2 || choice < 1)					cout << "Incorrect choice please choose again" << endl;				else					break;			}			if (choice == 1) {				cout << "Lets play again!" << endl;				break;			} else {				cout << "Good Bye" << endl;				break;				break;				break;			}		}		cout << "Dealers hand: " << dHand << endl;		cout << "Players hand: " << pHand << endl;		if (dHand >= pHand)			cout << "You lost!" << endl;		else			cout << "You won! " << endl;		int playAgain = 0;		for (;;) {			cout << "would you like to play again?" << endl;			cout << setw(5) << "(1)Yes" << setw(8) << "(2)No" << endl;			cin >> playAgain;			if (playAgain > 2 || playAgain < 1)				cout << "Invalid option, please choose again" << endl;			else				break;		}		if (playAgain == 1) {			break;		} else {			break;			break;			break;		}	}	}	}	return 0;}int randomDraw(){	srand (time(NULL)); 	int card;	card = (rand()%10+2);	return card;}


The game works, but can use a touch up with the help of functions...

To the more experienced programmers: I made the game as simple as I could so it would be easy for him to understand... rather than him trying to understand classes + inher.
thanks tommysauder, that helps a lot.
i will practice up on my own functions and using them in main, i tried to give a clean layout of what i was doing instead of all my practice runs with the program.
i really appreciate it, and thanks lonefox1 for the link.
also i might repost it when i get it to work with my functions to have someone review it.

No problem. When you're done putting it in functions I'll look it over for you, thats if none of the more experienced people do it before me!

This topic is closed to new replies.

Advertisement