Question
using java b) holdAt20 : This method should mimic a single turn of Pig where the player rolls until a 1 is rolled or until
using java
b) holdAt20 : This method should mimic a single turn of Pig where the player rolls until a 1 is rolled or until the turn total is greater than or equal to 20
input parameter(s): none
return value: an integer value representing the number of points the player scored on this turn (ie, their turn total).
output: your method should print the value of each roll the player takes. When the player's turn is over, the method should print out the total points scored.
Examples:
holdAt20() might generate the output:
Roll: 4
Roll: 6
Roll: 5
Roll: 5
Turn total: 20
In this case, the method should return 20 (the player's score for this turn)
Another call to holdAt20() might generate:
Roll: 4
Roll: 3
Roll: 1
Turn total: 0
In this case, the method would return 0 (the player's score for this turn)
c) Once you have part b working, modify this method by adding a parameter to your holdAt20 method. This parameter should be an integer and will represent the player's current score. Modify your method to immediately hold if the turn would put the player's score at 100 or greater. The method should still return the score for the current turn.
Example: holdAt20(90) might generate the output
Roll: 2
Roll: 3
Roll: 4
Roll: 4
Turn total: 13
This call should return 13
d) holdUI: This method will also simulate a single player turn, but this method will incorporate user input. Specifically, this method will ask the user whether or not to continue rolling at each step.
Input parameter(s) : the method takes no parameters, but it will need to use a Scanner to read user input. Make the Scanner a class variable and initialize it to read from System.in . In other words, the Scanner variable should be declared and initialized outside of any method.
Return value: this method should return the turn's total score, like the holdAt20 method.
Output f ormat: The output for this method would also include a prompt to the user, and a running total as the user rolls. It should also include roll values, same as holdAt20 .
Example of a single call to holdUI .
Roll: 2 Turn total: 2 (1 to Roll, 2 to Hold): 1
Roll: 2 Turn total: 4 (1 to Roll, 2 to Hold): 1
Roll: 4 Turn total: 8 (1 to Roll, 2 to Hold): 1
Roll: 2 Turn total: 10 (1 to Roll, 2 to Hold): 1
Roll: 3 Turn total: 13 (1 to Roll, 2 to Hold): 1
Roll: 6 Turn total: 19 (1 to Roll, 2 to Hold): 1
Roll: 5 Turn total: 24 (1 to Roll, 2 to Hold): 2
Turn total: 24
In this example, the method should return 24 (total score of turn)
The user should make ALL decisions. In other words, the player can continue rolling as many times as they would like, and they can hold whenever they would like. They should also be able to continue rolling even if their current turn total would surpass 100 points.
e) playGame : This method should i mplement a 2-player game of Pig where the user plays against a computer player that always 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 (ie, the behavior implemented in holdAt20 ). The human player can make a decision when to roll or hold (ie, the behavior implemented in holdUI ). The first player is chosen randomly.
Input p arameter ( s ) : One String , which is the human player's name/ identifier .
Return value: this method has no return value
Output format:
Before the game, randomly select which player the user will be, and print the line "[Computer or User's name], you will be player #.", where # is the user's player number: either 1 or 2.
Before each turn, print a line with "Player 1 score: " and player 1's score. Print another line with "Player 2 score: " and player 2's score. Finally, print a line with "It is player #'s turn.", where "#" is replaced by the current player number. Play starts with player 1 and then alternates.
After a "pig" roll of 1, or a "hold", print a line with "Turn total: " followed by the turn total. In the case of a "pig", this turn total is 0. Then, print a line with "New score: " followed by the new score for the current player.
When the game ends (ie when one player reaches 100 points), you should print out a message indicating the game is over and which player won
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