Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. Players take turns.

The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins.

Players take turns. On each turn, a player rolls a six-sided die:

If the player rolls a 1, then the player gets no new points and it becomes the other players turn.

If the player rolls 2 through 6, then he or she can either

ROLL AGAIN or

HOLD. At this point, the sum of all rolls is added to the players score and it becomes the other players turn.

Write a program that uses an object of the Die class ( inserted below) to play the game of Pig, where one player is a human and the other is the computer.

When it is the humans turn, the program should show the score of both players and the previous roll. Allow the human to input r to roll again or h to hold. The computer program should play according to the following rule:

Keep rolling when it is the computers turn until it has accumulated 20 or more points, then hold.

If the computer wins or rolls a 1, then the turn ends immediately.

Allow the human to roll first.

Die class:

import java.util.Random;

public class Die {

//create the private faceValue die.

private int faceValue;

//method getFaceValue to return the die face value

public int getFaceValue()

{

return faceValue;

}

//method setFaceValue to set the faceValue

public void setFaceValue(int faceValue)

{

this.faceValue = faceValue;

}

//method roll to roll die using a random generated number from 1 to 6 and set facevalue

//to the number

public int roll() {

Random random = new Random();

setFaceValue(random.nextInt(6) + 1);

return faceValue;

}

//method toString to demonstrate the die value

public String toString() {

return "Die [faceValue=" + faceValue + "]";

}

}//end class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil, Elizabeth O'Neil

2nd Edition

1558605800, 978-1558605800

More Books

Students also viewed these Databases questions

Question

5. Recognize your ability to repair and let go of painful conflict

Answered: 1 week ago