2014-10-02 69 views
0

像在风险游戏我必须掷骰子,但我不想使用Random.nextInt()方法为每卷。 由于某些原因,我已经是攻击的结果,我想简单地生成骰子的值。

在风险游戏骰子被排序和单独使用相比:
例如:如果攻击辊3个骰子和防御辊2个骰子和这些是结果:
[4,3,5] - [3, 5]
攻击失去1辆坦克,防御失去1辆坦克,因为:
[5,4,3] - [5,3]
5 = 5(在平局的情况下,攻击失败)
4> 3防守丢失)
3(第三个骰子在这种情况下是无用的)

这是输入的代码:
解决这个骰子风险的Java算法游戏

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和defenseLoses?

+0

为什么不生成骰子,然后根据规则确定结果? – rgettman 2014-10-02 17:45:52

+0

为攻击失败的每个坦克添加一条配合(随机值,攻击和防御相同)(它甚至不必是随机的,1很好)。为防守失去的每个坦克添加一个>。例如2和1。或[1-5]上的一个随机数,另一个在[x-6]上。 – njzk2 2014-10-02 17:47:32

+0

@rgettman因为我有很多骰子类型从易到现实。 Realistic使用基于规则的概率。但Easy模式有利于此次攻击。 – 2014-10-02 17:57:49

回答

1

一个干净,简单的方法来处理像这样的问题是重掷骰子,直到你得到你想要的结果。缺点是,如果要求的结果不太可能(甚至不可能),那么这将需要一段时间,但这似乎在这里不成问题。