MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply TodayGiven two positive integer inputs, a (attacker army units) and d (defender army units) return the probablity of victory (from 0.000 to 1.000) to +- 0.02 accuracy. The rules are given below for those unfamiliar with the game.

In the board game RISK battles are determined by the conflict of armies, namely the attacking army and the defending army. The results is determined as follows: the attacker rolls 3 six-sided die and the defender rolls 2 die. The highest two numbers of each player are compared respectively, and the higher number wins (this means the opposing army loses one unit). In the case of a tie the defender wins. For example:

Attacker has 10 units Defender has 10 units

Attacker rolls [6 3 2] Defender rolls [4 3]

The first comparison is attacker - 6, defender - 4. Since the attacker is higher, the defender loses one unit. Hence Attacker has 10 units, Defender now has 9 units.

The first comparison is attacker - 3, defender - 3. Since the defender is higher, the attacker loses one unit. Hence Attacker has 9 units, Defender now has 9 units.

This is continued until either the attacker has only one unit left, in which case the defender wins the battle; or the defender has no units left, in which case the attacker wins the battle.

This is one further rule: the number of die any player may roll cannot be more than the their units in case of the defender, or their units + 1 in case of the attacker.

Example: Attacker has 3 units, Defender has 1 units.

Attacker rolls 2 die (3 - 1), Defender rolls 1 die.

20 correct solutions
9 incorrect solutions

Last solution submitted on Jul 06, 2014

2 Comments

Richard Zapor
on 24 Feb 2013

James, your speed is impacted by recalculating previously evaluated cases. A case of 100 v 100 finished in ??

James
on 26 Feb 2013

You're correct. It is inefficient with super-large armies. I was going for an exact solution (which was an offshoot of the original problems I had with the test suite), and recursion seemed to be the best way to do it.
As our resident "Get this problem to run faster" expert, do you think setting up a global m-by-n matrix with previously calculated probabilities would help things?

2 Comments

Richard Zapor
on 11 Feb 2013

suggest writing script to produce testsuite of more cases to paste into cody

Jeremy
on 11 Feb 2013

Yes, I am enlarging the test suite. Thanks for the input!

6 Comments