CS 312 Assignment 7, Pig (Dice Game) Simulation

Programming Assignment 7:  Pair Assignment. You must complete this assignment by yourself or with one other person from your discussion section. Partners must be in the same small discussion section.

You may work with one other person (You make work with one other person on this assignment using the pair programming technique.. Read this paper on pair programming. One solution will be turned in for the pair. Once you start working with one partner on an assignment you may not switch partners. If you do not wish to work with a partner after starting on an assignment you must both start from scratch and complete it individually. The intent here is you work on the assignment together, at the same time, at the same computer. Do not simply try to work on different parts independently and then try to put it together.) 

To complete this assignment you must sign up for an Assignment 7 group on Canvas. The groups have already been created and are under the People section of Canvas. Even if you are working by yourself, you must sign up for a group. See this page for information on how to join a group in Canvas.

If you took CS312 previously and worked with a partner on this assignment then you must start this program from scratch. If you took CS312 previously and want to work with a partner on this assignment you must start from scratch.

If you use a slip day, each student is charged a slip day. (And if you use two slip days each student is charged two slip days.) Both students must have the required number of slip days or the assignment score is 0 for both students.

Placed online: Wednesday, October 15
20 points, ~2% of total grade
Due: no later than 11 pm, Thursday, October 23
General Assignment Requirements

The purpose of this assignment is to implement a program that plays the simulates the dice game of Pig. To do the program you will practice the following techniques.

  1. Practice creating a structured program to solve a problem.
  2. Write methods that use parameters.
  3. Practice using indefinite loops.
  4. Generate and use random numbers.
  5. Write a program that performs a large number of simulations to determine the effectiveness of various strategies

You are limited to the language features from chapters 1 through 6 of the textbook, Building Java Programs 3rd edition.

Provided Files:

  1. PigSimulation.java : A shell file with a main method, some starter code, and the header information. There are also some questions at the bottom of the file. Answer these questions in the comment.
  2. pig_output_1.txt : A file with the output of the program. Given the same starting seed, 2000316, for the random number generator your program must produce EXACTLY the same output.
  3. pig_output_2.txt : A file with the output of the program. Given the same starting seed, 20141015, for the random number generator your program must produce EXACTLY the same output.
  4. pig_output_3.txt  : A file with the output of the program. Given the same starting seed, 615853, for the random number generator your program must produce EXACTLY the same output.
  5. pig_detailed_output_1.txt : A file with detailed output for the outcome of each game when running experiments. The output is the game number, player 1's score, player 2's score, and the number of rounds.

Description: Pig is a dice game between two players. The Wikipedia article summarizes the rules nicely:

"Each turn, a player repeatedly rolls a die until either a 1 is rolled or the player decides to "hold"

The first player to score 100 or more points wins."

You can play a version of the game against a computer player that plays optimally at this site. There is another version in the programming language Scratch here.

Strategies: There are various strategies for how to approach the game. The decision a player has to make after each roll, if it is not a 1 and the turn is over, is whether to hold and take the points for the turn or roll again to try and get more points for the turn. Strategies could depend on the points so far for the turn, the number of successful rolls for the turn, the difference between players' scores, or some combination of those factors. Obviously if a player's score plus their points for a round are greater than or equal to the goal, normally 100, the player should hold and win the game.

You will implement two strategies in your program:

First Strategy: Roll until the total for the turn is 15 or greater. In this strategy if the die roll was not a 1, compare the current point total to the turn to 15. If the point total for the turn is less than 15 roll again. If the point total for the turn is greater than or equal to 15, hold and end the turn.

Second Strategy: Roll until 5 successful rolls are made in the turn. In this strategy the player ignores the current point total for the turn and instead looks at the number of successful rolls made so far this turn. If the number of successful (non 1) rolls this turn is less than 5 the player rolls again. If the number of successful (non 1) rolls this turn is 5 the player holds.

Output: Your program must produce the same output as the sample output: (pig_output_1.txt, pig_output_2.txt , pig_output_3.txt ) This is possible because even though we are using a Random object, if a Random object is given the same initial seed value, in this case 2000316, the numbers produced by the Random object will be exactly the same.

Note, the complete program runs one game with detailed output and then 40,000 games with no output, just a summary of results.

On the sample game with output player 1 is using the first strategy, stop after 15 points for the turn, and the second player is using the second strategy, stop after 5 rolls for the turn.

After the single game with detailed output the program runs 10,000 games for each of the following conditions:

  1. player 1: stop after 15 points - player 2: stop after 15 points
  2. player 1: stop after 5 rolls - player 2: stop after 5 rolls
  3. player 1: stop after 15 points - player 2: stop after 5 rolls
  4. player 1: stop after 5 rolls - player 2: stop after 15 points

Approach: You are not using anything new in this program other than the Random object and its nextInt() method. The rest is review of the programming techniques you have already learned: output, variables, parameters, definite and indefinite loops, parameters, conditional execution, methods for structure and to remove redundancy. As always you will graded on how well written your program is in addition to the correctness of the output.

Rolling the dice. When rolling the die you must call the nextInt(int) method on the Random object. For example:

int roll = randNumGen.nextInt(SIDES) + 1; // where randNumGen is the Random object and SIDES is a class constant == 6.

As usual we will be grading on correctness and style (program hygiene).

When finished, turn in your PigSimulation.java file via your Canvas Assignment 7 group.


Checklist: Did you remember to:

Back to the CS 312 homepage.