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

  • Thread starter Thread starter student2019
  • Start date Start date
  • Tags Tags
    C++ Dice Game
AI Thread Summary
The discussion revolves around troubleshooting a program for the dice game Pig, where two players take turns rolling a die to accumulate points. The main issues highlighted include a malfunctioning while loop that continues running when a player rolls a 1, which should end their turn and reset their turn score to zero. Additionally, there's a request to eliminate the prompt asking if the player wants to roll again right before announcing the winner. Suggestions for resolving these issues include breaking the code into smaller functions for better clarity and separating the total score from the turn score, ensuring that the total score remains intact even when a player rolls a 1. This approach aims to streamline the game's logic and improve the overall functionality of the program.
student2019
Messages
1
Reaction score
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
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top