Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For your program we will create a two-player game of Pig. You have worked on the Pig game in Lab 12. Both of the Pig

For your program we will create a two-player game of Pig. You have worked on the Pig game in Lab

12. Both of the Pig players for this lab are computer simulated players. However, the user can choose

the strategy that each player uses. In Lab 12, during each turn the simulated player rolled until its

total for the turn was 20. Now we will let the user choose the value that each player will roll until for

each turn.

In the Lab 12, you played a single turn for a simulated player. Use the provided

program pigSingleGame.cpp to understand how to implement an entire game for a single player

CSCI/CMPE 1370 Fall 2017 Mr. Gustavo Dietrich

(take turns until its total score reaches 100). This program contains the code that you had to write

for Lab 12 to have the player take one turn by rolling until its turn total is 20 or greater. This

functionality is combined with an outer loop that ends the game when the game total is 100 or

greater. Add it to a project in Visual C++, analyze it and then run it to see how it works. You can use

this code to help you write the code for this lab (see below please).

In this lab you will play an entire game with two simulated players. The game begins by asking the

user to enter the "roll until" value for each player (the players may have different values). Then the

program will play an entire game using the two simulated players. The game ends when one of the

player's total score is 100 or greater, and prints a message stating which player won.

One way to have two players play the game would be to copy and paste the code for one player's

turn to create a second player's turn. But this means that you will be repeating code, a very bad

design practice. Instead, you will write two functions for this program: one that gets the strategy for a

player (their "roll until" value), and one that simulates a player's turn. Inside the whole game loop,

you can call these functions for each of the players.

Our computer players are a bit stupid. A real player would end their turn as soon as their game total

was 100 or greater. Our computer player will continue its final turn until it reaches its specified turn

total, or until it rolls a one. We're letting our computer players be a bit stupid for this game in order

to simplify the logic needed to run the game.

Use the provided program PigFunction.cpp to get you started. Add it to a project in Visual C++ and

complete the program. I have marked each place where you need to add code with a long line of

slashes (//////////////). Use incremental development steps like I described for the example program.

I have already told you that you need three functions: one to get the strategy for a player, one to

simulate a player's turn, and one to print the formatted game results. Your game should play like the

game in the following figures.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/////////////////////////////////////////////////////////////////////

//

// Name:

// Date:

// Class:

// Semester:

// CSCI/CMPE 1370 Instructor:

//

// 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Theory Icdt 99 7th International Conference Jerusalem Israel January 10 12 1999 Proceedings Lncs 1540

Authors: Catriel Beeri ,Peter Buneman

1st Edition

3540654526, 978-3540654520

More Books

Students also viewed these Databases questions

Question

What must a creditor do to become a secured party?

Answered: 1 week ago

Question

3. Define the roles individuals play in a group

Answered: 1 week ago