Java Features / Requirements: Requirements you\'ll need a way to store multiple
ID: 3667726 • Letter: J
Question
Java
Features / Requirements:
Requirements
you'll need a way to store multiple die rolls; we don't know Arrays yet (the sort ofequivalent 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!
(Another run, with a better score… note that 3's are counted as 0's!)
Enter numbers of dice to pin... for example, to pin the die 1 and 2, enter 12):
Die Roll 1 - (3)
Die Roll 2 - (6)
Die Roll 3 - (1)
Die Roll 4 - (4)
Die Roll 5 - (3)
> 15
Pinned: (33)
Enter numbers of dice to pin... for example, to pin the die 1 and 2, enter 12):
Die Roll 1 - (5)
Die Roll 2 - (4)
Die Roll 3 - (4)
> 2
Pinned: (334)
Enter numbers of dice to pin... for example, to pin the die 1 and 2, enter 12):
Die Roll 1 - (3)
Die Roll 2 - (4)
> 1
Pinned: (3343)
Enter numbers of dice to pin... for example, to pin the die 1 and 2, enter 12):
Die Roll 1 - (2)
Automatically pin last die Pinned: (33432) Your score is 6!
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class DieRolls {
public static Scanner in = new Scanner(System.in);
public static Random random = new Random();
public static char getUnicodeChar(int num) {
if (num == 1) {
return 'u2680';
} else if (num == 2) {
return 'u2681';
} else if (num == 3) {
return 'u2682';
} else if (num == 4) {
return 'u2683';
} else if (num == 5) {
return 'u2684';
} else {
return 'u2685';
}
}
public static String getRollResult(int n) {
String ret = "";
for (int i = 0; i < n; ++i) {
ret += random.nextInt(6) + 1;
}
return ret;
}
public static void printDice(String str) {
for (int i = 0; i < str.length(); ++i) {
System.out.println("Die Roll " + (i + 1) + " - " + getUnicodeChar(Character.getNumericValue(str.charAt(i)))
+ " (" + (Character.getNumericValue(str.charAt(i))) + ")");
}
}
public static int getInputNumericValue(String input, int i) {
return Character.getNumericValue(input.charAt(i));
}
public static String getResultString(String rolls, String input) {
String ret = "";
for (int i = 0; i < input.length(); ++i) {
ret += rolls.charAt(getInputNumericValue(input, i) - 1);
}
return ret;
}
public static int getScore(String rolls) {
int total = 0;
for (int i = 0; i < rolls.length(); ++i) {
total += Character.getNumericValue(rolls.charAt(i));
}
return total;
}
public static void printChoice(String rolls) {
System.out.println("Pinned: ");
for (int i = 0; i < rolls.length(); ++i) {
System.out.print(getUnicodeChar(Character.getNumericValue(rolls.charAt(i))) + " ");
}
System.out.print("(");
for (int i = 0; i < rolls.length(); ++i) {
System.out.print(rolls.charAt(i));
}
System.out.println(")");
}
public static void main(String args[]) {
int n = 5;
String rolls, input, result = "";
int score = 0;
while (n > 0) {
System.out.println("Enter numbers of dice to pin... for example, to pin the die 1 and 2, enter 12):");
rolls = getRollResult(n);
printDice(rolls);
if (n == 1) {
input = "1";
System.out.println("Automatically pin last die");
} else {
input = in.next();
}
result += getResultString(rolls, input);
printChoice(result);
n -= input.length();
}
System.out.println("Your score is " + getScore(result) + "!");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.