C++ FORM PLEASE: 5.12 PROGRAM 6: Pig Dice Game You are to finish the program bel
ID: 3840616 • Letter: C
Question
C++ FORM PLEASE: 5.12 PROGRAM 6: Pig Dice Game You are to finish the program below that implements the dice game PIG played by 2 human players. Pig is a dice game where each player during their turn rolls a 6-sided die. If the player rolls a 2 through 6, they decide whether they want to keep their turn score or roll again. If they roll a 1, the player loses all the points for that turn, thus receiving a turn score of 0 and that turn is over. The first player to reach 100 points or more wins. Sample output of program as it would look in cloud9:
Solve this program using the following incremental steps:
Copy this code into cloud9:
You can submit to zyBook after each step to get feedback.
(Step 1) Implement the printIntro function (This is Test 1)
(Step 2) Get the names of the 2 players (This is Test 2)
(Step 3) Implement the logic to switch who's turn it is in main function.
(Step 4) Implement humanTurn function to roll once (This is Test 3)
(Step 5) Implement humanTurn function to keep rolling until player answers n. (This is Test 4)
(Step 6) Implement humanTurn function to end when player rolls a 1. (This is Test 5)
(Step 7) Finish main function to output who won. (Last test will test entire program)
Sample cloud9 output showing winner:
zyBooks Library cs10 home 5.12: PROGRAM 6: Pig Dice Game Player 1 Enter your name Aragorn Player 2 Enter your name: Legolas Aragorn You rolled a 6 Your score: 6 Do you want to roll again? (y/n) y Aragorn You rolled a 2 Your score 8 Do you want to roll again? y/n) y Mragorn You rolled a 6 Your score 11 Do you want Lo roll again? (y/n) n Logo las You rollod a M Your score: A Do you want roll again? y/n) y Loyola You olled a b Your score 9 Do you want Lo roll again. y/n) n Aragorn You rollod a 6 Your score 20 Do you want to roll again? (y/n) y Aragorn You rolled a 1 (PIG Your turn is over Your score: 14 Legolas You rolled a 2 Your score 11 Do you want to roll again (y/n)Explanation / Answer
The following code will give you the required functions:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int PLAYER1 = 0;
const int PLAYER2 = 1;
const int WINNING_SCORE = 100;
//FIXME (1): Implement the printIntro function
void printIntro()
{
/*
.........
..Rules...
.........
*/
}
//FIXME (4, 5, 6): Implement the humanTurn function
int humanTurn(string playername,int playerscore)
{
char ch;
int score=0;
do
{
cout<<playername<<endl;
int x=rand()%6 + 1;
if(x==1)
{
cout<<"You rolled a 1 (PIG!)"
score=0;
cout<<"Your score: "<<playerscore<<endl;;
break;
}
else
{
cout<<"You rolled a "<<x<<endl
score+=x;
cout<<"Your score: "<<playerscore+score<<endl;
}
cout<<"Do you want to roll again?(y/n):";
cin>>ch;
}while(ch=='y');
return (playerscore+score);
}
int main() {
srand(4444);
// setup and initialize variables
int turn = PLAYER1;
int player1score = 0;
int player2score = 0;
string player1name;
string player2name;
printIntro();
// FIXME (2): get names of players
cout<<"PLAYER 1 - Enter your name: ";
cin>>player1name;
cout<<" PLAYER 2 - Enter your name: ";
cin>>player2name;
//play game
turn =0;
while (player1score < WINNING_SCORE && player2score < WINNING_SCORE) {
//player 1's turn or player 2's turn
if (turn == PLAYER1) {
player1score = humanTurn(player1name, player1score);
}
else {
player2score = humanTurn(player2name, player2score);
}
//FIXME (3): update turn value
// based on whose turn it is, update the turn variable to other player
turn = ~turn;
}
// FIXME (7): Output who won the game.
if(player1score>=WINNING_SCORE)
cout<<player1name<<" wins"<<endl;
else if(player2score>=WINNING_SCORE)
cout<<player2name<<" wins"<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.