Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

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.

Java, Programming

  • Category:- Java
  • Reference No.:- M9312258
  • Price:- $70

Priced at Now at $70, Verified Solution

Have any Question?


Related Questions in Java

Answer the following question whats the difference public

Answer the following Question : What's the difference public inheritance and private inheritance? What can derived classes inherit from base classes? What cannot be inherited from base classes?

Object-oriented software development1 introduction 11

OBJECT-ORIENTED SOFTWARE DEVELOPMENT 1. Introduction 1.1 Assignment Requirement 1.2 Deliverables and Structure (what to submit) 1.3 Software Restrictions 1.4 How to score high... 1.5 Assumptions 2. System Requirements 2. ...

Can someone please help me with the following java

can someone please help me with the following java question The input is an N by N matrix of nonnegative integers. Each individual row is a decreasing sequence from left to right. Each individual column is a decreasing s ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

Assessment database and multithread programmingtasktask 1

Assessment: Database and Multithread Programming Task Task 1: Grade Processing University grading system maintains a database called "GradeProcessing" that contains number of tables to store, retrieve and manipulate stud ...

Project requirementsfor the problem described in the next

Project requirements For the problem described in the next section, you must do the following: 1. include your student ID at the end of all filenames for all java code files. Three classes have been identified in section ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Question slideshows or carousels are very popular in

Question : Slideshows (or carousels) are very popular in websites. They allow web developers to display news or images on the website in limited space. In this code challenge, you are required to complete the JavaScript ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As