代码之家  ›  专栏  ›  技术社区  ›  KaiMcKiernan

基于Math.random()的函数在另一个函数内不起作用[关闭]

  •  -1
  • KaiMcKiernan  · 技术社区  · 2 月前

    我正在为带有“爆炸骰子”的TTRPG编写一个模拟器,这基本上意味着如果你在一个骰子上掷出一个最大数字,那么你就可以重新掷出那个骰子,并将结果加到最大数字上。这意味着100%不可能得到等于最大值倍数的结果。

    我编写了一个函数, explosiveDiceRoll() ,滚动一个爆炸的骰子,它是有效的,它永远不会等于8(用一个永无止境的while循环进行测试)。

    但是我编写了另一个函数, multiDiceRoll() ,可以滚动多个爆炸骰子,并具有优势/劣势。In multiDiceRoll() ,我用 炸药DiceRoll() 并通过循环等方式运行它,以模拟多个骰子和优势。

    问题是 炸药DiceRoll() 在内部以某种方式滚动最大数字 multiDiceRoll() 功能,这应该是不可能的。这是为什么?此外,我该如何解决这个问题?

    我遇到了一个类似的问题 Math.random() 每次放入循环中时都是一样的。我不知道这是否相关或有帮助。

    我在while循环中分别运行了这两个函数。回路总体设计:

    while([function with an input of a 1d8, no advantage] != 8) {}
    

    这个想法是,由于获得8是不可能的,while循环将永远持续下去。我每次最多等几秒钟。

    炸药DiceRoll() 永远持续下去,它工作正常

    multiDiceRoll() 快速停止,总是以控制台中的以下内容结束:

    1d8
    8,8 - SHOULD NOT EQUAL
    [ 8 ]
    
    function explosiveDiceRoll(maxRoll) {
        let roll = diceRoll(maxRoll);
        let sum = roll;
        while(roll === maxRoll) { //Reroll and add everytime it hits the max roll
            roll = diceRoll(maxRoll);
            if(roll === 0) console.log("zero error");
            sum += roll
        }
        return sum;
        
    }
    
    function multiDiceRoll(diceString,advantage) {
        diceString = diceString.split("d");
        // console.log(diceString);
        let diceAmount= diceString[0];
        let diceType = diceString[1];
        console.log(`${diceAmount}d${diceType}`);
    
    
        let results = [];
        for(let i = 0; i <= Math.min(2,Math.abs(advantage)); i++) {
            let result = 0;
            for(let d = 1; d <= diceAmount; d++) {
                let roll = explosiveDiceRoll(diceType);
                console.log(`${roll},${diceType} - SHOULD NOT EQUAL`);
                result += roll;
            }
            results.push(result);
        }
        console.log(results);
        if(advantage >= 0) return Math.max(...results);
        else return Math.min(...results);
    }
    
    export function diceRoll(maxRoll) {
        return Math.ceil(Math.random()*maxRoll);
    }
    
    1 回复  |  直到 2 月前
        1
  •  0
  •   A_____ A_______    2 月前

    diceType是代码中的字符串

    function multiDiceRoll(diceString,advantage) {
        diceString = diceString.split("d");
        ...
        let diceType = diceString[1];
        ...
        for(let i = 0; i <= Math.min(2,Math.abs(advantage)); i++) {
            ...
            for(let d = 1; d <= diceAmount; d++) {
                let roll = explosiveDiceRoll(diceType);
                ...
            }
            ...
        }
        ...
    }
    

    所以在这里:

    function explosiveDiceRoll(maxRoll) {
        ...
        while(roll === maxRoll) {
            ...
        }
        ...
    }
    

    您正在尝试检查 number === string 这将永远是 false
    ( 1*'8' === '8' -> false 1*'8' === 8 -> true )
    这样做,你的代码就会工作:

    function multiDiceRoll(diceString,advantage) {
        diceString = diceString.split("d");
        
        let diceAmount = parseInt(diceString[0]);
        let diceType = parseInt(diceString[1]);
        ...
    }