Question
Pass the Pigs Create a class named PassThePigs. See the UML above (some of the required constants are not listed in the UML.) See this
Pass the Pigs
-
Create a class named PassThePigs. See the UML above (some of the required constants are not listed in the UML.)
-
See this Wikipedia article for an explanation of the game.
-
There are six possible rolls for each pig, listed in the table below. (The picture shows a trotter and a snouter.)
Name | Chance | Score |
Sider (side without dot up) | 33% | 0 |
Sider (side with dot up) | 33% | 0 |
Trotter (pig on 4 feet) | 10% | 5 |
Razorback (all 4 feet in the air) | 20% | 5 |
Snouter (on nose and two feet) | 3% | 10 |
Leaning jowler (on nose, ear, and foot) | 1% | 15 |
-
Create String constants for the six names. (Only three are listed in the UML. Follow the pattern to create the other three.)
-
Create constants for the six scores. (Only three are listed in the UML. Follow the pattern to create the other three.)
-
Create six probability constants. (Only three are listed in the UML. Follow the pattern to create the other three.) The actual probability from the table above is built into the difference between successive constants as shown in the parentheses.
-
Sider without dot: 33 (33%)
-
Sider with dot: 66 (66 - 33 = 33%)
-
Trotter: 76 (10%)
-
Razorback: 96 (20%)
-
Snouter: 99 (3%)
-
Leaning jowler: 100 (1%)
-
-
The program code is in main.
-
Create a String variable representing each rolled pig.
-
Create two score variables: one for a single roll of two pigs, one for the total for the turn - possibly multiple rolls.
-
Initialize the score for the whole turn (set to 0).
-
The rest of the program should be in a loop. The loop will represent one turn.
-
Set the score for this roll to 0.
-
Create a random number between 0-99 to represent a pig roll. (see section 5.2.)
-
Use the probability constants (#6 above) as range cutoffs (hint: use if - else if, see section 6.6 Detecting ranges) to:
-
Assign the appropriate roll name to the pig String variable
-
Add the appropriate score for this pig to the score for this roll.
-
-
Repeat the two steps above (15 & 16) for the second pig. If youre clever, use a loop.
-
Handle special cases
-
If the two pigs are different siders, this roll and the total score are set to 0, and the play (and loop) stops. Display Pig out!
-
If the two pigs are the same, the roll score is doubled
-
If the two pigs are the same type of sider, this roll score is 1 (not 0.)
-
-
Display the score for this roll, the score for the turn.
-
If the user didnt pig out, ask whether the user wants to roll again.
-
Use Scanners next() method to get the user input. (Not nextLine().)
-
Continue the loop until the user says they want to stop (or when a pig out occurs.)
-
Suggestion: To test that your scoring is correct, temporarily replace the Random classs nextInt method with the Scanner classs nextInt method. Then test with numbers 0-99. Otherwise, you may wait a long time to get a Leaning Jowler!
-
Submit your .java file.
Sample run
(user input is bold)
Rolling ... Sider(dot) & Sider(dot)
this roll: 1
this turn: 1
Roll again (y/n)? y
Rolling ... Trotter & Trotter
this roll: 20
this turn: 21
Roll again (y/n)? y
Rolling ... Snouter & Razorback
this roll: 15
this turn: 36
Roll again (y/n)? y
Rolling ... Sider(dot) & Razorback
this roll: 5
this turn: 41
Roll again (y/n)? y
Rolling ... Razorback & Sider(no dot)
this roll: 5
this turn: 46
Roll again (y/n)? y
Rolling ... Trotter & Razorback
this roll: 10
this turn: 56
Roll again (y/n)? y
Rolling ... Sider(dot) & Snouter
this roll: 10
this turn: 66
Roll again (y/n)? y
Rolling ... Razorback & Sider(dot)
this roll: 5
this turn: 71
Roll again (y/n)? y
Rolling ... Sider(dot) & Sider(no dot)
Pig out! No score.
Your turn is over.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started