Home CPSC 220

Pig Game

 

Due: September 21

 

Objective

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

Task

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.

You can try Pig for yourself here!.


 

Details


 

General Requirements


 

Example Run

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


Your turn!
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 turn!
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 turn!
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 turn!
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 turn!
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 turn!
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 turn!
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 turn!
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 turn!
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 turn!
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.

Copyright © 2024 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.