Search code examples
javadice

Creating a Dungeons and Dragons dice roller


I am being tasked to create a Dungeons and Dragons dice rolling program in which four dice are rolled, the highest three numbers are recorded, and the lowest number is put off to the side.

The program needs to use Object Oriented Programming, and that is where I am running into the problem.

Additionally, no nested If statements or loops may be used.

I am trying to figure out how to code each class separately from each other.

I need a class that will run the program itself, one that asks the person using the program how many sets of dice they'd like to roll, a class to actually represent the set of dice rolls, a class to represent the series of dice rolled (this being displaying what Dice 1 rolled, what Dice 2 rolled, etc.), and lastly a class to handle the rolling of a single die.

I know that the output needs to show each set of dice rolled.

The output needs to look like this:

Set 1: 11, 12, 3, 1
Set 2: 12, 3, 1, 4

And so on.

Also, the main thing that is throwing me off when trying to create this program is that I am being required to make it without and nested if statements or loops.


Solution

  • A solution to this problem should be able to be completed using two classes: you main class and a Dice class.

    Your main class should have your main(){} which will contain the code to prompt a user for input and print out dice rolls.

    You likely only needs two methods in your Dice class, that is going to be a constructor and a getter for the value of a die.

    Your dice constructor can set a roll value of a dice object to a random number from 1 to 6 (assuming you are using a D6 for your DnD rolls) when the die is created. This will save you from needing to call another method to actually "roll" the die.

    You can then use the getter for the roll value from your main() to get the roll value. This getter only needs to return the value of the roll.

    You will need a member variable for your Dice class that can hold the random value generated for a new die--I would suggest an int since you will only need integers.

    In your main(){} you can use a scanner to take the user's input for how many dice sets they would like to be rolled. Based on their input, you could use a for loop where the condition is checked against the user's input. (e.g. for(int i = 0 ; i <userInput ; i ++) ). The loop will iterate once for each set of die the user wants to roll.

    Inside this loop you could create four Dice objects with each iteration of the loop. This will effectively give you four "rolls" since each die is instantiated with a random number.

    Assuming you give the member variable that will hold a die's value in your 'Dice' class a name like roll, you could access the die's value through dot notation like this dice1.roll() This can be used in your output as well rather than needing to assign the value returned to another variable.

    Finally, to check which roll is the lowest, you can use a series of if - else statements to check which roll is the lowest. I would suggest using a condition like the following

    if(dice1.roll() < dice2.roll() && dice1.roll() < dice3.roll() && dice1.roll() < dice3.roll()){ //print your dice rolls. If the condition is true, we know that dice1 had the //lowest roll }

    You can then use an else to check if dice2 is the lowest, another else if to check for dice3 and another to check for dice4.

    In sudo code your main class might look something like this

    \\main
    
    \\declare a scanner
    \\declare int for user input
    
    \\output: How many dice would you like to roll
    \\input user input var is assigned the value of user input
    
    
    \\for loop checking if i < userInput
       \\Create new dice object dice1
       \\Create new dice object dice2
       \\Create new dice object dice3
       \\Create new dice object dice4
    
       \\check if dice1 is lowest
           \\print out die rolls, totals, and lowest roll
       \\else if check dice2 is lowest
           \\print out die rolls, totals, and lowest roll
       \\else if check dice3 is lowest
           \\print out die rolls, totals, and lowest roll
       \\else if check dice4 is lowest
           \\print out die rolls, totals, and lowest roll
    
    \\end for loop this loop will iterate how ever many times the user input
        \\each loop will create 4 dice objects with random D6 values
        \\essentially give you 4 "rolls" for each iteration of the loop
    

    This main class has no nested loops or if statements.

    Your Dice class may look something like this

    \\ Dice
    
    \\declare a private int named something like roll to hold the random value
    
    \\constructor
        \\assign roll a random int value between 1 and 6
    
    
    \\getter
        \\return the random value
    

    That should be enough to get you started.

    As an added bonus, if you wanted to get the program to "run" and ask he user for a another set of die and roll them again--over and over until you closed the program--, you could wrap the body of the main(), starting just before you prompt the user for input, with a while loop which is always true. This creates an infinite loop (and nested loops so it does not work for your solution). However, this loop will allow your program to "run" until you terminate the console it is running in. Again, this would be if you wanted to use this for your own use, it would not satisfy the requirements of your specific problem if you included the while(true){} loop.

    I hope this all helps

    Mkreegs