Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Asking bout a C++ questions. My code has something wrong . below is my code: #include #include #include using namespace std; int main(){ srand(99); int

Asking bout a C++ questions. My code has something wrong .

image text in transcribed

below is my code:

#include #include #include using namespace std;

int main(){

srand(99); int cgScore = 0; int humanTotalScore = 0; int computerTotalScore = 0; cout 20) { break; } cout

do { humanTotalScore = humanTurn(humanTotalScore) + humanTotalScore; cout = 100) cout = 100) cout

}

int dieRoll() { srand(99); return (rand() % 6) + 1;//in case we got 0. }

int humanTurn(int humanTotalScore) { char choices; int humanScore; cout > choices; if (choices == 'h') { humanTotalScore += humanScore; cout

return 0; }

int computerTurn(int computerTotalScore) { cout

return 0;

}

My output is below:

image text in transcribed

Why my random number is always 2????

I also tried putting the random formula after each score instead of using the dieRoll function. Like this:

srand(99);

humanScore = (rand() % 6) + 1;

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

srand(98);

int computerScore = (rand() % 6) + 1;

But the issue then becomes to every time the human has the same points which are 2 and computer has the same points which are 5 like below: image text in transcribed

Also, I found my total score is wrong as well.

For the computer, the previous 7 times it has : 2+4+4+6+1+2+5=24

And then computer gets two same points which is 5 for each time. so the total score would be: 24+5+5=34.

But the outcomes become 111 at the end..

I don't know which step I got wrong..

Really appreciated if you can help me out..

Thank you!

11. The game of Pig is a simple two player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn a player rolls a six-sided die: If the player rolls a 26 then he or she can either ROLL AGAIN or HOLD. At this point the sum of all rolls made this turn is added to the player's total score and it becomes the other player's turn. If the player rolls a 1 then the player loses his or her turn. The player gets no new points and it becomes the opponent's turn. If a player reaches 100 or more points after holding then the player wins. Write a program that plays the game of Pig, where one player is a human and the other is the computer. Allow the human to input I to roll again or h to hold. The computer program should play according to the following rule: keep rolling on the computer's turn until it has accumulated 20 or more points, then hold. Of course, if the computer wins or rolls a 1 then the turn ends immediately. Allow the human to roll first. Write your program using at least two functions: int humanTurn(int humanTotalScore); int computerTurn(int computerTotalScore); These functions should perform the necessary logic to handle a single turn for either the computer or the human. The input parameter is the total score for the human or computer. The functions should return the turn total to be added to the total score upon completion of the turn. For example, if the human rolls a 3 and 6 and then holds, then humanTurn should return 9. However, if the human rolls a 3 and 6 and then a 1, then the function should return 0. A Microsoft Visual Studio Debug Console Computer start roll: Computer chooses roll again... Computer chooses roll again... Computer chooses roll again... Computer chooses roll again... Computer chooses roll again... Computer chooses roll again... It's your turn! If you wanna roll again, press 'r'. If you wanna hold it as your point, press 'h'. You have 2 points You total score so far is: 2 It's computer's turn! Computer holds and has 2 points. Computer total score so far is: 50 It's your turn! If you wanna roll again, press 'r'. If you wanna hold it as your point, press 'h'. You have 4 points You total score so far is: 6 It's computer's turn! Computer holds and has 2 points. Computer total score so far is: 102 Computer win! CN Select Microsoft Visual Studio Debug Console Computer start roll: Computer chooses roll again... Computer chooses roll again... Computer chooses roll again... Computer chooses roll again... Computer chooses roll again... Computer chooses roll again... It's your turn! If you wanna roll again, press 'r'. If you wanna hold it as your point, press 'h'. h You have 2 points You total score so far is: 2 It's computer's turn! Computer holds and has 5 points. Computer total score so far is: 53 It's your turn! If you wanna roll again, press 'r'. If you wanna hold it as your point, press 'h'. If you wanna roll again, press 'r'. If you wanna hold it as your point, press 'h'. You have 4 points You total score so far is: 6 It's computer's turn! Computer holds and has 5 points. Computer total score so far is: 111 Computer win

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

Modern Database Management

Authors: Heikki Topi, Jeffrey A Hoffer, Ramesh Venkataraman

13th Edition

0134773659, 978-0134773650

More Books

Students also viewed these Databases questions