Question

Hello, Could you please help me code this program in Java? It is important that all...

Hello,

Could you please help me code this program in Java? It is important that all rules are carefully followed.

Using the PairOfDice class from PP 4.9, design and implement a class to play a game called Pig. In this game, the user competes against the computer. On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 100 points before your opponent does. If, on any turn, the player rolls a 1, all points accumulated for that round are forfeited and control of the dice moves to the other player. If the player rolls two 1’s in one turn, the player loses all points accumulated thus far in the game and loses control of the dice. The player may voluntarily turn over the dice after each roll. Therefore the player must decide to either roll again (be a pig) and risk losing points or relinquish control of the dice, possibly allowing the other player to win. Implement the computer player such that it always relinquishes the dice after accumulating 20 or more points in any given round.

This is the Die class:

public class Die {
  
private final int MAX = 6; //Maximum facevalue.
  
private int faceValue; //Current value showing on the die.
  
//Constructor: Sets the initial face value.
  
public Die()
{
faceValue = 1;
}
  
//Rolls the die and returns the result.
public int roll()
{
faceValue = (int)(Math.random() * MAX ) + 1;
return faceValue;
}
//Face value mutator.
public void setFaceValue(int value)
{
faceValue = value;
}
  
//Face value accessor.
public int getFaceValue()
{
return faceValue;
}
  
public String toString()
{
String result = Integer.toString(faceValue);
  
return result;
}
}

This is the PairOfDice class:

public class PairOfDice {
Die die1, die2;
//Creates two objects of the die class.
public PairOfDice()
{
die1 = new Die();
die2 = new Die();
}
//Initializes two objects of the die class.
  
public int getDie1Value()
{
return die1.getFaceValue();
}
//A method to get the FaceValue of the first die object.
  
public int getDie2Value()
{
return die2.getFaceValue();
}
//A method to get the FaceValue of the second die object.
  
public void setDie1Value(int v1)
{
die1.setFaceValue(v1);
}
//A method to set the FaceValue of the first die object.
  
public void setDie2Value(int v2)
{
die2.setFaceValue(v2);
  
}
//A method to set the FaceValue of the second die object.
  
public void rollTheDice()
{
die1.roll();
die2.roll();
//A method to roll both of the dice.
}
  
public int sumOfTheDice()
{
return getDie1Value() + getDie2Value();
}
//A method to print out the sum of the two dice's values.
}

Thank you

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class Pig {
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       boolean play1 = true;

       boolean play2 = true;
       int totalScore1 = 0;
       int totalScore2 = 0;

       int turnScore1 = 0;
       int turnScore2 = 0;
       int die;
       final int win = 100;
       Scanner scanIn = new Scanner(System.in);
       System.out.println("Hello, welcome to Pig!");
       System.out.println("Player 1, what is your name?");
       String player1 = scanIn.nextLine();
       System.out.println("Player 2, what is your name?");
       String player2 = scanIn.nextLine();
       System.out.println("" + player1 + ", it is your turn. Type yes/no to roll");
       String play = scanIn.nextLine();
       while (totalScore1 < 100 || totalScore2 < 100) {
           while (play1 == true) {
               System.out.println("" + player1 + ", it is your turn");
               System.out.println("Score: " + totalScore1 + "-" + totalScore2 + "");
               int roll = (int) (Math.random() * 6) + 1;
               if (roll == 1) {
                   System.out.println("You rolled a " + roll + "");
                   System.out.println("You get no points");
                   turnScore1 = 0;
                   play1 = false;
                   play2 = true;
                   if (totalScore1 >= 100) {
                       System.out.println("Congratulations " + player1 + " you won!");
                       System.exit(0);
                   }
               }

               else {
                   System.out.println("You rolled a " + roll + "");
                   System.out.println("You have " + roll + " points");
                   totalScore1 += turnScore1 + roll;

                   System.out.println("Would you like to roll again? yes/no");
                   String roll_again = scanIn.nextLine();
                   if (roll_again.equals("no")) {
                       turnScore1 += roll;
                       play1 = false;
                       play2 = true;
                   }
               }

           }
           while (play2 == true) {
               System.out.println("" + player2 + ", it is your turn");
               System.out.println("Score: " + totalScore1 + " and " + totalScore2 + "");
               int roll2 = (int) (Math.random() * 6) + 1;
               if (roll2 == 1) {
                   System.out.println("You rolled a " + roll2 + "");
                   System.out.println("You get no points");
                   turnScore2 = 0;
                   play2 = false;
                   play1 = true;
                   if (totalScore2 >= 100) {
                       System.out.println("Congratulations " + player2 + " you won!");
                       System.exit(0);
                   }
               } else {
                   System.out.println("You rolled a " + roll2 + "");
                   System.out.println("You have " + roll2 + " points");
                   totalScore2 += turnScore2 + roll2;
                   System.out.println("Would you like to roll again? yes/no");
                   String roll_again = scanIn.nextLine();
                   if (roll_again.equals("no")) {
                       turnScore2 += roll2;
                       play2 = false;
                       play1 = true;
                   }
               }
           }
       }
   }
}

Screenshot of Output:

