How can i create TWo Functions?

I WAnT to create two fucntions

Page 1 of 1

2 Replies - 677 Views - Last Post: 20 March 2008 - 12:57 PM Rate Topic: -----

#1 dinisrainman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 10-February 08

How can i create TWo Functions?

Post icon  Posted 20 March 2008 - 10:53 AM

I am developing a PIG game. The purpose of the game is to create an interactive game between the user and Computer. After 5 rolls the human stops and the computer outputs its 5 rolls in one turn.
My probelm is im having problems in passing the game to the Computer when the user's turn is complete. How can i do this back em forth issue. So far, i have these two loops i want create functions instead. Any tip, hint is much appreciated.

	
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
			
			//initializes time
			srand(time (NULL) );

			int humanScore = 0;
			int compScore = 0;
			char option;
			int dice;
			int count = 0;
			
			//compDie = randomNumber2;
			cout << "Welcome to Pig!" << endl;
			cout << "Now is your turn.";
			bool play = true;
				while (play)
				{
				cout << "Do you want roll or hold (r/h)?:";
				cin >> option;
				cin.get();
							
				if ( option == 'r')
				{
					
					dice = rand() % 6 + 1;
				}
				else if(option == 'h')
				{
					play = false;
					cout << " Your Score is " << humanScore << endl; 
					
				}
				else 
				{
					cout << "you pressed a wrong key, press r or h" << endl;
				}
					while ( humanScore >=100 || count < 5)
					{
						if ( dice == 1 )
						{
							cout << "The dice rolled is 1 and your current score is 0 . Your Score is" << humanScore << endl;
							cout << "Now it's Computer's turn" << endl;
							//count  = 5;
						}	humanScore += dice;
						cout << " The Dice rolled is " << dice << " and your current turn score is " << humanScore << endl;
						count++;
					}	
					 while(compScore >=100)
					 {
						if(dice == 1 )
						{
							cout << "The dice rolled is 1 and your current score is 0 ";
							cout << "It's the human's turn";
						}

					}	compScore += dice;
				}

			system ("pause");
			return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: How can i create TWo Functions?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,085
  • Joined: 18-April 07

Re: How can i create TWo Functions?

Posted 20 March 2008 - 12:08 PM

Interesting little program here. The only way to show you the pass back and forth was pretty much to do the game in its entirety. Below is one way you could do this by breaking the player's turn logic and computer's turn logic into their function and create a main game loop in main(). We call the functions back and forth until one of the scores reach 100+.

Check this out and use what you want of it. May need some work on the formatting of output to be nice and neat though. I will leave that up to you.

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

// Declarations for our two functions. Takes in the current score
// returns a turn score.

int PlayersTurn(int);
int ComputersTurn(int);

int main()
{
	//initializes time
	srand(time (NULL) );

	// Setup our scores
	int humanScore = 0;
	int compScore = 0;

	// Intro
	cout << "Welcome to Pig!" << endl;
	cout << "Now is your turn." << endl << endl;
	bool play = true;

	// Main game loop
	while (play)
	{
		// Call player's turn function and pass it the current score (to determine if there is a win condition)
		humanScore += PlayersTurn(humanScore);

		// Check if they won
		if (humanScore >= 100) { 
			cout << "Player 1 wins with a score of " << humanScore << endl; 
			play = false;
		}
		else {
			// Human didn't win, but show their current score
			cout << "Human score is: " << humanScore << endl << endl;

			// Computers turn, call its function and give it the current computer score
			compScore += ComputersTurn(compScore);

			// Did it win?
			if (compScore >= 100) {
				cout << "Computer wins with a score of " << compScore << endl;
				play = false;
			}
			else {
				// No it didn't win, so show its score and go back to top of game loop.
				cout << "Computer's score is: " << compScore << endl << endl;
			}
		}
	}

	system ("pause");
	return 0;
}

