1. Rolling Dice (15 points) Many pen-and-paper role-playing games (like Dungeons
ID: 3596016 • Letter: 1
Question
1. Rolling Dice (15 points) Many pen-and-paper role-playing games (like Dungeons and Dragons) use dice to determine damage chances of success, and so forth. These games use polyhedral dice to generate numerical values in specific ranges. For example, an eight-sided die will generate values in the range 1-8, while a "normal" six-sided (cubic) die only generates numbers between 1 and 6. These dice are often represented using the following shorthand ndx, where n represents the number of dice to roll and dx represents the type of die to be rolled For example, "2d6" means "roll two six-sided dice", while "1d12" means "roll one twelve-sided die". If you roll more than one die at a time, the values are added together to get the final result (so, for example, 2d6 would give you a value in the range 2-12). You may assume that n and x are always positive. There is no strict upper limit on the values of n and x, so you might encounter the (admittedly unlikely) input "204d130", which means "roll 204 130-sided dice" (thus producing an integer in the range 204-26520, inclusive) Define a Java method named rollDice() method, which takes a single String argument that represents a value in the format above (e.g., "4d12"). This method parses (breaks up) its argument and uses Math.random() (or Java's Random class) to return a randomly-generated integer value in the proper range. A pseudocode algorithm for this process is given below index index of "d" in the argument s1 substring of argument containing all characters up to (but not including) index s2 substring of argument containing all characters from (index + 1) to the end num integer value of sl size integer value of s2 roll 0 for 1 through num val-random int in the range 1 through size roll-roll + val return roll You may assume that the input to this method is always correctly formed (one integer value, followed immediately by a single lowercase 'd', followed immediately by a second integer value, with no other letters, punctuation, or whitespace) Finally, write a small Java program that reads in a string representing a set of dice to roll. Your program should use your rollDice() method to determine and print the outcome of the rollExplanation / Answer
package logics;
import java.util.Scanner;
public class simple {
static int diceValue;
static int sidesValue;
static String input;
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
System.out.println("enter the input string");
input = keyboard.nextLine();
simple s = new simple();
s.rollDice(input);
}
@SuppressWarnings("null")
public void rollDice(String s)
{
String[] x = s.split("d",2);
int diceValues[] = new int[1000];
diceValue = Integer.parseInt(x[0]);
sidesValue = Integer.parseInt(x[1]);
//System.out.println("No.of dice:"+diceValue);
//System.out.println("No.of sides:"+sidesValue);
for(int i=0;i<diceValue;i++)
{
diceValues[i] = (int)((Math.random()*sidesValue)+1);
}
for(int j=0;j<diceValue;j++)
System.out.println("dice "+j+" value:"+diceValues[j]);
}
}
----------------------------------------------------------------------------------------------------
Input : 3d4
output :
dice 1 value-->3
dice 2 value-->1
dice 3 value-->0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.