So, do we know an equation to find those probabilities? No. Probably we'll find one with a little google search, but why not study it using a more programming method? We have the technology, we know a little bit of programming, so, let's take our chances.

- import java.util.Arrays;
**public class**RiskProbability- {
**private static int**[] diceThrow(**int**n)- {
**int**[] dice =**new int**[n];**for**(**int**i = 0; i < dice.length; i++)- dice[i] = (
**int**)(Math.random()*6) + 1; - Arrays.sort(dice);
**return**dice;- }
**public enum**winner {ATTACKER, DEFENDER, TIE};**private static**winner battle(**int**a,**int**d)- {
**int**[] attacker = diceThrow(a);**int**[] defender = diceThrow(d);**int**aSoldiersLost=0;**int**dSoldiersLost=0;**int**j=defender.length - 1;**for**(**int**i = attacker.length - 1; i >= 0 && j >= 0 ; i--)- {
**if**(attacker[i] > defender[j--])- dSoldiersLost++;
**else**- aSoldiersLost++;
- }
**if**(aSoldiersLost > dSoldiersLost)**return**winner.DEFENDER;**else if**(dSoldiersLost > aSoldiersLost)**return**winner.ATTACKER;**else****return**winner.TIE;- }
**public static void**main(String[] args)- {
**int**experimentSize = Integer.parseInt(args[0]);**int**attWins = 0, defWins = 0, ties = 0;**int**attStyle = Integer.parseInt(args[1]);**int**defStyle = Integer.parseInt(args[2]);**for**(**int**ex = 1; ex <= experimentSize; ex++)- {
- winner w = battle(attStyle, defStyle);
**switch**(w)- {
**case**ATTACKER: attWins++;**break**;**case**DEFENDER: defWins++;**break**;**case**TIE: ties++;**break**;**default**:**break**;- }//switch
- }
- System.out.println("Experiment is with " + attStyle + " attackers and " + defStyle + "
- defenders " + experimentSize + " times");
- System.out.println((attWins/(
**double**)experimentSize) + " attackers"); - System.out.println((defWins/(
**double**)experimentSize) + " defenders"); - System.out.println((ties/(
**double**)experimentSize) + " ties"); - }//main
- }

Lines 4-11 is the definition of a throw dice function. You specify the number of dice to throw, and a sorted array of the values of each die is returned. Watch out because the dice are sorted in ascending order.

Line 12 is an enumeration to be used for specifying the winner.

Line 13 is the definition of the battle function that takes two arguments, the number of dice the attacker will use and those of the defender as a second argument. Then the diceThrow is called and the battle begins on lines 20-26 until one runs out of dice.

Line 34 is the main method where the battle is initiated for 'experimentSize' number of times using the specified number of dice for each player.

To run this properly you have to pass 3 integer arguments, the experiment size (1000000 should be more than enough), the number of dice of the attacker and the number of dice of the defender. These are the results obtained by using the above program:

*$ java RiskProbability 1000000 3 2*

Experiment is with 3 attackers and 2 defenders 1000000 times

0.371734 attackers

0.291923 defenders

0.336343 ties

*java RiskProbability 1000000 2 2*

Experiment is with 2 attackers and 2 defenders 1000000 times

0.227412 attackers

0.448301 defenders

0.324287 ties

*$ java RiskProbability 1000000 3 1*

Experiment is with 3 attackers and 1 defenders 1000000 times

0.66038 attackers

0.33962 defenders

0.0 ties

*$ java RiskProbability 1000000 2 1*

Experiment is with 2 attackers and 1 defenders 1000000 times

0.578971 attackers

0.421029 defenders

0.0 ties

*$ java RiskProbability 1000000 1 1*

Experiment is with 1 attackers and 1 defenders 1000000 times

0.416221 attackers

0.583779 defenders

0.0 ties

*$ java RiskProbability 1000000 1 2*

Experiment is with 1 attackers and 2 defenders 1000000 times

0.25389 attackers

0.74611 defenders

0.0 ties

So, the next time you'll play RISK, you'll know.

## No comments:

## Post a Comment