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

IN JAVA LANGUAGE PLEASE! Problem: Write a program to play the pig game against t

ID: 3605866 • Letter: I

Question

IN JAVA LANGUAGE PLEASE!

Problem:

Write a program to play the pig game against the computer. At each turn, the current player will roll a pair of dice and accumulates points. The goal is to reach to 100 or more points before your opponent does. (For the testing purposes use 30 instead of 100 points) If, on any turn, the player rolls a 1, all the points accumulated for that round are forfeited and the 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 are forfeited and the control moves to the other player. The player may voluntarily turn over the control of the dice after each roll. Therefore player must decide to roll again (be a pig) and risk losing points, or relinquish control of the dice, possibly allowing the other player to win. Computer is going to flip a coin to choose the first player

Here is the list of the tasks that needs to be done

Describe the game by writing a method.

Data validation: A method that accepts a Scanner object as its parameter, prompt the user to

enter “yes” or “no”. as long as the user is not entering a valid input , prompt the user again

Flip the coin: This method accepts a Random object and returns” head” or “tail” based on the

random number that was generated

Roll two dices: this method accepts a Random object. Generates two random number

representing one of the numbers on a dice. Returns the sum of the dices.

Choose a name for the computer: Come up with 10 different name for the computer. Then

select a random name from the list that you created. Return the selected name.

Play: this method calls the other methods to play the game

Declare all the needed variables to keep track of the scores for each player, and Boolean variables to indicate who is playing at the moment.

Ask the user’s name

Decide who start the game first by calling one of the methods you created to flip the

coin.

Write conditional statements to switch the game between the computer and the player

