遇到我的地下城和龙骰子滚轮问题

时间:2015-09-23 19:43:43

标签: java dice

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();
}

这是我的主要课程。这一点的全部意义是采用四个不同的六面模具卷,检查并确定哪个卷最低,并将三个最高的卷添加到一起,同时告诉用户他们的最低卷是什么。

我遇到麻烦的主要是骰子课。

我知道我需要一个int来保存骰子值,一个实际创建随机整数的构造函数,以及一个实际将随机整数返回到主类的getter。我该怎么做呢?

另一个问题:如何让用户选择一个集合并重新设置该集合中的最低值?我的意思是当用户滚动时,三个最大的骰子卷被加在一起而最下面的一个被放到一边。必须强制用户重新掷出三个最高骰子卷的最低值,即使他们不想这样做。

用户必须能够输入一个数字,指示哪一组最终会被重新滚动。

如果这没有多大意义,我很抱歉,所以如果有人想建议编辑以使其更清楚,那就太棒了。

1 个答案:

答案 0 :(得分:0)

自定义DiceRoller类,可以滚动任意数量的4d6-drop-lower集。

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;
    }
}

在你的主要方法中:

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;
}