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?