Question: C++ help finishing my program having a hard time understanding user defined functions :(. I already have the pseudocode. // This program simulates a two
C++ help finishing my program having a hard time understanding user defined functions :(. I already have the pseudocode.
// 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
#include
#include
#include
using namespace std;
////////////////////////////////
// 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 getStrat(string name);
int playerRoll(int strat, string name);
// 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 (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).
// 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).
//
// 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 getStrat() that gets the player's strategy from user input.
// This function should receive the name of the player, 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.
// Run the sample program provided to see how this function ensures an integer
// is entered.
//
//
// Function playerRoll() that simulates a single player's turn. This function should return
// the total of the rolls for the turn.
//
//
// Function printResults() 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
//////////////////////////
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
