Pig Game

Objective

To continue practicing Java programming including control structures, input, output, variables, functions and random numbers.

For this program, you will write a version of the Pig dice game in Java. Pig is a two-player game in which each player tries to get 100 points before the other.

In Pig, players take turns rolling a die. For each turn, the player may roll the die as many times as they like until they get a 1. If a player rolls a 1, then they receive zero points for that turn. If a player elects to stop rolling (or "hold") before they get a 1, then they get the sum of all die rolls that turn. The score for that turn is then added into their final score.

For example, if it's my turn I can choose to roll, and roll a 5. Then, I can choose to roll again and roll a 2. Then I can choose to roll again and roll a 6. Then I can choose to hold, and my score for that turn is 13.

On the other hand, if I roll a 6, then a 3, then a 1, I receive 0 points for that turn.

The strategy in Pig is to try to get as many points as you can, then hold before you lose them all, which happens when you roll a 1.

Details

• Your program should allow the user to play Pig against a computer opponent.
• The program should alternate between the players turn, wherein you ask the user if they want to roll or hold, and calculate their score appropriately, and the computers turn.
• During the computers turn, you should use the following strategy: try to get at least a score of 20 each round, then hold.
• You should alternate between player and computer turns until one gets a total score of 100.
• As soon as someone gets a score of 100, the game is over (you should not allow rolls over this).
• After each complete game, you should ask the user if they want to play again.
• It should be randomly chosen who goes first.
• You can use the Random class for random dice throws as discussed in class.
• You should split this program into at least three functions.

General Requirements

• Your code should be readable and reasonably indented.
• You must provide comments in your program.
• You must include a comment at the top with your name.
• You should not have a "package" line in your program.
• You should not have the comments NetBeans puts in by default e.g. the "license header", "@author" or "TODO" lines.
• Your file name/class name should be something descriptive, not something like "JavaApplication4".

Example Run

Here is an example run of this program. User input is shown in bold face:

Your total score is 0
Your turn score is 0
The computer score is 0
Would you like to (R)oll or (H)old?
:R
You rolled a 5
Your total score is 0
Your turn score is 5
The computer score is 0
Would you like to (R)oll or (H)old?
:R
You rolled a 3
Your total score is 0
Your turn score is 8
The computer score is 0
Would you like to (R)oll or (H)old?
:R
You rolled a 3
Your total score is 0
Your turn score is 11
The computer score is 0
Would you like to (R)oll or (H)old?
:R
You rolled a 1
You scored 0 this turn!

Computer turn.
Computer rolled a 3
Computer rolled a 4
Computer rolled a 1
Computer scored 0

Your total score is 0
Your turn score is 0
The computer score is 0
Would you like to (R)oll or (H)old?
:R
You rolled a 6
Your total score is 0
Your turn score is 6
The computer score is 0
Would you like to (R)oll or (H)old?
:R
You rolled a 2
Your total score is 0
Your turn score is 8
The computer score is 0
Would you like to (R)oll or (H)old?
:R
You rolled a 2
Your total score is 0
Your turn score is 10
The computer score is 0
Would you like to (R)oll or (H)old?
:R
You rolled a 2
Your total score is 0
Your turn score is 12
The computer score is 0
Would you like to (R)oll or (H)old?
:H
You scored 12 this turn!

Computer turn.
Computer rolled a 6
Computer rolled a 4
Computer rolled a 1
Computer scored 0

Your total score is 12
Your turn score is 0
The computer score is 0
Would you like to (R)oll or (H)old?
:R
You rolled a 5
Your total score is 12
Your turn score is 5
The computer score is 0
Would you like to (R)oll or (H)old?
:R
You rolled a 5
Your total score is 12
Your turn score is 10
The computer score is 0
Would you like to (R)oll or (H)old?
:R
You rolled a 3
Your total score is 12
Your turn score is 13
The computer score is 0
Would you like to (R)oll or (H)old?
:R
You rolled a 4
Your total score is 12
Your turn score is 17
The computer score is 0
Would you like to (R)oll or (H)old?
:H
You scored 17 this turn!

Computer turn.
Computer rolled a 6
Computer rolled a 1
Computer scored 0

