【问题标题】:Resolve this java algorithm for Dice for Risk game为 Dice for Risk 游戏解决这个 java 算法
【发布时间】:2014-10-02 17:43:41
【问题描述】:

就像在风险游戏中一样,我必须掷骰子,但我不想每次掷骰都使用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];
...
...
...


问题是:
因为我已经知道有多少坦克会失去进攻和防守。
如何生成两个数组以使骰子尊重值 attackLoses 和 defenceLoses?

【问题讨论】:

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

标签: java algorithm random dice


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多