Having trouble with my dungeons and dragons dice roller

﹥>﹥吖頭↗ 提交于 2019-12-12 02:47:33

问题


import java.util.Scanner;

public class Project2Main {

public static void main(String[] args) {

    Scanner kb = new Scanner(System.in);
    int numberOfSets = 0;
    System.out.println("How many sets of dice would you like to roll?");
    numberOfSets = kb.nextInt();
    kb.nextLine();

    for (int i = 0; i < numberOfSets; i++) {
        int dice1 = 0;
        int dice2 = 0;
        int dice3 = 0;
        int dice4 = 0;
        int diceTotal = 0;

        if (dice1 < dice2 && dice1 < dice3 && dice1 < dice4) {
            diceTotal = dice2 + dice3 + dice4;
            System.out.println("Your roll total is " + diceTotal + " and your lowest roll was " + dice1 + ".");
            break;
        } else if (dice2 < dice1 && dice2 < dice3 && dice2 < dice4) {
            diceTotal = dice1 + dice3 + dice4;
            System.out.println("Your roll total is " + diceTotal + " and your lowest roll was " + dice2 + ".");
            break;
        } else if (dice3 < dice1 && dice3 < dice2 && dice3 < dice4) {
            diceTotal = dice1 + dice2 + dice4;
            System.out.println("Your roll total is " + diceTotal + " and your lowest roll was " + dice3 + ".");
            break;
        } else if (dice4 < dice1 && dice4 < dice2 && dice4 < dice3) {
            diceTotal = dice1 + dice2 + dice3;
            System.out.println("Your roll total is " + diceTotal + " and your lowest roll was " + dice4 + ".");
            break;
        }
    }
    kb.close();
}

This is my main class. The whole point of this is to take four different rolls of a six sided die, check and see which roll is the lowest, and add the three highest rolls together while telling the user what their lowest roll was.

The main thing I am having trouble with is making a dice class.

I know I need an int to hold the dice value, a constructor to actually create the random integer, and a getter to actually return the random integers to the main class. How can I go about doing this?

Another question: How can I make the user choose a set and reroll the lowest value in that set? What I mean by that is when the user rolls, the three largest dice rolls are added together and the lowest one is put off to the side. The user must be forced to re roll the lowest value of the three highest dice roll even if they do not want to.

The user must be able to put in a number dictating which set will end up being re rolled.

I apologize if this does not make much sense, so if anyone would like to suggest edits to make this more clear, that would be great.


回答1:


Custom DiceRoller class that has capability of rolling any number of 4d6-drop-lowest sets.

import java.util.*;

public class DiceRoller
{
    private Random rand;

    public DiceRoller()
    {
        this.rand = new Random();
    }

    public int rollSingleDie()
    {
        return rand.nextInt(6)+1;
    }

    public List<Integer> roll4d6DropLowest()
    {
        List<Integer> retList = new ArrayList<Integer>();

        // Add 4 numbers to the list to simulate 4 rolls.
        for (int i=0; i<4; i++)
        {
            retList.add(rollSingleDie());
        }

        // Remove the lowest roll of the 4. 
        retList.remove(Collections.min(retList));

        return retList;
    }

    public List<List<Integer>> rollSets(int numSets)
    {
        List<List<Integer>> results = new List<List<Integer>>();
        for (int i=0; i<numSets; i++)
        {
            results.add(roll4d6DropLowest());
        }
        return results;
    }
}

And in your main method:

public static void main(String args[])
{
    /*** Rolling the initial sets ***/
    int numSets = 10; // Ten is a placeholder.
                      // This is where you get input from user.

    DiceRoller roller = new DiceRoller();
    List<List<Integer>> diceSets = roller.rollSets(numSets);

    for (List<Integer> diceRolls : diceSets)
    {
        Integer total = sum(diceRolls);
        Integer lowest = Collections.min(diceRolls);
        System.out.println("Total is : " + total + " and lowest is : " + lowest);
    }

    /*** Forcing user to reroll lowest of one set ***/
    int setToRerollIndex = 1; // Placeholder value.
    // Replace with input from user on which set to reroll.
    // Remember to adjust user input - they will probably use a number between 1 
    // and the number of sets, but the List needs an index between 0 and the number of
    // sets - 1. 
    List<Integer> setToReroll = diceSets.get(setToRerollIndex);
    int indexOfLowest = setToReroll.indexOf(Collections.min(setToReroll));
    setToReroll.set(indexOfLowest, roller.rollSingleDie());

    // Selected set has now rerolled the lowest value.
}

public static int sum(List<Integer> list)
{
    int sum= 0; 
    for (int number : list)
        sum = sum + number;
    return sum;
}


来源:https://stackoverflow.com/questions/32748203/having-trouble-with-my-dungeons-and-dragons-dice-roller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!