The game of pig

Assignment Help JAVA Programming
Reference no: EM1376957

Requirements

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:

•roll - If the player rolls a

1: the player scores nothing and it becomes the opponent's turn.
2 - 6: the number is added to the player's turn total and the player's turn continues.

•hold - The turn total is added to the player's score and it becomes the opponent's turn.

Write a program that allows a single player to practice the game (that is, there will be no opponent, just a single player). Play 5 turns in the game to get a total score for the player. Refer to the sample output below for a detailed example.

Declare and use the following methods appropriately. You will only receive full credit if these methods are properly used in your solution.

•public static int roll();
oGenerates a random dice roll value and returns it.

•public static int playTurn();

oPlays a single turn by:
-Roll a the die using the roll() method.
-If the value is 1, the turn total is reset to 0.
-If the value is not 1, add the value to the player's turn score.
-Print the rolled value and turn total using the printRollResult(...) method.
-If the value is 1, end the turn.
-If the value is not 1, prompt the player to roll again or hold using the isHold() method. If the user selects to roll again, repeat this process.

•public static void printRollResult(int rollValue, int turnTotal)
oDisplay the rolled value and turn total.

•public static boolean isHold();
oPrompt the user to 1. Roll again or 2. Hold.
oReturn true if the user selects to hold.

The output must be:
•For each turn, print a line with "Turn:" and the turn number.
•For each roll, print a line with "Roll value:" and the random die roll value (1-6) followed by "Turn total:" and the turn total (0 if the user rolled a pig).
•For each roll that isn't a 1 (i.e. the pig), a prompt to hold or roll again.
•At the end of the game, print "Game total: " and the total score.

Be sure to use proper coding conventions including indentation, constants, and comments. Be sure to use method level comments and JavaDoc style.

Formula
A random die roll can be simulated with the statement:

int roll = (int) (Math.random() * 6) + 1;

Hint
The main() method will call the playTurn() method 5 times, summing the results into a game total. The playTurn() method will call the roll(), printRollResult(), and isHold() methods.

Example Program Execution

Game 1 Example
Turn: 1
Roll value: 3
Turn total: 3
1) Roll again or 2) Hold? 1
Roll value: 5
Turn total: 8
1) Roll again or 2) Hold? 1
Roll value: 2
Turn total: 10
1) Roll again or 2) Hold? 1
Roll value: 3
Turn total: 13
1) Roll again or 2) Hold? 1
Roll value: 3
Turn total: 16
1) Roll again or 2) Hold? 1
Roll value: 5
Turn total: 21
1) Roll again or 2) Hold? 2

Turn: 2
Roll value: 6
Turn total: 6
1) Roll again or 2) Hold? 1
Roll value: 2
Turn total: 8
1) Roll again or 2) Hold? 1
Roll value: 4
Turn total: 12
1) Roll again or 2) Hold? 1
Roll value: 5
Turn total: 17
1) Roll again or 2) Hold? 1
Roll value: 6
Turn total: 23
1) Roll again or 2) Hold? 2

Turn: 3
Roll value: 3
Turn total: 3
1) Roll again or 2) Hold? 1
Roll value: 3
Turn total: 6
1) Roll again or 2) Hold? 1
Roll value: 2
Turn total: 8
1) Roll again or 2) Hold? 1
Roll value: 4
Turn total: 12
1) Roll again or 2) Hold? 1
Roll value: 6
Turn total: 18
1) Roll again or 2) Hold? 1
Roll value: 1
Turn total: 0

Turn: 4
Roll value: 2
Turn total: 2
1) Roll again or 2) Hold? 1
Roll value: 4
Turn total: 6
1) Roll again or 2) Hold? 1
Roll value: 4
Turn total: 10
1) Roll again or 2) Hold? 1
Roll value: 3
Turn total: 13
1) Roll again or 2) Hold? 1
Roll value: 3
Turn total: 16
1) Roll again or 2) Hold? 2

Turn: 5
Roll value: 5
Turn total: 5
1) Roll again or 2) Hold? 1
Roll value: 2
Turn total: 7
1) Roll again or 2) Hold? 1
Roll value: 2
Turn total: 9
1) Roll again or 2) Hold? 1
Roll value: 4
Turn total: 13
1) Roll again or 2) Hold? 1
Roll value: 6
Turn total: 19
1) Roll again or 2) Hold? 1
Roll value: 6
Turn total: 25
1) Roll again or 2) Hold? 1
Roll value: 3
Turn total: 28
1) Roll again or 2) Hold? 2