// Controls the player's interaction (rolling and holding) and returns the turn's score
// which is then added to the player's score back in the main game loop.
int PlayersTurn(int humanScore) {
	int dice = 0;
	int turnScore = 0;
	int count = 0;
	char option;

	while ((humanScore + turnScore) < 100 && count < 5)
	{
		cout << "Do you want roll or hold (r/h)?: ";
		cin >> option;
		cin.get();

		if ( option == 'r')
		{
			dice = rand() % 6 + 1;
		}
		else 
		{
			cout << "Your Score this turn is " << turnScore << endl;
			return turnScore;
		}


		if ( dice == 1 )
		{
			cout << "The dice rolled is 1 and your current score is now 0 for this turn." << endl;
			cout << "Now it's the Computer's turn" << endl << endl;
			return 0;
		}    
		turnScore += dice;
		cout << "The Dice rolled is " << dice << " and your current turn score is " << turnScore << endl;
		count++;
	}  


	return turnScore;

}

// Computers turn function. Rolls 5 times until it hits 1 or reaches 5 rolls.
// It then returns the turn score back to the main game loop where the win condition is checked.

int ComputersTurn(int compScore) {
	int dice = 0;
	int turnScore = 0;

	for (int i = 0; i < 5; i++) {
		dice = rand() % 6 + 1;

		if (dice == 1) { 
			cout << "The dice rolled is 1 and the computer's current score is now 0 for this turn." << endl;
			cout << "It's the human's turn" << endl << endl;
			return 0;
		}
		else {
			turnScore += dice;

			// If their score and the turn are 100 or greater, no use rolling anymore. The computer knows it wins.
			if ((compScore + turnScore) >= 100) {
				return turnScore;
			}

			cout << "Computer's current turn score is: " << turnScore << endl;
		}
	}

	return turnScore;
}



I have put in-code comments to show you the logic there. Essentially we enter the main game loop, call the player's turn function which loops through five rolls or until they hold. It turns that turn score. If at that time they hit a 1, it will automatically return 0. Whatever the turn's outcome at the end, is added to the player's score and checked to see if they have won.

The computer loops through five rolls until it either hits 1 and returns 0 or makes it through five rolls and its turn score is then added to its current score.

At any time during either player's roll if the value of the turnScore + their current score is greater than 100, it automatically returns the turn score because a normal person would have just held then to win the game.

Oh and btw... I made it so that if they enter anything other than "r" it will be considered a hold.

I hope this makes sense. Enjoy!

"At DIC we be PIG..... ummm I mean code ninjas!" :snap:

This post has been edited by Martyr2: 20 March 2008 - 12:11 PM

Was This Post Helpful? 0
  • +
  • -

#3 dinisrainman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 10-February 08

Re: How can i create TWo Functions?

Posted 20 March 2008 - 12:57 PM

Thanks for the tip Martyr 2. Im a beginner so, im learning it's just the first time as go through the book i often get stuck. But i was thinking in building this function rolldice() that will have two parameters, this fuction will calculate the the score of both human and computer . Not finished yet but, take a look. i'll come back when im done: this an other idea i was trying


#include <iostream>
#include <ctime>

using namespace std;

int rollDice ( int, int );
int score ( int, int );
int resetScore ( int, int);

int main ()
{
	//genarate numbers
	srand (( NULL));

	int dice;
	int humanScore = 0;
	int compScore = 0;
	char option;

	// const variable
	const int WINNING = 100;
	const int COMP_HOLD = 20;
	const int SIDE = 6;

	cout << " Welcome to Pig!" << endl;
	cout << "Now is your turn" << endl;
	
	bool play = false;
	while (play)
	{
		if (option == 'h' )
		{
			play = false;
		}
		else if (option == 'r' )
		{
			rollDice(humanScore, compScore);
		}
		else 
			cout << "You entered the wrong key" << endl;
	
		do
		{
			cout << "Do you want to roll or hold (r/h)?";
			cin >> option;
			cin.get();
			
		}while ( rollDice != WINNING);// not sure if i can have a fuction within a loop


	}
		system ("pause");
		return 0;
}

int rollDice ( int humanScore, int compScore )
{
	int dice;
	dice = rand()% 6 + 1;
	if (humanScore >= WINNING)
	{
		cout << " Your dice rolled is " << dice << "and your current Score is" << humanScore;
	}
	else if (compScore >=WINNING)

}




Was This Post Helpful? 0
  • +
  • -

Page 1 of 1