从文本字段计算骰子掷骰

Calculate Dice Roll from Text Field

问题: 我如何读取字符串 "d6+2-d4" 以便每个 d# 将在掷骰子的参数内随机生成一个数字?

澄清: 我想读取一个字符串并拥有它,所以当 d# 出现时,它会随机生成一个数字,例如模拟掷骰子。然后,将所有卷和数字相加以获得总数。举个例子,就像 Roll20 如何使用他们的 /roll 命令一样。 If !clarifying {lstThen.add("look at the Roll20 and play with the /roll command to understand it")} else if !understandStill {lstThen.add("I do not know what to say, someone else could try explaining it better...")}

信息: 我正在为 Dungeons and Dragons 制作一个 Java 程序,却发现我在弄清楚如何计算用户输入时遇到了一个问题:我不知道如何计算这样的字符串。

我推测最后可能需要 Java 的 eval。我知道我想要什么 happen/have 一个关于如何执行的理论(PseudoCode 比 Java 更重要):

Random rand = new Random();
int i = 0;
String toEval;
String char;

String roll = txtField.getText();

while (i<roll.length) {
    check if character at i position is a d, then highlight the numbers
    after d until it comes to a special character/!aNumber
    // so if d was found before 100, it will then highlight 100 and stop
    // if the character is a symbol or the end of the string
    if d appears {
        char = rand.nextInt(#);
        i + #'s of places;
        // so when i++ occurs, it will move past whatever d# was in case
        // d# was something like d100, d12, or d5291
    } else {
        char = roll.length[i];
    }
    toEval = toEval + char;
    i++;
}

perform evaluation method on toEval to get a resulting number
list.add(roll + " = " + evaluated toEval);

编辑: 在 weston 的帮助下,我研究了可能需要的东西,使用带有数组的拆分器,它可以检测某些符号并将其添加到列表中。但是,我没有澄清还需要什么是我的错。上面的伪代码没有帮助,所以这是我还需要弄清楚的。

roll.split("(+-/*^)");

因为这部分也是让我绊倒的地方。我也应该在有数字的地方进行拆分吗?所以像这样的等式:

String[] numbers = roll.split("(+-/*^)");
String[] symbols = roll.split("1234567890d")

// Rough idea for long way
loop statement {
    loop to check for parentheses {
        set operation to be done first
    }

    if symbol {
        loop for symbol check {
            perform operations
    }}} // ending this since it looks like a bad way to do it...

// Better idea, originally thought up today (5/11/15)
int val[];
int re = 1;

loop {
    if (list[i].containsIgnoreCase(d)) {
        val[]=list[i].splitIgnoreCase("d");
        list[i] = 0;
        while (re <= val[0]) {
            list[i] = list[i] + (rand.nextInt(val[1]) + 1);
            re++;
        }
    }
}
// then create a string out of list[]/numbers[] and put together with
// symbols[] and use Java's evaluator for the String

wenton 有它,它似乎不是为我做的(直到我意识到我没有具体说明我想要什么)所以基本上更新,我想要评估的字符串是(我知道它是有点不正统,但这是为了说明一点;我也希望这能进一步阐明使其工作所需的条件):

(3d12^d2-2)+d4(2*d4/d2)

阅读本文后,您可能会看到我不知道如何表现得很好的地方...但这就是为什么我要问你们所有可爱、聪明的程序员!我希望我问得够清楚,谢谢你抽出时间 :3

任何编程问题的诀窍是将其分解并为每个部分编写一个方法,因此下面我有一个掷一个骰子的方法,由掷多个骰子的方法调用。

private Random rand = new Random();

/**
 * @param roll can be a multipart roll which is run and added up. e.g. d6+2-d4
 */
public int multiPartRoll(String roll) {
    String[] parts = roll.split("(?=[+-])"); //split by +-, keeping them
    int total = 0;
    for (String partOfRoll : parts) { //roll each dice specified
        total += singleRoll(partOfRoll);
    }
    return total;
}

/**
 * @param roll can be fixed value, examples -1, +2, 15 or a dice to roll
 * d6, +d20 -d100
 */
public int singleRoll(String roll) {
    int di = roll.indexOf('d');
    if (di == -1) //case where has no 'd'
        return Integer.parseInt(roll);
    int diceSize = Integer.parseInt(roll.substring(di + 1)); //value of string after 'd'
    int result = rand.nextInt(diceSize) + 1; //roll the dice
    if (roll.startsWith("-")) //negate if nessasary
        result = -result;
    return result;
}