Question
This is what I got. BELOW IS THE CODE I TRIED. PLEASE FIX IT CORRECTLY. #include #include #include #include using namespace std; const int PLAYER1
This is what I got.
BELOW IS THE CODE I TRIED. PLEASE FIX IT CORRECTLY.
#include
#include
#include
#include
using namespace std;
const int PLAYER1 = 0;
const int PLAYER2 = 1;
const int WINNING_SCORE = 100;
int prevRoll;
//function declarations
void printIntro();
int humanTurn(string,int);
//FIXME (1): Implement the printIntro function
void printIntro()
{
//print welcome messages
cout
cout
}
//FIXME (4, 5, 6): Implement the humanTurn function
int humanTurn(string playerName, int playerScore)
{
//print player name
cout
//roll die
int num = rand() % 6 + 1;
//if the rolled number is 1, over the turn and substract the previous turn score
//from the player score
if (num == 1)
{
cout
cout
if(prevRoll!=1)
playerScore = playerScore-prevRoll;
cout
}
//if the turn socre is not 1, add the turn score to player socre
else
{
cout
playerScore += num;
cout
}
//save trun score as previous turn score for
//future purpose
prevRoll = num;
//return player updated score
return playerScore;
}
int main()
{
srand(4444);
// setup and initialize variables
int turn = PLAYER1;
int player1score = 0;
int player2score = 0;
string player1name;
string player2name;
char newTurn;
printIntro();
// FIXME (2): get names of players
cout
cin >> player1name;
cout
cin >> player2name;
//play game
while (player1score
{
//player 1's turn or player 2's turn
if (turn == PLAYER1)
{
player1score = humanTurn(player1name, player1score);
}
else
{
player2score = humanTurn(player2name, player2score);
}
//FIXME (3): switch whose turn it is
if (turn == PLAYER1)
{
//if prevRoll is 1, that is the player1 loses turn.
//give turn to player 2
if (prevRoll ==1)
{
turn = PLAYER2;
prevRoll = 0;
}
//if player1 rolls 2 through 6 and player1 yet to reach Winnning socre
//ask player 1 to decide whether to keep turn score or to roll again
else if (player1score
{
cout
cin >> newTurn;
cout
if (newTurn == 'y')
turn = PLAYER1;
else
{
turn = PLAYER2;
prevRoll = 0;
}
}
}
else if (turn == PLAYER2)
{
//if prevRoll is 1, that is the player2 loses turn.
//give turn to player 1
if (prevRoll == 1)
{
turn = PLAYER1;
prevRoll = 0;
}
//if player2 rolls 2 through 6 and player2 yet to reach Winnning socre
//ask player 2 to decide whether to keep turn score or to roll again
else if (player2score
{
cout
cin >> newTurn;
cout
if (newTurn == 'y')
turn = PLAYER2;
else
{
turn = PLAYER1;
prevRoll = 0;
}
}
}
}
// FIXME (7): Output who won the game.
if (player1score >= WINNING_SCORE)
cout
else if(player2score >= WINNING_SCORE)
cout
return 0;
}
6: Compare output Charles Ada Y y Y y Y n Input Y n y y y y y Y Y Y Y Y y Y y y y you want to roll again? (y) Charles You rolled a 6 Your score 90 Do you want to roll again? (y) Charles You rolled a 3 Your score: 93 Do you want to roll again? (y) Charles Your output ends You rolled a 4 with Your score: 97 Do you want to roll again? (y) CharlesStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started