CSC 221: Computer Programming I         Spring 2010

HW 5: Modeling and Repetition II

Part 1 of this assignment is to be completed in teams of two. You will work with a partner to design and implement a Java class for simulating the game of Pig. In part 2, you will work on your own to simulate a variant of the game.

Part 1: Working in a team of two.

You are to design and implement a class for simulating the game of Pig. Pig is a 2-player dice game in which the players take turns rolling a die. On a given turn, a player rolls until either (1) he/she rolls a 1, in which case his/her turn is over and no points are awarded, or (2) he/she chooses to hold, in which case the sum of the rolls from that player's turn are added to his/her score. The winner of the game is the first player to reach 100 points.

Your PigGame class should have the following methods:

Once you have completed and tested your class, perform repeated simulations to determine the optimal cutoff point total, i.e., the cutoff amount that minimizes the number of turns needed to complete a game. Provide a justification of your answer, including data obtained from your simulations.

Part 2: Working individually.

Implement a variant of the Pig game named DoublePigGame. In this variant, each player rolls two dice. If the roll is snake-eyes (double 1's), the player's turn is over and no points are awarded. Every other doubles roll is ignored - the player's turn continues but the roll is not added to the sum. Non-doubles result in the two dice values being added to the turn total.

As in part 1, you will want a playTurn method that simulates a single turn, a playGame method that simulates an entire game, and an averageGameLength method that calculates the average number of turns required to play a game.

Once you have completed and tested your class, perform repeated simulations to determine the optimal cutoff point total, i.e., the cutoff amount that minimizes the number of turns needed to complete a game. Provide a justification of your answer, including data obtained from your simulations.


Submit your PigGame and DoublePigGame classes and written answers via BlueLine. For part 1, only one copy of the code and answers needs to be submitted per team.