Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解决这个风险骰子游戏的java算法_Java_Algorithm_Random_Dice - Fatal编程技术网

解决这个风险骰子游戏的java算法

解决这个风险骰子游戏的java算法,java,algorithm,random,dice,Java,Algorithm,Random,Dice,就像在风险游戏中一样,我必须掷骰子,但我不想在每次掷骰子时使用Random.nextInt()方法。 由于某些原因,我已经知道了攻击的结果,我只想生成骰子的值。 在风险游戏中,骰子被单独排序和比较: 例如:如果攻击掷3个骰子,防御掷2个骰子,结果如下: [4,3,5]-[3,5]攻击损失1辆坦克,防御损失1辆坦克,因为:[5,4,3]-[5,3]5=5(平局时攻击损失)4>3(防御损失)3(本例中第三个骰子无效) 这是输入代码: int diceForAttack = StringUtils.n

就像在风险游戏中一样,我必须掷骰子,但我不想在每次掷骰子时使用
Random.nextInt()
方法。 由于某些原因,我已经知道了攻击的结果,我只想生成骰子的值。

在风险游戏中,骰子被单独排序和比较:
例如:如果攻击掷3个骰子,防御掷2个骰子,结果如下:
[4,3,5]-[3,5]
攻击损失1辆坦克,防御损失1辆坦克,因为:
[5,4,3]-[5,3]
5=5(平局时攻击损失)
4>3(防御损失)
3(本例中第三个骰子无效)

这是输入代码:

int diceForAttack = StringUtils.nextInt(3) + 1;
int diceForDefense = StringUtils.nextInt(3) + 1;
//prints: attack Vs. defense
System.out.println(String.format("%d Vs. %d", diceForAttack, diceForDefense));

int maxLost = Math.min(diceForAttack, diceForDefense);
int attackLoses = StringUtils.nextInt(maxLost + 1);
int defenseLoses = maxLost - attackLoses;
//prints:
//Attack lost x
//Defense lost y
System.out.println(String.format("Attack lost %d\nDefense lost %d", attackLoses, defenseLoses));

//Now I want to generate two arrays
//such that the dice respect the values attackLoses and defenseLoses
int[] attack = new int[diceForAttack];
int[] defense= new int[diceForDefense];
...
...
...

问题是:
因为我已经知道有多少坦克会失去进攻和防御。

如何生成两个数组,使骰子尊重AttackLoss和DefenseLoss的值?

处理此类问题的简单方法是重新掷骰子,直到得到想要的结果。缺点是,如果请求的结果不太可能(甚至不可能),则需要一段时间,但这似乎不是问题。

为什么不生成骰子,然后根据规则确定结果?为每辆因攻击而损失的坦克添加平局(随机值,攻击和防御相同)(它甚至不一定是随机的。1就可以了)。例如,为每辆因防御而损失的坦克添加一个>。2和1。或者在[1-5]上添加一个随机值,在[x-6]上添加另一个随机值@rgetman,因为我有很多骰子类型,从简单到现实。现实使用基于规则的概率。但是简单模式支持攻击。@njzk2谢谢。我尝试实现你的建议,然后我会让你知道。对于“简单”模式,你可以打平手,而不是防守方。