eclipse-workspace-Chegg/src/Pigjava-Eclipse Eile Edit Source Refactor Navigate Search Projec Bun Window Help Quick Access Problems@ JavadocDeaioConsole Progress Pig [Java Application]C:\Program Files J ayre18017hbin yava ee (31-lan-2019404 10A Hello, welcone to Pig! Player 1, what is your nane? BRIKSH Player 2, what is your name? BRİKSH, it is your turn. Type yes/no to roll BRIKSH, İt is your turn Score: 0-0 You rolled a 3 You have 3 points Nould you like to roll again? yes/no DS, it is your turn Score: 3 and 0 You rolled a 2 You have 2 points Nould you like to roll again? yes/no DS, it is your turn Score: 3 and 2 You rolled a 4 You have 4 points Nould you like to roll again? yes/no DS, it is your turn Score: 3 and 6 You rolled a 3 You have 3 points Nould you like to roll again? yes/no BRIKSH, it is your turn Score: 3-9 You rolled a 5 You have 5 points Nould you like to roll again? yes/no OType here to search ^回倨0xENG 04:05 31-01-2019

Add a comment
Know the answer?
Add Answer to:
Hello, Could you please help me code this program in Java? It is important that all...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using the Die class defined in this chapter, design and implement a class called PairOfDice, composed...

    Using the Die class defined in this chapter, design and implement a class called PairOfDice, composed of two Die objects. Include methods to set and get the individual die values, a method to roll the dice, and a method that returns the current sum of the two die values. Create a driver class called RollingDice2 to instantiate and use a PairOfDice object. I need the main method or test class public class Die { private final int MAX = 6;...

  • I need help with these Java programming assignements. public class Die { //here you declare your...

    I need help with these Java programming assignements. public class Die { //here you declare your attributes private int faceValue; //operations //constructor - public Die() { //body of constructor faceValue=(int)(Math.random()*6)+1;//instead of 1, use random approach to generate it } //roll operation public void roll() { faceValue=(int)(Math.random()*6)+1; }    //add a getter method public int getFaceValue() { return faceValue; }    //add a setter method public void setFaceValue(int value) { faceValue=value; } //add a toString() method public String toString() { String...

  • Please Help! I need to update the code below to meet these criteria! 1. The constructor...

    Please Help! I need to update the code below to meet these criteria! 1. The constructor of die class initializes face of Die to 3 2. roll method updates face of die to value from 1-6 3. getFace() method provides current face to the main program 4. main create a Die object 5. main create a Die object 6. main rolls die 5 times 7. main computes sum of rolls 8. main computes actual average of the rolls <--------- Code...

  • Create Junit tests for the following classes public class Dice {    private Die die1;    private Die die2;    public Dice()    {        this.die1 = new Die();        this.die2 = new Die();    }    pu...

    Create Junit tests for the following classes public class Dice {    private Die die1;    private Die die2;    public Dice()    {        this.die1 = new Die();        this.die2 = new Die();    }    public Dice(int[] programmedRolls)    {        int[] programmableroll = programmedRolls;        this.die1 = new Die(programmableroll);        this.die1 = new Die(programmableroll);           }       public Dice(Die die1, Die die2)    {        this.die1 = die1;        this.die2 = die2;    }       public void roll() {        die1.roll();        die2.roll();    }       public int getDie1Value() {        return die1.getLastRoll();    }       public int getDie2Value() {        return die2.getLastRoll();    }      ...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

  • please write the following program in Java PIG is one of a family of games called...

    please write the following program in Java PIG is one of a family of games called jeopardy dice games, since a player's main decision after each roll is whether to jeopardize previous gains by trying for potentially even greater gains. In PIG, all players start with ZERO points, and each turn involves rolling a die to earn points. The first player to earn 100 points or more total shouts "PIG!" and wins the game. On their turn, a player does...

  • hey, struggling with this assignment, wondering if if I could get some help. Here is the...

    hey, struggling with this assignment, wondering if if I could get some help. Here is the project details The following lists a Dice class that simulates rolling a die with a different number of sides. The default is a standard die with six sides. The rollTwoDice function simulates rolling two dice objects and returns the sum of their values. The srand function requires including cstdlib. my class ------------------------------------ class Dice { public: Dice(); DIce(int numSides); virtual int rollDice()const; protected: int...

  • This is for C++ #include <random> #include <iostream> #include <ctime> using namespace std; /* In the...

    This is for C++ #include <random> #include <iostream> #include <ctime> using namespace std; /* In the game of craps, a shooter rolls 2 dice and adds the dots on the upper most faces of the dice. 7 or 11 on the first roll wins, 2, 3, or 12 on the first roll loses, andthing else is call the point and the player rolls again The following program fragment uses 1-way if statements simulate the 1st roll of the dice. Replace...

  • I am currently doing homework for my java class and i am getting an error in...

    I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact {       public static void main(String[] args) {        Random Rand = new Random();        Scanner sc = new Scanner(System.in);        System.out.println("welcome to the contact application");        System.out.println();               int die1;        int die2;        int Total;               String choice = "y";...

  • Requirements: 1. You are not allowed to use global variables. 2. Must declare the following two...

    Requirements: 1. You are not allowed to use global variables. 2. Must declare the following two constants public static final int POINTS = 30; public static final int FORFEIT_POINTS = 20; 3. You can implement your own solution but you are required to break down the problem into smaller pieces. 4. You must provide the output for two rounds of game minimum. 5. Since we are using the Random number generator your output will not be exactly like mine. However,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT