this is what i have i really need help on updating the score

this is what i have. i really need help on updating the scores only. like if a person gets pig on their second turn their score would be what the have before.

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.

this is what i have. i really need help on updating the scores only. like if a person gets pig on their second turn their score would be what the have before.

You are to finish the program below that implements the dice game PIG played by 2 human players.

#include
#include
#include
#include
using namespace std;

const int PLAYER1 = 0;
const int PLAYER2 = 1;
const int WINNING_SCORE = 100;
// after long a while realized i needed this
int turn = PLAYER1;


//FIXME (1): Implement the printIntro function
void printIntro ()
{
cout << \"Welcome to the dice game Pig!\" << endl;
cout << \"The objective is to be first to score 100 points.\" << endl;
return;
  
}

//FIXME (4, 5, 6): Implement the humanTurn function
int humanTurn (string playername, int playerscore)
{
  

  
// have to print the player name (first playerfirst)
cout << playername << endl;

// have to roll the dice to see either if they rolled a pig or not
int dice = rand() % 6 + 1;


// now have to write an if statment whether or not they get 1 or anything else
if ( dice == 1)
{
cout << \"You rolled a 1 (PIG!)\" << endl;
cout << \"Your turn is over\" << endl;
  

}
else
{
playerscore += dice;
cout << \"You rolled a \" << dice << endl;

}

cout << \"Your score: \" << playerscore << endl;

if( dice == 1){
// if players it the first then the next player should be player2

if(turn == PLAYER1)

turn = PLAYER2;
// vise versa
else
turn = PLAYER1;
}
else{
cout << \"Do you want to roll again? (y/n): \";
char answer;
cin >> answer;
cout << endl;

if( answer != \'y\')
{
if( turn == PLAYER1)
turn = PLAYER2;
else
turn = PLAYER1;
}
}
// mind tried must keepgoing
return playerscore;
  
}

int main() {
srand(4444);

// setup and initialize variables

int player1score = 0;
int player2score = 0;
string player1name;
string player2name;

printIntro();
cout << endl;

// FIXME (2): get names of players
cout << \"Player 1 - Enter your name: \";
cin >> player1name;
cout << endl;

cout << \"Player 2 - Enter your name: \";
cin >> player2name;
cout << endl;

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

}

// FIXME (7): Output who won the game.
if(player1score >= WINNING_SCORE)
{
cout << player1name << endl;
cout << \"You rolled a 6\"<< endl;
cout << \"Your score: \"<< player1score << endl;
cout << endl;
cout << player1name << \" wins!\" << endl;
}
else
{
cout << player2name << endl;
cout << \"You rolled a 6\" << endl;
cout << \"Your score: \" << player2score << endl;
cout << endl;
cout << player2name << \" wins!\" << endl;
}

  
return 0;
}

Solution

#include<string>
#include<iostream>
#include<stdlib.h>
using namespace std;

const int PIG=-1;
const int PLAYER1 = 0;
const int PLAYER2 = 1;
const int WINNING_SCORE = 100;

//const PIG would indicate that a player just got 1

// after long a while realized i needed this
int turn = PLAYER1;

//FIXME (1): Implement the printIntro function
void printIntro ()
{
cout << \"Welcome to the dice game Pig!\" << endl;
cout << \"The objective is to be first to score 100 points.\" << endl;
return;
}
//FIXME (4, 5, 6): Implement the humanTurn function

