Solve C++ Pig Dice Game: Aragorn Wins!

In summary: For example, if player 2 has a total score of 96 and player 1 has a total score of 102, then player 1 has won and doesn't want to roll again. In summary, the code does not work and the program ends up continuously running in the while loop when I want the output to be different for when diceRoll ==1. How can I fix this? Also how would I get rid of the statement "Do you want to roll again" at the end right before it outputs who the winner is?
  • #1
student2019
1
0
Hello!
So for an assignment, we have to write a program for the 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.

This is the code I have so far. However, it does not work and the program ends up continuously running in the while loop when I want the output to be different for when diceRoll ==1. How can I fix this? Also how would I get rid of the statement "Do you want to roll again" at the end right before it outputs who the winner is?
Thank you!

Code:
code removed

This is a sample output of what the program is suppose to come out as:
Welcome to the dice game Pig!
The objective is to be first to score 100 points.

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

Aragorn
You rolled a 6
Your score: 14
Do you want to roll again? (y/n): n

Legolas
You rolled a 4
Your score: 4
Do you want to roll again? (y/n): y

Legolas
You rolled a 5
Your score: 9
Do you want to roll again? (y/n): n

Aragorn
You rolled a 6
Your score: 20
Do you want to roll again? (y/n): y
*This part and on is what I cannot get for the code I had written
Aragorn
You rolled a 1 (PIG!)
Your turn is over
Your score: 14
Do you want to roll again? (y/n):y

Aragorn
You rolled a 5
Your score: 96
Do you want to roll again? (y/n): y

Aragorn
You rolled a 6
Your score: 102

Aragorn wins!
 
Last edited:
Technology news on Phys.org
  • #2
student2019 said:
This is the code I have so far. However, it does not work and the program ends up continuously running in the while loop when I want the output to be different for when diceRoll ==1. How can I fix this? Also how would I get rid of the statement "Do you want to roll again" at the end right before it outputs who the winner is?
Thank you!

Hi student2019! ;)

I recommend splitting your code up into smaller functions.
That makes it easier to distinguish the situation where we've won and don't want to ask to roll again.

As for your while loop that doesn't end when you want it to, perhaps we should distinguish the total score of each player versus their turn scores?
The turn score becomes 0 when rolling a 1, but the total score does not.
 

FAQ: Solve C++ Pig Dice Game: Aragorn Wins!

How does the C++ Pig Dice game work?

The C++ Pig Dice game is a two-player game where each player takes turns rolling a pair of dice. The goal of the game is to be the first player to reach 100 points. On each turn, a player can choose to roll the dice or hold. If the player rolls a 1, they lose their turn and all points for that round. If they roll any other number, it is added to their round score. The player can continue rolling until they choose to hold or they roll a 1. Once a player reaches 100 points, the game is over and that player wins.

What is the strategy for winning the C++ Pig Dice game?

The strategy for winning the C++ Pig Dice game is to balance risk and reward. Rolling a 1 can result in losing all points for that round, so it may be best to hold if you have a high round score. However, if your opponent is close to winning, it may be worth taking the risk and continuing to roll the dice. It is also important to keep track of your opponent's score and adjust your strategy accordingly.

How do you keep track of scores in the C++ Pig Dice game?

In the C++ Pig Dice game, the scores are stored in two variables: player1Score and player2Score. These variables are updated after each turn to keep track of the overall score for each player. The round score for each player is also stored in separate variables and is added to the overall score if the player chooses to hold. A simple if/else statement is used to determine which player's turn it is and update the appropriate score variable.

Can the C++ Pig Dice game be modified to have more players?

Yes, the C++ Pig Dice game can be modified to have more players. To do so, you would need to create additional variables to store the scores for each player and modify the logic to determine which player's turn it is. You could also add a loop to continue the game until one player reaches a certain number of points.

Is it possible to play the C++ Pig Dice game on a computer?

Yes, it is possible to play the C++ Pig Dice game on a computer. The game can be coded using a C++ compiler and played through the command line. Alternatively, it could also be developed into a graphical user interface game using a library like Qt or SFML.

Similar threads

Replies
1
Views
3K
Replies
17
Views
2K
Replies
53
Views
6K
Replies
2
Views
3K
Replies
2
Views
3K
Replies
3
Views
1K
Replies
2
Views
7K
Back
Top