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