//I have modified the function definition as per the requirement
int humanTurn (string playername)
{
  
int turnscore;
// have to print the player name (first playerfirst)
cout << playername << endl;
// have to roll the dice to see either if they rolled a pig or not
int dice = rand() % 6 + 1;

// now have to write an if statment whether or not they get 1 or anything else
if ( dice == 1)
{
cout << \"You rolled a 1 (PIG!)\" << endl;
cout << \"Your turn is over\" << endl;
// if players it the first then the next player should be player2

if(turn == PLAYER1)
turn = PLAYER2;
// vise versa
else
turn = PLAYER1;
return PIG;
}
else
{
cout << \"You rolled a \" << dice << endl;
cout << \"Do you want to roll again? (y/n): \";
char answer;
cin >> answer;
cout << endl;
if( answer != \'y\')
{
turnscore=dice;
if( turn == PLAYER1)
turn = PLAYER2;
else
turn = PLAYER1;
}
else{
int diceReturned=humanTurn (playername);

//A recursive call to humanTurn function is made if a player chooses to roll again

if(diceReturned==PIG){
return PIG;
}

else{
turnscore=diceReturned+dice;
}

//if player gets 1 on the second turn, then PIG is returned

//else, the dice is added to turn score

}
}
// mind tried must keepgoing

return turnscore;
  
}
int main() {
// setup and initialize variables
int scoreInitial=0;
int player1score = 0;
int player2score = 0;
string player1name;
string player2name;
printIntro();
cout << endl;
// FIXME (2): get names of players
cout << \"Player 1 - Enter your name: \";
cin >> player1name;
cout << endl;
cout << \"Player 2 - Enter your name: \";
cin >> player2name;
cout << endl;
//play game
while (player1score < WINNING_SCORE && player2score < WINNING_SCORE) {
//player 1\'s turn or player 2\'s turn
if (turn == PLAYER1) {
scoreInitial = humanTurn(player1name);
if(scoreInitial==PIG){
cout << \"Your score: \" << player1score << endl;
cout<<\"\ \";
}
else{
player1score+=scoreInitial;
cout << \"Your score: \" << player1score << endl;
cout<<\"\ \";
}
}
else {
  
scoreInitial = humanTurn(player2name);
if(scoreInitial==PIG){
cout << \"Your score: \" << player2score << endl;
cout<<\"\ \";
}
else{
player2score+=scoreInitial;
cout << \"Your score: \" << player2score << endl;
cout<<\"\ \";
}
}
//FIXME (3): update turn value
// based on whose turn it is, update the turn variable to other player
}
// FIXME (7): Output who won the game.
if(player1score >= WINNING_SCORE)
{
cout << player1name << endl;
cout << \"You rolled a 6\"<< endl;
cout << \"Your score: \"<< player1score << endl;
cout << endl;
cout << player1name << \" wins!\" << endl;
}
else
{
cout << player2name << endl;
cout << \"You rolled a 6\" << endl;
cout << \"Your score: \" << player2score << endl;
cout << endl;
cout << player2name << \" wins!\" << endl;
}
  
return 0;
}

SAMPLE IMPLEMENTATION THAT I TESTED:

Welcome to the dice game Pig!

The objective is to be first to score 100 points.

Player 1 - Enter your name: K1

Player 2 - Enter your name: K2

K1

You rolled a 2

Do you want to roll again? (y/n): y

K1

You rolled a 5

Do you want to roll again? (y/n): y

K1

You rolled a 4

Do you want to roll again? (y/n): n

Your score: 11

K2

You rolled a 2

Do you want to roll again? (y/n): y

K2

You rolled a 6

Do you want to roll again? (y/n): y

K2

You rolled a 2

Do you want to roll again? (y/n): y

K2

You rolled a 5

Do you want to roll again? (y/n): n

Your score: 15

K1

You rolled a 1 (PIG!)

Your turn is over

Your score: 11

K2

You rolled a 4

Do you want to roll again? (y/n): y

K2

You rolled a 2

Do you want to roll again? (y/n): y

K2

You rolled a 3

Do you want to roll again? (y/n): y

K2

You rolled a 2

Do you want to roll again? (y/n): y

K2

You rolled a 3

Do you want to roll again? (y/n): y

K2

You rolled a 2

Do you want to roll again? (y/n): n

Your score: 31

K1

You rolled a 6

Do you want to roll again? (y/n): y

K1

You rolled a 5

Do you want to roll again? (y/n): y

K1

You rolled a 1 (PIG!)

Your turn is over

Your score: 11

K2

You rolled a 1 (PIG!)

Your turn is over

Your score: 31

K1

You rolled a 5

Do you want to roll again? (y/n): y

K1

You rolled a 5

Do you want to roll again? (y/n): y

K1

You rolled a 6

Do you want to roll again? (y/n): y

K1

You rolled a 3

Do you want to roll again? (y/n): n

Your score: 30

K2

You rolled a 4

Do you want to roll again? (y/n): y

K2

You rolled a 4

Do you want to roll again? (y/n): y

K2

You rolled a 3

Do you want to roll again? (y/n): y

K2

You rolled a 3

Do you want to roll again? (y/n): y

K2

You rolled a 3

Do you want to roll again? (y/n): y

K2

You rolled a 2

Do you want to roll again? (y/n): n

Your score: 50

K1

You rolled a 2

Do you want to roll again? (y/n): y

K1

You rolled a 2

Do you want to roll again? (y/n): y

K1

You rolled a 6

Do you want to roll again? (y/n): y

K1

You rolled a 1 (PIG!)

Your turn is over

Your score: 30

K2

You rolled a 1 (PIG!)

Your turn is over

Your score: 50

K1

You rolled a 1 (PIG!)

Your turn is over

Your score: 30

K2

You rolled a 6

Do you want to roll again? (y/n): y

K2

You rolled a 4

Do you want to roll again? (y/n): y

K2

You rolled a 2

Do you want to roll again? (y/n): y

K2

You rolled a 5

Do you want to roll again? (y/n): n

Your score: 67

K1

You rolled a 2

Do you want to roll again? (y/n): y

K1

You rolled a 5

Do you want to roll again? (y/n): y

K1

You rolled a 4

Do you want to roll again? (y/n): y

K1

You rolled a 4

Do you want to roll again? (y/n): y

K1

You rolled a 4

Do you want to roll again? (y/n): n

Your score: 49

K2

You rolled a 6

Do you want to roll again? (y/n): y

K2

You rolled a 3

Do you want to roll again? (y/n): y

K2

You rolled a 2

Do you want to roll again? (y/n): y

K2

You rolled a 3

Do you want to roll again? (y/n): y

K2

You rolled a 3

Do you want to roll again? (y/n): n

Your score: 84

K1

You rolled a 6

Do you want to roll again? (y/n): y

K1

You rolled a 1 (PIG!)

Your turn is over

Your score: 49

K2

You rolled a 6

Do you want to roll again? (y/n): y

K2

You rolled a 5

Do you want to roll again? (y/n): n

Your score: 95

K1

You rolled a 2

Do you want to roll again? (y/n): n

Your score: 51

K2

You rolled a 3

Do you want to roll again? (y/n): y

K2

You rolled a 3

Do you want to roll again? (y/n): n

Your score: 101

K2

You rolled a 6

Your score: 101

K2 wins!

Please let me know in case of any doubts :)

this is what i have. i really need help on updating the scores only. like if a person gets pig on their second turn their score would be what the have before. Y
this is what i have. i really need help on updating the scores only. like if a person gets pig on their second turn their score would be what the have before. Y
this is what i have. i really need help on updating the scores only. like if a person gets pig on their second turn their score would be what the have before. Y
this is what i have. i really need help on updating the scores only. like if a person gets pig on their second turn their score would be what the have before. Y
this is what i have. i really need help on updating the scores only. like if a person gets pig on their second turn their score would be what the have before. Y
this is what i have. i really need help on updating the scores only. like if a person gets pig on their second turn their score would be what the have before. Y
this is what i have. i really need help on updating the scores only. like if a person gets pig on their second turn their score would be what the have before. Y
this is what i have. i really need help on updating the scores only. like if a person gets pig on their second turn their score would be what the have before. Y
this is what i have. i really need help on updating the scores only. like if a person gets pig on their second turn their score would be what the have before. Y
this is what i have. i really need help on updating the scores only. like if a person gets pig on their second turn their score would be what the have before. Y
this is what i have. i really need help on updating the scores only. like if a person gets pig on their second turn their score would be what the have before. Y

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site