Now I took three semesters of statistics in college. Unfortunately that was 35 years ago and not as much stuck as I would like. Embarrassing but true. So when someone suggests “calculate the odds” I know that there is a right and mathematically sound statistical formula to do it. On the other hand creating a simulation just seems like more fun. Which brings me to my latest interesting coding project. Not long ago Henk Nicolai sent me the following project suggestion:

Since I'm not sure if leaving a comment works, I thought I'd try sending you this nice little challenge by email: Code a function that gives you the probability of a successful attack in RISK, given the sizes of the attacking and defending armies. You're allowed to make assumptions about the number of dice the attacker and defender will use.

As someone who used to love playing RISK with his family as a younger person this had some immediate appeal. For those of you who are not familiar with the game this is more or less how an attach works. Each player has some number of “armies” or game pieces. The attacker can role the same number of dice as he has armies on the attacking location up to three. The defender can roll the same number of dice as he has pieces on the defending location up to two. Either player can roll fewer dice if they wish – a lot of strategy comes into play that we will ignore for right now. After the roll the highest attacking dice of each player are compares. If the highest die of the attacker is higher than the highest die of the defender then the defender loses a game piece. If the highest die of the defender is the same value or higher than the attacker’s die then the attacker loses a game piece. Then the next highest dice are compared with the same sorts of results.

The combat is over when either the defender has no more game pieces or the attacker either decides not to continue or has only one game piece left in the attacking location. So assuming the attacker continues to attach until they either win or have only one game piece left what are the winning chances based on different starting balances of game pieces? Simple? OK maybe it requires some thought. Actually I have spent quite a bit of time thinking about how to do this the best way.

At first I was thinking just use a pair of variables to hold how many armies are at each of two locations. Perhaps two arrays of length two and three for the dice. Pick some random values, compare and decrement the army values appropriately. Now I am wondering if each location on the board should be a class. A little more work in set up but I could also use it to track more information such as name, capital, continent, etc. in case I ever decided to do a larger more involved simulation. Plus maybe that would be an easier way to handle the rolls of dice (have each object do its own rolling), calculating of wins and new values. It’s a thought. I just not one to start coding without a plan. On the other hand I have to stop at some point and say “let’s translate these ideas into code and see where it takes us.”

How would you solve it?

You are forgetting to factor in who is throwing the dice. If it was my mother throwing the dice you would want to only include the numbers 3 – 6, and if my father was rolling you would only allow 1-4. For some reason the dice lose numbers depending on who is throwing them.

Years ago my father created electronic dice to play with. He still couldn't roll good numbers.

I think we'll have to assume a fair and normal roll of the dice. Although you could obviously "load" the dice if you really wanted to. 🙂