O-O Pig Game

Pig is a folk jeopardy dice game with simple rules: Two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 ("pig") 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:

Problem:  Create an object-oriented implementation of a game of Pig where the user plays against a "hold at 20 or goal" computer player that rolls until a 1 ("pig") is rolled, or the turn total is greater than or equal to 20, or the score plus the turn total is greater than or equal to 100.  The first player is chosen randomly.

O-O Design Constraints:

BlueJ UML diagram:

Although one need not implement in Java, the object-oriented specification is supplied via HTML Javadoc documentation.  The global input/output specification is given here:

Input Format:  An empty input (i.e., Enter) indicates that the user wishes to roll.  Any entered line of non-zero length indicates that the user wishes to hold.

Output Format:

Sample Transcript (input underlined):

You will be player 2.
Enter nothing to roll; enter anything to hold.
Player 1 score: 0
Player 2 score: 0
It is player 1's turn.
Roll: 5
Roll: 3
Roll: 5
Roll: 1
Turn total: 0
New score: 0
Player 1 score: 0
Player 2 score: 0
It is player 2's turn.
Roll: 6
Turn total: 6 	Roll/Hold? (Enter)
Roll: 5
Turn total: 11 	Roll/Hold? (Enter)
Roll: 6
Turn total: 17 	Roll/Hold? (Enter)
Roll: 2
Turn total: 19 	Roll/Hold? (Enter)
Roll: 2
Turn total: 21 	Roll/Hold? h
Turn total: 21
New score: 21
Player 1 score: 0
Player 2 score: 21
It is player 1's turn.
Roll: 5
Roll: 6
Roll: 3
Roll: 5
Roll: 1
Turn total: 0
New score: 0
Player 1 score: 0
Player 2 score: 21
It is player 2's turn.
Roll: 6
Turn total: 6 	Roll/Hold? (Enter)
Roll: 6
Turn total: 12 	Roll/Hold? (Enter)
Roll: 2
Turn total: 14 	Roll/Hold? (Enter)
Roll: 6
Turn total: 20 	Roll/Hold? h
Turn total: 20
New score: 41
Player 1 score: 0
Player 2 score: 41
It is player 1's turn.
Roll: 3
Roll: 3
Roll: 6
Roll: 4
Roll: 4
Turn total: 20
New score: 20
Player 1 score: 20
Player 2 score: 41
It is player 2's turn.
Roll: 3
Turn total: 3 	Roll/Hold? (Enter)
Roll: 3
Turn total: 6 	Roll/Hold? (Enter)
Roll: 2
Turn total: 8 	Roll/Hold? (Enter)
Roll: 2
Turn total: 10 	Roll/Hold? (Enter)
Roll: 4
Turn total: 14 	Roll/Hold? (Enter)
Roll: 2
Turn total: 16 	Roll/Hold? (Enter)
Roll: 4
Turn total: 20 	Roll/Hold? h
Turn total: 20
New score: 61
Player 1 score: 20
Player 2 score: 61
It is player 1's turn.
Roll: 5
Roll: 1
Turn total: 0
New score: 20
Player 1 score: 20
Player 2 score: 61
It is player 2's turn.
Roll: 3
Turn total: 3 	Roll/Hold? (Enter)
Roll: 3
Turn total: 6 	Roll/Hold? (Enter)
Roll: 5
Turn total: 11 	Roll/Hold? (Enter)
Roll: 2
Turn total: 13 	Roll/Hold? (Enter)
Roll: 6
Turn total: 19 	Roll/Hold? h
Turn total: 19
New score: 80
Player 1 score: 20
Player 2 score: 80
It is player 1's turn.
Roll: 3
Roll: 1
Turn total: 0
New score: 20
Player 1 score: 20
Player 2 score: 80
It is player 2's turn.
Roll: 2
Turn total: 2 	Roll/Hold? (Enter)
Roll: 2
Turn total: 4 	Roll/Hold? (Enter)
Roll: 4
Turn total: 8 	Roll/Hold? (Enter)
Roll: 2
Turn total: 10 	Roll/Hold? (Enter)
Roll: 3
Turn total: 13 	Roll/Hold? (Enter)
Roll: 6
Turn total: 19 	Roll/Hold? (Enter)
Roll: 5
Turn total: 24 	Roll/Hold? h
Turn total: 24
New score: 104

Extra Exercises:

1. Use an alternative computer play policy.  Let i be the player's score, j be the opponent's score, and k be the current turn total.  Some alternative play policies:

2. Allow play of multiple games, alternating the starting player and computing win statistics.


Todd Neller