Pig is a dice game with the following rules: Two players race to reach 100 points. Each turn, a player
repeatedly rolls a die until either a 1 is rolled or the player holds and scores the sum of the rolls (i.e. the
turn total). At any time during a player’s turn, the player is faced with two decisions:
• roll – If the player rolls a
o 1: the player scores nothing and it becomes the opponent’s turn.
o 2 – 6: the number is added to the player’s turn total and the player’s turn continues.
• hold – The turn total is added to the player’s score and it becomes the opponent’s turn.
The first player to score 100 or more points wins. For example, the first player, Ann, begins a turn with a
roll of 5. Ann could hold and score 5 points, but chooses to roll again. Ann rolls a 2, and could hold with a
turn total of 7 points, but chooses to roll again. Ann rolls a 1, and must end her turn without scoring. The
next player, Bob, rolls the sequence 4-5-3-5-5, after which he chooses to hold, and adds his turn total of
22 points to his score.
Your job is to write a C++ program in which the computer will play the user in a game of Pig.
A key decision to build strategy for the computer player is how large a turn total should be risked to
possibly get an even larger total. Use “hold at 20” strategy for your computer player (unless holding will
win the game). (This will maximize the expected number of points per turn, though it does not maximize
the expected probability of winning.)
Below are bullet points describing specific behaviors your program should have.
• Before every round, the program must output the following where xx is the user’s score and
yy is the computer’s score:
New round:
User: xx
Computer: yy
• The user goes first.
• Before the user makes a decision, the program should tell them their current accumulated
points and asks them what they would like to do using this exact prompt:
You have zz points this round.
Would you like to ‘roll’ or ‘hold’ ?
• The user should then input their request. If it is neither “roll” or “hold” they get the
following message (where bad_command is whatever the user entered) and are reminded
of their points and asked to make their decision again:
I did not understand “bad_command”, sorry.• If the user decided to roll, they should be told their roll with the following message, and be
prompted to decide what to do again:
You rolled a n.
• If the user’s roll ends their turn, they should be told the following:
Sorry, your turn is over, and you gain 0 points.
• If the user holds, they should be told how many points they held this turn and their new
total (notice there is no period at the end of this line!):
You held xx points, bringing you to yy
• Once it is the computer’s turn, it pursues its “hold at 20” strategy.
• Every time it rolls, output that roll like so:
The Computer rolled a n.
• If the computer rolls a 1, output the following message and end its turn:
The Computer turn is over, and they gain 0 points.
• If the computer manages to hold, output how many points they held this turn and their new
total and end its turn:
The Computer held xx points, bringing you to yy
• At the end of the game output one of the following two lines:
The Computer wins!
or
You win!
• The random dice rolls are supplied by a file `rolls.txt`. I provide a different set of rolls for
each test I give it. Leave the first eight (8) lines of the main function alone. These load the
dice rolls. You can access the next dice roll, and store it in an integer result, with the
following excerpt of code:
result = roll();
• You must never re-roll the dice! That is to say, every time `roll()` is evaluated in your code,
you must use that value as a dice roll! One hint you are re-rolling is if the Autograder is
telling you that the dice rolls are not correct.I’ve provided everything you need to get started on the program in this zip file. Note that I’ve given you
an example `rolls.txt` file that specifies the random dice, a `Makefile`, a `helper.cpp` file that should not
be touched, and the start of a `main.cpp` file that will contain your program! You can build with the
command `make` and run with the command `./main`.
Your project can be submitted to the Autograder at https://autograder.benvds.com/web/project/4.
Note that you are only submitting the `main.cpp` file. All other changes you make are impossible to
submit! And for good reason: I want you to solve the problem only by adding to the one C++ file.
The grade for this project is composed of two portions: autograding and handgrading. The autograder
determines the correctness of your code while handgrading determines how easy it is to read and
understand. Both are absolutely vital for a good programmer to master. However, I weight autograding
heavily because you can know if you were correct before the deadline, and therefore fix it.
For this project, the Autograder will give you 1 point for compiling correctly, and then run 5 tests, each
worth 3 points. You only get the points for the test if your solution is exactly correct. The Autograder
can be very unforgiving. This totals up to 16 points.
The remaining 4 points on this project are determined by my handgrading. I will look at your code and
determine if it meets the following criteria:
• Consistent Code Style
• Legible Code Style
• Comments Exist and are Helpful
• Variable Names are Descriptive
Each of these criteria are useful metrics to describe the readability of a piece of code. Each is
also worth 1 point and up to my discretion. If VSCode automatically formats your code when
you save, you are probably going to get the first two points! Then you just need to add helpful
comments and make sure your variables are descriptive.
0 comments