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

Java Setup: Create a class (.java) file called PlayThrees.java (This can be with

ID: 3667277 • Letter: J

Question

Java

Setup:

Create a class (.java) file called PlayThrees.java

(This can be within a project that contains the other parts of this homework or it can be in separate project on its own)

Features / Requirements:

Requirements

you'll need a way to store multiple die rolls; we don't know Arrays yet (the sort of equivalent of lists in Python)… so you'll probably make heavy use of Strings and chars… as well as methods in the Character class.

in particular, there's a method not mentioned in the book called:

you'll need it to convert characters to the int value that they represent (rather than the unicode code point)

check out the documentation for more details - https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#getNumericValue(char)

Features:

roll 5 6-sided dice

print out the results of each die roll, with each die numbered from 1 through 5

use the following unicode literals for the die faces, and put the actual value in parentheses:

u2680 (1)

u2681 (2)

u2682 (3)

u2683 (4)

u2684 (5)

u2685 (6)

ask the user to enter the numbers of the rolls that they would like to pin for their score

they have to choose at least 1 die roll

they can pin more than 1 by typing in each roll number they'd like to pin

for example, typing in 34 pins rolls 3 and 4 (which correspond to the values 4 and 1)

display the pinned dice Pinned: (34)

if there's only one die rolled, automatically pin that die for the user

add the roll do your collection of pinned dice

print out a message that says: Automatically pin last die

roll the remainder of the dice… and continue the same process until all dice are pinned

once all of the dice are pinned, print out the player's score

Example Output

(Pinning multiple dice, automatically pin roll if only 1 die)

Enter numbers of dice to pin... for example, to pin the die 1 and 2, enter 12):

Die Roll 1 - (1)

Die Roll 2 - (4)

Die Roll 3 - (5)

Die Roll 4 - (1)

Die Roll 5 - (1)

> 145

Pinned: (111)

Enter numbers of dice to pin... for example, to pin the die 1 and 2, enter 12):

Die Roll 1 - (1)

Die Roll 2 - (2)

> 1

Pinned: (1111)

Enter numbers of dice to pin... for example, to pin the die 1 and 2, enter 12):

Die Roll 1 - (4)

Automatically pin last die

Pinned: (11114)

Your score is 8!

Explanation / Answer

See Code below:

-----------------------------------------

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
* Class PlayThree
*
*/
public class PlayThree {

   // function to roll a dice
   public static int rollDice() {
       int min = 1;
       int max = 6;
       return ((int) (Math.random() * (max - min))) + min;
   }

   // function to get the face of a rolled dice
   public static char getRolledFace(int diceValue) {
       char rolledFace = 0;
       // Die faces
       char faceOne = 'u2680';
       char faceTwo = 'u2681';
       char faceThree = 'u2682';
       char faceFour = 'u2683';
       char faceFive = 'u2684';
       char faceSix = 'u2685';

       switch (diceValue) {
       case 1:
           rolledFace = faceOne;
           break;
       case 2:
           rolledFace = faceTwo;
           break;
       case 3:
           rolledFace = faceThree;
           break;
       case 4:
           rolledFace = faceFour;
           break;
       case 5:
           rolledFace = faceFive;
           break;
       case 6:
           rolledFace = faceSix;
           break;
       }
       return rolledFace;
   }

   /**
   * @param args
   * @throws IOException
   */
   public static void main(String[] args) throws IOException {

       int totalDices = 5; // total no. of dices to roll
       int pinnedDices = 0; // no. of pinned dices
       int dicesToRoll = totalDices - pinnedDices; // remaining dices to roll
       String dicesToPin = ""; // which dices to pin

       // rolled dice values
       int dice1 = 0, dice2 = 0, dice3 = 0, dice4 = 0, dice5 = 0;

       // rolled dice faces
       char faceDice1 = 0, faceDice2 = 0, faceDice3 = 0, faceDice4 = 0, faceDice5 = 0;

       // message string for pinned dices
       String pinnedDicesMsg = "";
       String pinnedDicesValues = "";

       // score of pinned dices
       int score = 0;

       System.out.println("(Pinning multiple dice, automatically pin roll if only 1 die)");
       // loop to roll and pin dices
       while (pinnedDices < totalDices) {
           System.out.println("Enter numbers of dice to pin... for example, to pin the die 1 and 2, enter 12):");
           int rolledDice;
           for (int i = 1; i <= dicesToRoll; i++) {
               rolledDice = rollDice();
               // set dice values
               switch (i) {
               case 1:
                   dice1 = rolledDice;
                   faceDice1 = getRolledFace(dice1);
                   System.out.println("Die Roll " + i + " - " + faceDice1 + "(" + dice1 + ")");
                   break;
               case 2:
                   dice2 = rolledDice;
                   faceDice2 = getRolledFace(dice2);
                   System.out.println("Die Roll " + i + " - " + faceDice2 + "(" + dice2 + ")");
                   break;
               case 3:
                   dice3 = rolledDice;
                   faceDice3 = getRolledFace(dice3);
                   System.out.println("Die Roll " + i + " - " + faceDice3 + "(" + dice3 + ")");
                   break;
               case 4:
                   dice4 = rolledDice;
                   faceDice4 = getRolledFace(dice4);
                   System.out.println("Die Roll " + i + " - " + faceDice4 + "(" + dice4 + ")");
                   break;
               case 5:
                   dice5 = rolledDice;
                   faceDice5 = getRolledFace(dice5);
                   System.out.println("Die Roll " + i + " - " + faceDice5 + "(" + dice5 + ")");
                   break;
               }
           }
           if (dicesToRoll == 1) {
               System.out.println("Automatically pin last die...");
           } else {
               System.out.print(">");
               BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
               dicesToPin = br.readLine();
           }

           // no. of pinned dices
           pinnedDices = pinnedDices + dicesToPin.length();

           // no. of remaining dices to roll
           dicesToRoll = totalDices - pinnedDices;

           // pinning of dices
           for (int j = 0; j < dicesToPin.length(); j++) {
               char ch = dicesToPin.charAt(j);
               int pinnedDice = Character.getNumericValue(ch);
               switch (pinnedDice) {
               case 1:
                   pinnedDicesMsg += faceDice1;
                   pinnedDicesValues += Integer.toString(dice1);
                   score += dice1;
                   break;
               case 2:
                   pinnedDicesMsg += faceDice2;
                   pinnedDicesValues += Integer.toString(dice2);
                   score += dice2;
                   break;
               case 3:
                   pinnedDicesMsg += faceDice3;
                   pinnedDicesValues += Integer.toString(dice3);
                   score += dice3;
                   break;
               case 4:
                   pinnedDicesMsg += faceDice4;
                   pinnedDicesValues += Integer.toString(dice4);
                   score += dice4;
                   break;
               case 5:
                   pinnedDicesMsg += faceDice5;
                   pinnedDicesValues += Integer.toString(dice5);
                   score += dice5;
                   break;
               }
           }
           System.out.println("Pinned:" + pinnedDicesMsg + "(" + pinnedDicesValues + ")");
       }
       System.out.println("Your score is " + score + "!");
   }

}

--------------------------------------

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