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 bean jsp program which will compute simple interest

Create the bean which will compute Simple Interest. Use bean in JSP program. Accept details of saving like principal amount, rate of interest, period-in years from user and show amount

  Data structures and algorithms

Code analysis and programming.

  Write java program which will permit user to make selection

Write the Java Program which will permit the user to make selection. You will present user with two options to perform, then you will perform action selected by user.

  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:

  Implement a class quiz

Implement a class Quiz that implements the Measurable interface.

  Implement avl tree

Implement AVL trees that allows both iterative traversal and recursive traversal.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Create java application which creates random phone number

Create and implement Java application which creates and prints a random phone number of the form xxx-xxx-xxxx. Include the dashes in the output.

  Prepare executable program and a dictionary program

Prepare executable program and a dictionary Program.

  Write complete java program to read from keyboard

Write complete Java program to read, from keyboard, a student's first and last name and six grades. It will display on screen last name then first letter of first name with a period.

  Write program in java to calculate interest

Write a program in java to do the following : the following data is stored for each customer in the bank: account number,average balance and number of transaction for each customer it is required to calculate the interest earn

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

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