Your total score is 29
Your turn score is 0
The computer score is 0
Would you like to (R)oll or (H)old?
:R
You rolled a 4
Your total score is 29
Your turn score is 4
The computer score is 0
Would you like to (R)oll or (H)old?
:R
You rolled a 6
Your total score is 29
Your turn score is 10
The computer score is 0
Would you like to (R)oll or (H)old?
:R
You rolled a 1
You scored 0 this turn!

Computer turn.
Computer rolled a 4
Computer rolled a 1
Computer scored 0

Your total score is 29
Your turn score is 0
The computer score is 0
Would you like to (R)oll or (H)old?
:R
You rolled a 2
Your total score is 29
Your turn score is 2
The computer score is 0
Would you like to (R)oll or (H)old?
:R
You rolled a 1
You scored 0 this turn!

Computer turn.
Computer rolled a 5
Computer rolled a 4
Computer rolled a 6
Computer rolled a 6
Computer scored 21

Your total score is 29
Your turn score is 0
The computer score is 21
Would you like to (R)oll or (H)old?
:R
You rolled a 2
Your total score is 29
Your turn score is 2
The computer score is 21
Would you like to (R)oll or (H)old?
:R
You rolled a 6
Your total score is 29
Your turn score is 8
The computer score is 21
Would you like to (R)oll or (H)old?
:R
You rolled a 2
Your total score is 29
Your turn score is 10
The computer score is 21
Would you like to (R)oll or (H)old?
:R
You rolled a 6
Your total score is 29
Your turn score is 16
The computer score is 21
Would you like to (R)oll or (H)old?
:H
You scored 16 this turn!

Computer turn.
Computer rolled a 6
Computer rolled a 4
Computer rolled a 4
Computer rolled a 5
Computer rolled a 3
Computer scored 22

Your total score is 45
Your turn score is 0
The computer score is 43
Would you like to (R)oll or (H)old?
:R
You rolled a 6
Your total score is 45
Your turn score is 6
The computer score is 43
Would you like to (R)oll or (H)old?
:R
You rolled a 5
Your total score is 45
Your turn score is 11
The computer score is 43
Would you like to (R)oll or (H)old?
:R
You rolled a 2
Your total score is 45
Your turn score is 13
The computer score is 43
Would you like to (R)oll or (H)old?
:H
You scored 13 this turn!

Computer turn.
Computer rolled a 1
Computer scored 0

Your total score is 58
Your turn score is 0
The computer score is 43
Would you like to (R)oll or (H)old?
:R
You rolled a 5
Your total score is 58
Your turn score is 5
The computer score is 43
Would you like to (R)oll or (H)old?
:R
You rolled a 5
Your total score is 58
Your turn score is 10
The computer score is 43
Would you like to (R)oll or (H)old?
:R
You rolled a 4
Your total score is 58
Your turn score is 14
The computer score is 43
Would you like to (R)oll or (H)old?
:H
You scored 14 this turn!

Computer turn.
Computer rolled a 5
Computer rolled a 6
Computer rolled a 1
Computer scored 0

Your total score is 72
Your turn score is 0
The computer score is 43
Would you like to (R)oll or (H)old?
:R
You rolled a 5
Your total score is 72
Your turn score is 5
The computer score is 43
Would you like to (R)oll or (H)old?
:R
You rolled a 4
Your total score is 72
Your turn score is 9
The computer score is 43
Would you like to (R)oll or (H)old?
:R
You rolled a 6
Your total score is 72
Your turn score is 15
The computer score is 43
Would you like to (R)oll or (H)old?
:H
You scored 15 this turn!

Computer turn.
Computer rolled a 5
Computer rolled a 1
Computer scored 0

Your total score is 87
Your turn score is 0
The computer score is 43
Would you like to (R)oll or (H)old?
:R
You rolled a 4
Your total score is 87
Your turn score is 4
The computer score is 43
Would you like to (R)oll or (H)old?
:R
You rolled a 4
Your total score is 87
Your turn score is 8
The computer score is 43
Would you like to (R)oll or (H)old?
:R
You rolled a 6
Your total score is 87
Your turn score is 14
The computer score is 43
Would you like to (R)oll or (H)old?
:H
You scored 14 this turn!

Your final score is 101
Computer final score is 43
Congratulations, you won!!!
Play again (Y/N)?
:N

Submitting

To submit your program, email the source file to ifinlay@umw.edu.