PDA

View Full Version : Need help with functions



Overtime
10-18-2011, 12:31 AM
/////////////////////////////////////////////////////////////////////
//
// Name: <Put your name here>
// Date: <Today's date>
// Class: CSCI/CMPE 1170.03
// Semester: Fall 2011
// CSCI/CMPE 1370 Instructor: Christine Reilly
//
// This program simulates a two player game of Pig. Both players are
// computer simulated, but the user is able to choose the strategy
// for each player.
//
// This program makes use of functions. Because we have two players
// we are repeating certain actions in the game (asking for the
// players' strategy and performing the actions for a turn of the
// game for each player). Functions allow us to write the code one
// time (in the function), then call the function each time we want
// to use that block of code. We use parameters (values passed to
// the function) to give the function different values to work with.
//
/////////////////////////////////////////////////////////////////////


#include<iostream>
#include<cstdlib>
#include<string>
#include<time.h>

using namespace std;

int playerRoll(int strat, string name);
int getStrat(string name);


////////////////////////////////
// Note: It is best to not work on this program in the order that things are
// written in this file. Start by writing pseudocode, then write the function
// prototypes, followed by the functions, and finally write the main function.
////////////////////////////////


////////////////////////////////
// Put the function prototypes here
// You will write three functions: One to get the player's strategy from user
// input, another to simulate a turn of the game, and a third to print the
// game results.

int getUserStrat (string message);
int playersTurn ( int playerTurn1, int playerTurn2);


// End of function prototypes. Move on to write the block of code in the
// main function
////////////////////////////////


int main()
{
int total_player1 = 0; // Player 1 score tracker
int total_player2 = 0; // Player 2 score tracker
int player1_strat; // Player 1's strategy for each turn
int player2_strat; // Player 2's strategy for each turn

// seed the random number generator.
// Notice that we seed the random number generator in the main function
// (not in the player roll function), and we make sure not to put the
// random number generator inside a loop. This is because we only want
// to seed the random number generator ONCE per program run.
srand(static_cast<int> (time(NULL)));


////////////////////////////////
// Write code from here until the end comment. Use the comments as guidelines.


// Get the strategy for each player (use a function).
// You should write a single function and call it twice (once for
// each player).

player1_strat = getUserStrat (" Enter Player1's strategy: ");
player2_strat = getUserStrat (" Enter Player2's strategy: ");



// Use a loop to have the two players continue to take turns until one of
// has a total game score of 100 or greater. If the first player wins, make
// sure that you don't allow the second player to take a turn (quit the loop
// as soon as one player reaches 100 or greater).
//

total_player1 = playersTurn();
total_player2 = playersTurn();



// Use a function for the player's turn. You should write a single function
// and call it two times (once for each player's turn).



// End of your code block in the main function.
// Move on to write the three functions below the main function.
////////////////////////////////

// Print the results
// NOTE - you need to write the printResults function
/* if(total_player > total_player2)
{
printResults("Player 1", total_player, "Player 2", total_player2);
}
else if(total_player2 > total_player)
{
printResults("Player 2", total_player2, "Player 1", total_player);
}
else
{
// This should never happen because we quit the loop if Player 1
// wins and don't allow Player 2 to play their last turn.
// But we'll put this in just for completeness.
cout << "Draw! ";
cout << "(Note: if this happens there is something wrong with the code)";
cout << endl;
}*/

system("pause");
return 0;

} // end of main function



//////////////////////////
// Write the following three functions



//
// Function that gets the player's stategy from user input.
// This function should get the strategy for a single player and return
// the "roll until" value.
// This function checks that the user entered an integer, and
// keeps prompting the user to enter an integer until an integer is entered.
//
int getUserStrat (string message)
{
int strat;

cout << message << " ";
cin >> strat;

while ( !cin )
{
cin.clear();
cin.ignore (5000, '\n');

cout << " Enter a double please: ";
cin >> strat;
}
return strat;
}



//
// Function that simulates a single player's turn. This function should return
// the total of the rolls for the turn.
//

int playersTurn ( int playerTurn1, int playerTurn2)
{
int rollTotal;
int roll;
int totalTurns;
int player;
int strat;

while (rollTotal <= 100)
{
while (totalTurns <= strat)
{
roll = (rand() % 6) + 1;

if ( roll == 1 )
{
cout << " Computer rolled a 1, its turn is over. " << endl;
rollTotal = 0;
break;
}
else
{
cout << " Player 1's rolls for this turn are: " << roll << endl;
rollTotal += roll;
}
}
player += rollTotal;

cout << " Player 1 has completed its turn. ";
cout << " Player 1 received " << rollTotal << " points for the turn. ";
cout << " Current score for Player 1 is : " << rollTotal << endl << endl;

rollTotal = 0;

}
return rollTotal;
}






//
// Function that prints the results of the game. See the example program output
// to see how the end of game message should look.
// This function is already called by the main program so you can look there to
// see what return type and parameters it needs.
//



// End of your code for this program
//////////////////////////



help :/ I don't know if im doing this right.

If someone can explain and point me in the right direction would be greatly appreciated.

noidea
10-18-2011, 03:05 AM
Might be good to ask on the irc. irc.rizon.net #srl

i luffs yeww
10-18-2011, 05:52 AM
A little more information may be helpful.. What exactly needs checking/do you need help with?

Echo_
10-18-2011, 07:49 PM
First of all, use ctime instead of time.h for this:

srand(static_cast<unsigned int>(time(0)));

And like ily said, more info is needed to answer your question.