Well… here it goes.

The basic idea is that it follows every possible path the battle can take, along with the chance that it will take each given path, until all the paths end, that is, one or both sides run out of units, or the number of rounds has reached the “hit and run” value. At this point the number of units for each player and the chance of arriving at that situation are stored for the result. For a given situation, it has to figure out the chance that each side will hit every given possible number of times, e.g. the chance that 2 infantry and 1 armor will hit zero, one, two, or three times. It then has to do the same thing for the defender, and then create every possible offshoot from the current situation.

For instance, a situation of 1 infantry on attack and one on defense will have 4 possible outcomes after one round of rolling:

attacker hits 0, defender hits 0

attacker hits 1, defender hits 0

attacker hits 0, defender hits 1

attacker hits 1, defender hits 1

The chance of, for example, the second one occurring is (1/6)*(2/3)*c, with c being the chance that this particular situation would arise in the first place.

In this situation, all possible outcomes will result in the battle being over, i.e. they wont have to create any new paths that have to be further calculated.

Of course, there is a problem: this will result in an infinite loop of each side missing forever, which is where some math comes in. That possibility is ignored, and c has to be increased by the chance that both sides will miss, plus the chance that both sides will miss again, etc., forever. Conveniently, it works out that this particular infinite series just equals 1/(1-n), where n is the chance that both sides will completely miss. In the 1 inf. vs. 1 inf. example this would mean that the first situation would never be calculated, and c would be multiplied by 1/(1-(5/6)(2/3)) = 2.25. None of this is necessary if “hit and run” is enabled, though, since the number of battles is limited anyway.

The way it calculates the probability of a given number x of units hitting is by using a formula (n choose k)*p^k*(1-p)^(n-k), where n is the number of events, k is the number that must be true, and p is the probability of each event being true. It has to set up 5 different sets of events for the 5 different chances of a unit hitting, i.e. defending bombers or transports are in the first set of 1/6, while defending jet fighters are in the set of 5/6. It then has to use an algorithm to find every possible combination of x different hits, and add all the probabilities up. In a battle with lots of attacking infantry and tanks, the chance that exactly two units hit is the chance that exactly 0 infantry and 2 tanks hit, plus the chance that 1 inf. and 1 tank hits, plus 0 inf. and 2 tanks.

The current product has a number of improvements over the original, most notably that it allows paths to re-converge and add their probabilities together rather than calculating the same parallel paths over and over again.

*sits back in chair, panting*