based on the dice rolled and overall points. Read the output and the program description to figure out the conditions. You need to use couple while loops: one loop for the human player, one loop for the computer player. The previous two loops will be nested in another while loop to witch the game between the two players.
//declaring your variables
While (there is no winner)
{

//some codes
While (human is playing)

{

//some codes, conditional statements }

//you may need some codes While (computer is playing)

{
//some codes, conditional statements

}
//you may need some codes }

e. Keep playing the game until the player or the computer has 100 or more points (40 or more for the simplicity). You are not allowed to hard code any numbers.

7. Main method: Calls the method play, keep playing the game as long as there are more players.

SAMPLE OUTPUT:

Explanation / Answer

********************************************************************************************************************************

Code:

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Dice {
   private int die1;
   private int die2;
   private int points = 0;

   public int rollDice(Random myRan) {
       die1 = myRan.nextInt(6) + 1;
       die2 = myRan.nextInt(6) + 1;
       return die1 + die2;
   }

   public int getPoints() {
       return points;
   }

   public void resetPoints() {
       points = 0;
   }

   public void addPoints(int p) {
       points = points + p;
   }

   // accessor method
   public int getDie1() {
       return die1;
   }

   // accessor method
   public int getDie2() {
       return die2;
   }

   public static void describeGame() {
       System.out.println("*********************************************************************************** "
               + "*       You are about to play the pig game against the computer.                  * "
               + "*       On each turn, the current player will roll a pair of dice                 * "
               + "*       and acumulates points. The goal is to reach to40 points                  * "
               + "*       before your opponent does. If, on any turn, the player roll               * "
               + "*       all the points accumulated for that round are forfeited and               * "
               + "*       the 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 and forfeit and the control moves to the            * "
               + "*       other player. The player may voluntarily turn over the dice after         * "
               + "*       each roll. Therefore player must decide to roll again(be a pig)           * "
               + "*       and risk losing points , or relinquish control of the dice, possibly      * "
               + "*       allowing the other player to win.                                         * "
               + "*       Computer is going to flip a coin to choose the first player              * "
               + "***********************************************************************************");
   }

   public static String userInput(Scanner sc) {
       String input;
       do {
           input = sc.next();
       } while (!input.equals("yes") && !input.equals("no"));
       return input;
   }

   public static String toss(Random random) {
       int t = random.nextInt(2);
       if (t == 0)
           return "heads";
       else
           return "tails";
   }

   public static String computerName() {
       List<String> names = new ArrayList<String>();
       names.add("Sophia");
       names.add("Isabella");
       names.add("Emma");
       names.add("Olivia");
       names.add("Ava");
       names.add("Jacob");
       names.add("Mason");
       names.add("William");
       names.add("Jayden");
       names.add("Noah");
       Random random = new Random();
       return names.get(random.nextInt(10));
   }


   public static void main(String[] args) throws IOException {
       while (true) {
           boolean isWinner = false;
           boolean isComputerPlaying = false;
           describeGame();
           String computerName = computerName();
           System.out.println("Hi my name is " + computerName);
           System.out.print("What is your name? ");
           Scanner sc = new Scanner(System.in);
           String playerName = sc.next();
           System.out.println("Hi Mary, I am fliping the coin to determine who goes first");
           System.out.println("press any key to start the game.");
           // System.in.read();
           String toss = toss(new Random());
           if (toss.equals("heads")) {
               isComputerPlaying = true;
               System.out.println(computerName + " is going to start the game");
           } else {
               isComputerPlaying = false;
               System.out.println(playerName + " is going to start the game");
           }
           Dice computer = new Dice();
           Dice player = new Dice();
           while (true) {
               while (isComputerPlaying) {
                   boolean pass = false;
                   System.out.println(computerName + "'s turn:");
                   System.out.println("Points: " + computer.points);
                   computer.rollDice(new Random());
                   System.out.println("Die 1:" + computer.die1);
                   System.out.println("Die 2:" + computer.die2);
                   if (computer.die1 == 1 && computer.die2 == 1) {
                       computer.resetPoints();
                       System.out.println("Sorry" + computerName + "you lost all your points");
                       pass = true;
                   } else if (computer.die1 == 1 || computer.die2 == 1) {
                       System.out.println("Sorry " + computerName + " you lost the points for this turn");
                       pass = true;
                   } else {
                       computer.addPoints(computer.die1 + computer.die2);
                   }
                   System.out.println("Points: " + computer.points);
                   if (computer.points >= 40)
                       break;
                   if (computer.points >= 20) {
                       System.out.println("I am forfeiting my turn since I have " + computer.points
                               + " which is more than twenty points.");
                       pass = true;
                   }
                   System.out.println("Press any key to continue");

                   if (pass == true) {
                       isComputerPlaying = !isComputerPlaying;
                       break;
                   }
               }
               if (computer.points >= 40)
                   break;
               while (!isComputerPlaying) {
                   boolean pass = false;
                   System.out.println(playerName + "'s turn:");
                   System.out.println("Points: " + player.points);
                   player.rollDice(new Random());
                   System.out.println("Die 1:" + player.die1);
                   System.out.println("Die 2:" + player.die2);
                   if (player.die1 == 1 && player.die2 == 1) {
                       player.resetPoints();
                       System.out.println("Sorry" + playerName + "you lost all your points");
                       pass = true;
                   } else if (player.die1 == 1 || player.die2 == 1) {
                       System.out.println("Sorry " + playerName + " you lost the points for this turn");
                       pass = true;
                   } else {
                       player.addPoints(player.die1 + player.die2);
                   }
                   System.out.println("Points: " + player.points);
                   if (player.points >= 40)
                       break;
                   if (player.points >= 20 && pass == false) {
                       System.out.print("Do you want to forfeit your turn since you have 20 or more points?");
                       String choice = userInput(new Scanner(System.in));
                       if (choice.equals("yes"))
                           pass = true;
                   }
                   System.out.println("Press any key to continue");

                   if (pass == true) {
                       isComputerPlaying = !isComputerPlaying;
                       break;
                   }
               }
               if (player.points >= 40)
                   break;
           }
           System.out.println("Hurray!!! We have a winner " + "Somebody got 40 or more");
           System.out.println(computerName + " points " + computer.points);
           System.out.println(playerName + " points " + player.points);
           if (computer.points > player.points)
               System.out.println(computerName + " won the game");
           else
               System.out.println(playerName + " won the game");
           System.out.print("Is there another player?");
           String more = userInput(sc);
           if (more.equals("no"))
               break;
       }
   }
}

*****************************************************************************************************************************

Sample Output:

***********************************************************************************
*       You are about to play the pig game against the computer.                  *
*       On each turn, the current player will roll a pair of dice                 *
*       and acumulates points. The goal is to reach to40 points                  *
*       before your opponent does. If, on any turn, the player roll               *
*       all the points accumulated for that round are forfeited and               *
*       the 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 and forfeit and the control moves to the            *
*       other player. The player may voluntarily turn over the dice after         *
*       each roll. Therefore player must decide to roll again(be a pig)           *
*       and risk losing points , or relinquish control of the dice, possibly      *
*       allowing the other player to win.                                         *
*       Computer is going to flip a coin to choose the first player              *
***********************************************************************************
Hi my name is Noah
What is your name? Jon
Hi Mary, I am fliping the coin to determine who goes first
press any key to start the game.
Noah is going to start the game
Noah's turn:
Points: 0
Die 1:2
Die 2:6
Points: 8
Press any key to continue
Noah's turn:
Points: 8
Die 1:1
Die 2:3
Sorry Noah you lost the points for this turn
Points: 8
Press any key to continue
Jon's turn:
Points: 0
Die 1:4
Die 2:3
Points: 7
Press any key to continue
Jon's turn:
Points: 7
Die 1:3
Die 2:3
Points: 13
Press any key to continue
Jon's turn:
Points: 13
Die 1:2
Die 2:3
Points: 18
Press any key to continue
Jon's turn:
Points: 18
Die 1:5
Die 2:5
Points: 28
Do you want to forfeit your turn since you have 20 or more points?no
Press any key to continue
Jon's turn:
Points: 28
Die 1:5
Die 2:5
Points: 38
Do you want to forfeit your turn since you have 20 or more points?yes
Press any key to continue
Noah's turn:
Points: 8
Die 1:5
Die 2:4
Points: 17
Press any key to continue
Noah's turn:
Points: 17
Die 1:5
Die 2:1
Sorry Noah you lost the points for this turn
Points: 17
Press any key to continue
Jon's turn:
Points: 38
Die 1:2
Die 2:2
Points: 42
Hurray!!! We have a winner
Somebody got 40 or more
Noah points 17
Jon points 42
Jon won the game
Is there another player?yes
***********************************************************************************
*       You are about to play the pig game against the computer.                  *
*       On each turn, the current player will roll a pair of dice                 *
*       and acumulates points. The goal is to reach to40 points                  *
*       before your opponent does. If, on any turn, the player roll               *
*       all the points accumulated for that round are forfeited and               *
*       the 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 and forfeit and the control moves to the            *
*       other player. The player may voluntarily turn over the dice after         *
*       each roll. Therefore player must decide to roll again(be a pig)           *
*       and risk losing points , or relinquish control of the dice, possibly      *
*       allowing the other player to win.                                         *
*       Computer is going to flip a coin to choose the first player              *
***********************************************************************************
Hi my name is Sophia
What is your name? King
Hi Mary, I am fliping the coin to determine who goes first
press any key to start the game.
King is going to start the game
King's turn:
Points: 0
Die 1:6
Die 2:4
Points: 10
Press any key to continue
King's turn:
Points: 10
Die 1:2
Die 2:1
Sorry King you lost the points for this turn
Points: 10
Press any key to continue
Sophia's turn:
Points: 0
Die 1:2
Die 2:5
Points: 7
Press any key to continue
Sophia's turn:
Points: 7
Die 1:1
Die 2:1
SorrySophiayou lost all your points
Points: 0
Press any key to continue
King's turn:
Points: 10
Die 1:5
Die 2:3
Points: 18
Press any key to continue
King's turn:
Points: 18
Die 1:2
Die 2:5
Points: 25
Do you want to forfeit your turn since you have 20 or more points?no
Press any key to continue
King's turn:
Points: 25
Die 1:2
Die 2:2
Points: 29
Do you want to forfeit your turn since you have 20 or more points?yes
Press any key to continue
Sophia's turn:
Points: 0
Die 1:2
Die 2:3
Points: 5
Press any key to continue
Sophia's turn:
Points: 5
Die 1:1
Die 2:2
Sorry Sophia you lost the points for this turn
Points: 5
Press any key to continue
King's turn:
Points: 29
Die 1:3
Die 2:4
Points: 36
Do you want to forfeit your turn since you have 20 or more points?no
Press any key to continue
King's turn:
Points: 36
Die 1:6
Die 2:1
Sorry King you lost the points for this turn
Points: 36
Press any key to continue
Sophia's turn:
Points: 5
Die 1:5
Die 2:2
Points: 12
Press any key to continue
Sophia's turn:
Points: 12
Die 1:4
Die 2:2
Points: 18
Press any key to continue
Sophia's turn:
Points: 18
Die 1:4
Die 2:4
Points: 26
I am forfeiting my turn since I have 26 which is more than twenty points.
Press any key to continue
King's turn:
Points: 36
Die 1:3
Die 2:4
Points: 43
Hurray!!! We have a winner
Somebody got 40 or more
Sophia points 26
King points 43
King won the game
Is there another player?no


*****************************************************************************************************************************

I hope this helps you.
If you find my answer helpful,
Kindly rate the answer.
All the best :)