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

Hello, PLEASE read the instrcutions and please look out the text file to see how

ID: 3607620 • Letter: H

Question

Hello, PLEASE read the instrcutions and please look out the text file to see how the porgram should be. The requirments and whats needed in the code are shown at the bottom. Please read them.This is a beginner Java class so my knowledge is limited. For loops, if statements, parameters, nested loops, Scanner, return, printf, char.At, isLowerCase, printf, etc. So please nothing too complex, no tokenizer or arrays just yet. Thank you!

Text File:

What to turn in: You must upload your java file and a.txt file of your output to SacCT before midnight on the due date. To get your output into a txt file you can right-click in the output window in jGRASP and click on "Save as Text File" Make sure to clear your output window before you run your program if you are going to be saving your text file. No late HW will be accepted. No email is accepted For the purpose of testing, set the maximum score to 30, otherwise it will take a long time to find the winner of the game. 1. 2. 3. 4. 5. 6. 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. Programs without proper number of the methods will get zero points. 4. Proper naming conventions 7 points 5. Proper indentation 7 points 6. Proper commenting 6 point. You must provide comments for each method and comments at the beginning of your program 7. Working programs with the perfect output: 80 points 8. Codes that don't compile will get zero points 9. Codes that compiles but don't produce the correct output will get 20 points 10. You can implement your own solution but you are required to break down the problem into smaller pieces. 11. You must provide the output for two rounds of game minimum. 12. Since we are using the Random number generator your output will not be exactly like mine. However, all the prompts must be exactly as the provided 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 static final int POINTS = 30;
   public static final int FORFEIT_POINTS = 20;
  
   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 to 30 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              * " +
               " *********************************************************************************** " +
               " lets start the fun");
   }

   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 "+playerName+", I am fliping the coin to determine who goes first");
           System.out.print("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 >= POINTS)
                       break;
                   if (computer.points >= FORFEIT_POINTS) {
                       System.out.println("I am forfeiting my turn since I have " + computer.points
                               + " which is more than "+ FORFEIT_POINTS +" points.");
                       pass = true;
                   }
                   System.out.print("Press any key to continue");
                   System.in.read();

                   if (pass == true) {
                       isComputerPlaying = !isComputerPlaying;
                       break;
                   }
               }
               if (computer.points >= POINTS) {
                   System.out.println("You reached or passed "+ POINTS +" points");
                   System.out.print(" Press any key to continue");
                   System.in.read();
                   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 >= POINTS)
                       break;
                   if (pass == false) {
                       System.out.print("Do you want to forfeit your turn?");
                       String choice = userInput(new Scanner(System.in));
                       if (choice.equals("yes"))
                           pass = true;
                   }
                   System.out.print("Press any key to continue");

                   if (pass == true) {
                       isComputerPlaying = !isComputerPlaying;
                       break;
                   }
               }
               if (player.points >= POINTS) {
                   System.out.println("You reached or passed "+ POINTS +" points");
                   System.out.print(" Press any key to continue");
                   System.in.read();
                   break;
               }
           }
           System.out.println("Hurray!!! We have a winner " + "Somebody got "+ POINTS +" 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 to 30 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              *
***********************************************************************************
lets start the fun
Hi my name is Isabella
What is your name? Eric
Hi Eric, I am fliping the coin to determine who goes first
press any key to start the game.
Isabella is going to start the game

Isabella's turn:
Points: 0
Die 1:6
Die 2:2
Points: 8
Press any key to continue
Isabella's turn:
Points: 8
Die 1:6
Die 2:1
Sorry Isabella you lost the points for this turn
Points: 8
Press any key to continue

Eric's turn:
Points: 0
Die 1:4
Die 2:4
Points: 8
Do you want to forfeit your turn?no
Press any key to continue
Eric's turn:
Points: 8
Die 1:5
Die 2:1
Sorry Eric you lost the points for this turn
Points: 8
Press any key to continue
Isabella's turn:
Points: 8
Die 1:1
Die 2:1
Sorry Isabella you lost all your points
Points: 0
Press any key to continue

Eric's turn:
Points: 8
Die 1:2
Die 2:2
Points: 12
Do you want to forfeit your turn?no
Press any key to continue
Eric's turn:
Points: 12
Die 1:2
Die 2:4
Points: 18
Do you want to forfeit your turn?no
Press any key to continue
Eric's turn:
Points: 18
Die 1:3
Die 2:3
Points: 24
Do you want to forfeit your turn?yes
Press any key to continue
Isabella's turn:
Points: 0
Die 1:2
Die 2:6
Points: 8
Press any key to continue

Isabella's turn:
Points: 8
Die 1:4
Die 2:1
Sorry Isabella you lost the points for this turn
Points: 8
Press any key to continue
Eric's turn:
Points: 24
Die 1:6
Die 2:6
Points: 36
You reached or passed 30 points
Press any key to continue
Hurray!!! We have a winner
Somebody got 30 or more
Isabella points 8
Eric points 36
Eric 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 :)

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