Game total: 88
Game 2 Example
Turn: 1
Roll value: 4
Turn total: 4
1) Roll again or 2) Hold? 1
Roll value: 3
Turn total: 7
1) Roll again or 2) Hold? 1
Roll value: 5
Turn total: 12
1) Roll again or 2) Hold? 1
Roll value: 3
Turn total: 15
1) Roll again or 2) Hold? 1
Roll value: 6
Turn total: 21
1) Roll again or 2) Hold? 2

Turn: 2
Roll value: 5
Turn total: 5
1) Roll again or 2) Hold? 1
Roll value: 2
Turn total: 7
1) Roll again or 2) Hold? 1
Roll value: 2
Turn total: 9
1) Roll again or 2) Hold? 1
Roll value: 5
Turn total: 14
1) Roll again or 2) Hold? 1
Roll value: 2
Turn total: 16
1) Roll again or 2) Hold? 1
Roll value: 4
Turn total: 20
1) Roll again or 2) Hold? 1
Roll value: 3
Turn total: 23
1) Roll again or 2) Hold? 1
Roll value: 4
Turn total: 27
1) Roll again or 2) Hold? 2

Turn: 3
Roll value: 6
Turn total: 6
1) Roll again or 2) Hold? 1
Roll value: 1
Turn total: 0

Turn: 4
Roll value: 1
Turn total: 0

Turn: 5
Roll value: 1
Turn total: 0

Game total: 48
Notes
The Game of Pig by Todd Neller and Clif Presser inspired this assignment.

Reference no: EM1376957

Questions Cloud

Critical thinking impact on decision process : Discuss and explain what critical thinking is and its impact on decision process and consider of your experience and reflect about what characteristics critical thinkers shown,
Give an example of a good decision : Give an example of a good decision you made that resulted in a bad outcome or an example of a bad decision you made that had a good outcome. Why was the decision considered good or bad?
Queuing problem for a business unit : Create and solve a queuing problem for a business unit. For illustration, phone calls coming to the receptionist, or work arriving by mail, phone, or in person.
Discuss the impact of critical thinking : Think about a condition where your critical thinking abilities would be tested or pushed to limit. For example, even the most skilled critical thinker will surely have a difficult time making a decision in a life or death condition.
The game of pig : Write a program that allows a single player to practice the game (that is, there will be no opponent, just a single player). Play 5 turns in the game to get a total score for the player. Refer to the sample output below for a detailed example.
Component of critical thinking : Perception is one component of critical thinking. Think of how your individual perceptions concerning a specific issue or problem have changed as a result of an event or experience.
Succession planning in an organization : Discuss the succession planning in an organization and what has been your experience with succession planning in the company’s you have worked for?
Discuss the nature of a problem : Discuss the nature of a problem or a decision and explain how might a problem present itself?
Identifying a problem through critical thinking : Determine forces of influence should be considered while identifying a problem by critical thinking and what methods can be used during problem formulation?

Reviews

Write a Review

JAVA Programming Questions & Answers

  Create your own date class

You are to write a program that determines the day of the week for New Year's Day in the year 3000. To do this, you must create your own date class (MyDate) and use the following interface and main program:

  Write a java class

Write a Java class called PQueue that extends the provided abstract QueueADT class.

  Javascript validation - cgi program

CGI mailer script form-mail.pl to your server's cgi-bin directory, and change the permissions on the script to make it executable - BMIS 310

  Java applet to find how much federal tax we need to pay

Write a java applet to determine how much federal tax we need to pay assuming the tax rate is 12%.  Ask the taxable income.

  Create a recursive factorial program

Assignment 1: Create a recursive factorial program that prompts the user for an integer  N  and writes out a series of equations representing the calculation of  N !. For example, if the input is 4, the output could be:

  Please write the code in java

Please write the code in java for  Recursion,  Sorting and Searching

  Function using javascript syntax to compute gross pay

Write a function using JavaScript syntax to compute a person's gross pay for a week. The function must receive the number of hours worked and the rate of pay per hour.

  Java application to generate three-digit random number

Write a Java application that generates a 3-digit random number 100 times. Display the output for each of the generated numbers and the sum of all 100 numbers.

  Write java program which will permit user to input data

Write the Java Program which will permit the user to input data. The data will be validated using a loop that requires the user to input the data until it is correct or in the correct range. T

  Write java program to receive input and displayed interest

Write a Java program that receive some input and displayed the interest. Format the number into 2 decimal places. Using loop is an advantage.

  Java program to write-read data from user using i-o function

Write java program to write and read data from user using I/O functions. Describe class with data members name[20],emp_id,basic pay,net pay.calculate all the allowances.

  Write down java program for furniture company

Write down a Java program for the furniture company. Ask user to select P for Pine, O for Oak or M for Mahogany. Illustrate price of table manufactured with chosen wood.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd