/Risk

An odds calculator for the board game Risk

Primary LanguageScala

README

Risk Odds Calculator
by Ryan Williams (ryan.blake.williams@gmail.com)

Given the an attacking force of size <A> and a defending force of size <D>, this utility computes a variety of probabilities related to the scenario where, in a game of Risk, an attacking force of size <A> attacks a defending force of size <D>, each side always rolls the maximum possible number of dice, and the attacker fights all the way to the last man.

Note that it is often disadvantageous to fight to the last man. The "single-roll" odds (see the -s flag below) show how an attacker is likely to fare on just one roll of, say, 3 dice vs. a defender's 2 (answer: attacker expects to lose 2387/2592 (~= .921) armies, defender expects to lose 2797/2592 (~= 1.079) armies, meaning in the limit an attacker and a defender are tied when the attacker has 2387/2797 (~= .853) times as many armies as the defender.

Usage:
# To simulate a battle to the death between forces of two given sizes:
$ ./risk <attacking_force> <defending_force>

# To see "steady-state" single-roll outcome probabilities:
$ ./risk -s

Examples:

# Probable single-roll outcomes:
$ ./risk -s

	1 attacker die vs. 1 defender die:
		Attacker loses 1, defender loses 0: 21 / 36 = 0.583
		Attacker loses 0, defender loses 1: 15 / 36 = 0.417

	1 attacker die vs. 2 defender die:
		Attacker loses 1, defender loses 0: 161 / 216 = 0.745
		Attacker loses 0, defender loses 1: 55 / 216 = 0.255

	2 attacker die vs. 1 defender die:
		Attacker loses 1, defender loses 0: 91 / 216 = 0.421
		Attacker loses 0, defender loses 1: 125 / 216 = 0.579

	2 attacker die vs. 2 defender die:
		Attacker loses 2, defender loses 0: 581 / 1296 = 0.448
		Attacker loses 1, defender loses 1: 420 / 1296 = 0.324
		Attacker loses 0, defender loses 2: 295 / 1296 = 0.228

	3 attacker die vs. 2 defender die:
		Attacker loses 2, defender loses 0: 2275 / 7776 = 0.293
		Attacker loses 1, defender loses 1: 2611 / 7776 = 0.336
		Attacker loses 0, defender loses 2: 2890 / 7776 = 0.372

	3 attacker die vs. 1 defender die:
		Attacker loses 1, defender loses 0: 441 / 1296 = 0.340
		Attacker loses 0, defender loses 1: 855 / 1296 = 0.660

# Sample outcome of a rather large battle
$ ./risk 20 10

When 20 armies attack 10:

	Probability of winning:

		Attacker: 0.965
		Defender: 0.035

	Expected losses:

		Attacker: 8.267
		Defender: 9.900

	Probabilities of various outcomes:

		Attacker wins by  20: 0.007 | xxxx
		Attacker wins by  19: 0.021 | xxxxxxxxxxxxx
		Attacker wins by  18: 0.039 | xxxxxxxxxxxxxxxxxxxxxxxx
		Attacker wins by  17: 0.059 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
		Attacker wins by  16: 0.077 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
		Attacker wins by  15: 0.087 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
		Attacker wins by  14: 0.096 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
		Attacker wins by  13: 0.092 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
		Attacker wins by  12: 0.092 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
		Attacker wins by  11: 0.079 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
		Attacker wins by  10: 0.074 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
		Attacker wins by   9: 0.059 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
		Attacker wins by   8: 0.052 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
		Attacker wins by   7: 0.039 | xxxxxxxxxxxxxxxxxxxxxxxx
		Attacker wins by   6: 0.033 | xxxxxxxxxxxxxxxxxxxx
		Attacker wins by   5: 0.024 | xxxxxxxxxxxxxx
		Attacker wins by   4: 0.020 | xxxxxxxxxxxx
		Attacker wins by   3: 0.010 | xxxxxx
		Attacker wins by   2: 0.005 | xx
		----------------------------------------------------------------------------------------------------
		Defender wins by   1: 0.007 | xxxx
		Defender wins by   2: 0.011 | xxxxxx
		Defender wins by   3: 0.008 | xxxx
		Defender wins by   4: 0.005 | xx
		Defender wins by   5: 0.003 | x
		Defender wins by   6: 0.001 | 
		Defender wins by   7: 0.001 | 
		Defender wins by   8: 0.000 | 
		Defender wins by   9: 0.000 | 
		Defender wins by  10: 0.000 |