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

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

  Write a program to find solution tocryparithmetic puzzle

Write a program (Crypta.java) that finds a solution to the cryparithmetic puzzle: TOO + TOO + TOO+ TOO = GOOD

  Java project

Prompt the user for an int between lower and upper boundary.

  Java program use breadth-first search closest broadcast

Write the java program which will use breadth-first search (which you implement as part of your program) to determine the closest broadcast vertex for each vertex in graph.

  Implement the lexical and syntactic analysis

Implement the lexical and syntactic analysis of Minifun programming language.

  Write a program that assigns seats on an airplane

Prepare a program that teaches arithmetic to a young child. The program tests subtraction and addition. Write a program that assigns seats on an airplane.

  Write a java program in a netbeans project

Write a Java program in a NetBeans project

  Implement a class quiz

Implement a class Quiz that implements the Measurable interface.

  Develop a reliable transfer protocol over udp

Develop a reliable transfer protocol over UDP. Focus on a Stop- and-Wait protocol.

  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.

  Prepare a program that constructs a binary tree

Write your own implementation of primitive operations and use self-referential classes in your implementation.

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