BELOW IS THE CODE I TRIED. PLEASE FIX IT CORRECTLY. #include <iostream> #include
ID: 3840188 • Letter: B
Question
BELOW IS THE CODE I TRIED. PLEASE FIX IT CORRECTLY.
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
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 << "Welcome to the dice game Pig!"<<endl;
cout<<"The objective is to be first to score 100 points." << endl;
}
//FIXME (4, 5, 6): Implement the humanTurn function
int humanTurn(string playerName, int playerScore)
{
//print player name
cout << endl << playerName;
//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 << endl << "You rolled a 1 (PIG!)";
cout << endl << "Your turn is over";
if(prevRoll!=1)
playerScore = playerScore-prevRoll;
cout << endl << "Your score: " << playerScore << endl;
}
//if the turn socre is not 1, add the turn score to player socre
else
{
cout << endl << "You rolled a " << num;
playerScore += num;
cout << endl << "Your score: " << playerScore<<endl;
}
//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 << "Player 1 - Enter your name: " << endl;
cin >> player1name;
cout << "Player 2 - Enter your name: ";
cin >> player2name;
//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): 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 < WINNING_SCORE)
{
cout << "Do you want to roll again?(y/n): ";
cin >> newTurn;
cout << endl;
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 < WINNING_SCORE)
{
cout << "Do you want to roll again?(y/n): ";
cin >> newTurn;
cout << endl;
if (newTurn == 'y')
turn = PLAYER2;
else
{
turn = PLAYER1;
prevRoll = 0;
}
}
}
}
// FIXME (7): Output who won the game.
if (player1score >= WINNING_SCORE)
cout << "Congrats "<<player1name<<endl<<"You won the game!"<<endl;
else if(player2score >= WINNING_SCORE)
cout << "Congrats " << player2name << endl << "You won the game!" << endl;
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/n) Charles You rolled a 6 Your score 90 Do you want to roll again? (y/n) Charles You rolled a 3 Your score: 93 Do you want to roll again? (y/n) Charles Your output ends You rolled a 4 with Your score: 97 Do you want to roll again? (y/n) CharlesExplanation / Answer
// The only thing that is wrong with the program is using of srand(). If you pass a constant seed. it will give you the same output everytime. You need to make it random. so use srand(time(0)). And also I optimized at few places
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
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 << "Welcome to the dice game Pig!"<<endl;
cout<<"The objective is to be first to score 100 points." << endl;
}
//FIXME (4, 5, 6): Implement the humanTurn function
int humanTurn(string playerName, int playerScore)
{
//print player name
cout << endl << playerName;
//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 << endl << "You rolled a 1 (PIG!)";
cout << endl << "Your turn is over";
if(prevRoll!=1)
playerScore = playerScore-prevRoll;
cout << endl << "Your score: " << playerScore << endl;
}
//if the turn socre is not 1, add the turn score to player socre
else
{
cout << endl << "You rolled a " << num;
playerScore += num;
cout << endl << "Your score: " << playerScore<<endl;
}
//save trun score as previous turn score for
//future purpose
prevRoll = num;
//return player updated score
return playerScore;
}
int main()
{
srand(time(0));
// 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 << "Player 1 - Enter your name : ";
cin >> player1name;
cout << "Player 2 - Enter your name : ";
cin >> player2name;
//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): 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 < WINNING_SCORE)
{
cout << "Do you want to roll again?(y/n): ";
cin >> newTurn;
cout << endl;
if (newTurn == 'n')
{
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 < WINNING_SCORE)
{
cout << "Do you want to roll again?(y/n): ";
cin >> newTurn;
cout << endl;
if (newTurn == 'n')
{
turn = PLAYER1;
prevRoll = 0;
}
}
}
}
// FIXME (7): Output who won the game.
if (player1score >= WINNING_SCORE)
cout << "Congrats "<<player1name<<endl<<"You won the game!"<<endl;
else if(player2score >= WINNING_SCORE)
cout << "Congrats " << player2name << endl << "You won the game!" << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.