# F# and Monopoly Probabilities

August 13, 2013 Leave a comment

My sons and I were playing Monopoly when we started discussing different strategies for property acquisition. For example, should you try and get Park Place and Boardwalk for the large rent but low probability of someone landing on it or should you get the purples with a high hit chance but lower payout?

We decided to run a simulation and since I an teaching myself F#, we coded up a F# answer. I created a F# Tutorial project and then added a .fsx file. In that file, I first created a couple of variables – 1 of which is a .NET type:

- let tiles = [|0 .. 39|]
- let random = System.Random()

I then added a Community Chest function that returns a 1 in 16 chance of Going to Jail (board location 10) and a 1 in 16 chance of going to GO (board location 0). This is not completely accurate because we don’t shuffle the deck after every draw – but it seems close enough.

- let communityChest x =
- let communityChestDraw = random.Next(1,17)
- if communityChestDraw = 1 then
- 0
- else if communityChestDraw = 2 then
- 10
- else
- x

I then added a Chance function that did the same thing – with a lot more possibilities (Go to Boardwalk, go to the nearest railroad, etc…)

- let chance x =
- let chanceDraw = random.Next(1,17)
- if chanceDraw = 1 then
- 0
- else if chanceDraw = 2 then
- 10
- else if chanceDraw = 3 then
- 11
- else if chanceDraw = 4 then
- 39
- else if chanceDraw = 5 then
- x – 3
- else if chanceDraw = 6 then
- 5
- else if chanceDraw = 7 then
- 24
- else if chanceDraw = 8 then
- if x < 5 then
- 5
- else if x < 15 then
- 15
- else if x < 25 then
- 25
- else if x < 35 then
- 35
- else
- 5
- else if chanceDraw = 9 then
- if x < 12 then
- 12
- else if x < 28 then
- 28
- else
- 12
- else
- x

I then added a move function that handled going past the 39th tile and looping around past go and also the “Go to Jail” Tile:

- let move x y =
- if x + y > 39 then
- x + y – 40
- else if x + y = 30 then
- 10
- else if x + y = 2 then
- communityChest 2
- else if x + y = 7 then
- chance 7
- else if x + y = 17 then
- communityChest 2
- else if x + y = 22 then
- chance 22
- else if x + y = 33 then
- communityChest 2
- else if x + y = 36 then
- chance 36
- else
- x + y

I then put it together with a simulation function that ran 10000 iterations:

- let simulation =
- let mutable startingTile = 0
- let mutable endingTile = 0
- let mutable doublesCount = 0
- let mutable inJail = false
- let mutable jailRolls = 0
- for diceRoll in 1 .. 10000 do
- let dieOneValue = random.Next(1,7)
- let dieTwoValue = random.Next(1,7)
- let numberOfMoves = dieOneValue + dieTwoValue
- if dieOneValue = dieTwoValue then
- doublesCount <- doublesCount + 1
- else
- doublesCount <- 0
- if inJail = true then
- if doublesCount > 1 then
- inJail <- false
- jailRolls <- 0
- endingTile <- move 10 numberOfMoves
- else
- if jailRolls = 3 then
- inJail <- false
- jailRolls <- 0
- endingTile <- move 10 numberOfMoves
- else
- inJail <- true
- jailRolls <- jailRolls + 1
- else
- if doublesCount = 3 then
- inJail <- true
- endingTile <- 10
- else
- endingTile <- move startingTile numberOfMoves
- let endingTile = move startingTile numberOfMoves
- printfn "die1: %A + die2: %A = %A FROM %A TO %A"
- dieOneValue dieTwoValue numberOfMoves startingTile endingTile
- startingTile <- endingTile
- tiles.[endingTile] <- tiles.[endingTile] + 1

I hate the mutable keywords. I don’t know enough about F# to not use it – but it seems that my code is a F# plate of spaghetti

I then spit out the results like this:

- let Aggregation =
- for tile in tiles do
- printfn "%A" tile

And sure enough, I got some results:

I then put these results into Excel where I added the tile names

and did a quick pivot table on property groups like this:

Note that the results seem wrong (or not 100% correct) because Tile #2 (Community Chest) can’t be the most landed on tile and I also had 30 out of the 10,000 times where the cop was the final resting place for a turn – which can’t happen.

If I was using C#, I would have done this in about 25% of the time and been 100% right using unit tests – but I am trying to make myself uncomfortable by learning F# and so I muddle through – often I find that the process is more important than the results in learning.

In any event, I want to make the following changes:

- 1) Create a tuple using the Tile Name, the PropertyGroup, and the Count
- 2) Write the unit tests so that I am 100% correct
- 3) Re-write it getting rid of the mutable keyword
- 4) Aggregate the list using the F# constrcuts (versus using Excel)

The kids also want to put in the expected rate of return based on the rent for each tile and then the adjustment for each house. That might be fun – but it is irrelevant for actually winning the game (the marginal benefit of additional analysis is very low). As long as you know they key colors and can get monopolies on them (and prevent monopolies by your opponent), you will win more often than not.