-2

Could you please help me find a way to add these values? I'm trying to make a dice thrower to play Dungeons and Dragons. Thanks for you help!

System.out.println("What kind of dice?");
    
    int  diceType = userInput.nextInt();
    


    System.out.println("How much?");
    
    
    int  diceNumber = userInput.nextInt();
    
    int i = 1;
    while ( i <=diceNumber)
        {
     System.out.println("Value:  " 
              + getRandomInteger(diceType, 1));
            i++;
clod9353
  • 1,942
  • 2
  • 5
  • 20
  • Java and Javascript are completely different languages – greg-449 Jan 20 '21 at 18:58
  • You could ... define a variable outside the loop, initialize it zero, and add to it inside the loop. You can use an expression like `a = a + diceValue`, or the equivalent `a += diceValue`. – Andy Thomas Jan 20 '21 at 18:58
  • How can i name a random integer? right now they are just random integers, so i can't add it to a variable – Thomaeasz Jan 20 '21 at 19:05
  • Create a variable, ``int sum = 0;`` (outside the loop), and just add the random value. You can use ``Random#nextInt(diceType) + 1`` to get a random value from 1 to diceType. – NomadMaker Jan 20 '21 at 20:39
  • Remember that a function call such as dice.getDie1() represents a value, so it can be used anyplace where a literal number or variable could be used. There is no requirement that you assign the value returned by the function to a variable. You can use it directly. – Lakshay Sharma Jan 20 '21 at 20:42

1 Answers1

1

Problem #1: Create a data structure for the die.

This die object should store AT LEAST the number of sides and the current face value. You can implicitly assume that each side has a unique value ranging from 1 to NUM_SIDES. You could create dice with different number of sides, or you could mutate the same dice collection to alter the value of NUM_SIDES. Regardless, the face values will still range from 1 to NUM_SIDES.

public class Die {
   private int currentValue;
   private int numberOfSides;

   public int getCurrentValue() { return currentValue;}
   public int roll() {currentValue = getRandomInteger();}

}

Problem #2: Rolling the dice

First of all, store the dice into an array of die (or some other collection that allow duplicates). Your die object should have a roll() function that will change the current value. Your getRandomInteger()should be called from this method and the result of this call should be stored in the currentValue field.

Problem #3: Adding the values

Iterate through the array of die and add get their current values. This means that your Die class should have a getCurrentValue() method. Accumulate the sum of the dice.

Things to consider

  1. Should all die have the same number of sides? I don't think this matters one bit. In fact, I will argue that you could use the same implementation for a game like Yahtzee where the dice are the same number of sides or AD&D where you have dice of different number of sides.
  2. Re-roll Rules. In a game like Yahtzee, you don't always roll the entire collection. You need to consider a strategy where the user could pick SOME of the dice to reroll. I don't remember if this is true for AD&D but if it is, you need to consider how to differentiate between each die and partially accumulate the current values of each roll.
  3. Randomization. For a more realistic randomization, consider seeding your randomizer with something like the current time. Also, you need to set your boundaries for the possible roll values to be between 1 and NUM_SIDES (inclusive).
  4. Setting the number of sides. Assuming the number of sides could change, you will need to add a setter method to set the number of sides. You will also need to pass this parameter via the constructor so that the dice are set to some valid number upon instantiation of a die object.
hfontanez
  • 5,774
  • 2
  • 25
  • 37