Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Defined a class named Die and provide necessary data fields (Members) and method

ID: 3887126 • Letter: D

Question

Defined a class named Die and provide necessary data fields (Members) and methods, make sure all your classes overwrites toString method, then write 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. Using the PairOfDice class you wrote, 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 1s in one turn, the player loses all the 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.

Explanation / Answer

package pigGame;

import java.util.Scanner;

import java.util.Random;

class Die{

   private int dievalue;

  

   @Override

   public String toString() {

       return " value =" + dievalue ;

   }

   public int getDievalue() {

       return dievalue;

   }

   public void setDievalue(int dievalue) {

       this.dievalue = dievalue;

   }

   public Die() {

       super();

       Random rand = new Random();

       this.dievalue = rand.nextInt(5) + 1;

   }

  

}

class PairOfDice{

   private Die die1;

   private Die die2;

  

   @Override

   public String toString() {

       return " die1" + die1.toString() + ", die2" + die2.toString() ;

   }

   public Die getDie1() {

       return die1;

   }

   public void setDie1(Die die1) {

       this.die1 = die1;

   }

   public Die getDie2() {

       return die2;

   }

   public void setDie2(Die die2) {

       this.die2 = die2;

   }

   //roll Dice

   public void rollDice(){

       this.die1 = null;

       this.die2 = null;

       this.die1 = new Die();

       this.die2 = new Die();

   }

   //sum of Dice

   public int sumOfDice(){

       return this.die1.getDievalue() + this.die2.getDievalue();

   }

}

class User{

   private int point;

   public User(int point) {

       super();

       this.point = point;

   }

   @Override

   public String toString() {

       return "points = " + point ;

   }

   public int getPoint() {

       return point;

   }

   public void setPoint(int point) {

       this.point = point;

   }

  

}

public class Pig {

   private User user,comp;

   private int i;

   private PairOfDice dices;

  

   public Pig() {

       user = new User(0);

       comp = new User(0);

       i = 0;

       dices = new PairOfDice();

   }

  

  

  

   @Override

   public String toString() {

       return "Your " + user.toString() + ", Computer " + comp.toString() ;

   }

   //function to play a game

   public void turn(boolean skip){

       i++;

       if(skip)

           return;

       dices.rollDice();

       System.out.println(dices.toString());

       if (i%2 != 0){

      

           if (dices.getDie1().getDievalue() == 1 && dices.getDie2().getDievalue() == 1){

               this.user.setPoint(0);

               return;

           }

           else if (dices.getDie1().getDievalue() == 1 || dices.getDie2().getDievalue() == 1){

               return;

           }

           else {

               this.user.setPoint(this.user.getPoint() +dices.sumOfDice());

           }

          

       }else {

          

           if (dices.getDie1().getDievalue() == 1 && dices.getDie2().getDievalue() == 1){

               this.comp.setPoint(0);

               return;

           }

           else if (dices.getDie1().getDievalue() == 1 || dices.getDie2().getDievalue() == 1){

               return;

           }

           else {

               this.comp.setPoint(this.comp.getPoint() + dices.sumOfDice());

           }

       }

   }

  

   public static void main(String[] args) {

       Pig play = new Pig();

       int x = 0;

      

       while(play.user.getPoint() < 100 && play.comp.getPoint() < 100){

           Scanner sc=new Scanner(System.in);

           System.out.println("Please enter you choice type 1 for roll Dice or other for skip :");

           int val;

           try {

           val = sc.nextInt();

           }

           catch(Exception e){

               System.out.println("You enter Invalid number please enter a valid number");

               continue;

           }

           if (val == 1)

               play.turn(false);

           else

               play.turn(true);

           if( play.comp.getPoint() > x+19){

               x += play.comp.getPoint();

               play.turn(true);

           }

           else{

               play.turn(false);

           }

           System.out.println(play.toString());

       }

       if (play.comp.getPoint() < 100){

           System.out.println("Congratulation, You Won");

       }

       else if(play.user.getPoint() < 100){

           System.out.println("Sorry, You Lose");

       }

       else {

           System.out.println("Game Draw");

       }

   }

Sample OUTPUT :

Please enter you choice type 1 for roll Dice or other for skip :

a

You enter Invalid number please enter a valid number

Please enter you choice type 1 for roll Dice or other for skip :

1

die1 value =3, die2 value =5

die1 value =2, die2 value =5

Your points = 8, Computer points = 7

Please enter you choice type 1 for roll Dice or other for skip :

2

die1 value =3, die2 value =2

Your points = 8, Computer points = 12

Please enter you choice type 1 for roll Dice or other for skip :

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote