Last active
August 29, 2015 14:10
-
-
Save TheRayTracer/0b6228f22bfd91f7dfe8 to your computer and use it in GitHub Desktop.
A small collection of scripts to simulate and analyse a game of Monopoly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def roll_dice(dice_count=1, face_count=6): | |
sum = 0 | |
for i in range(0, dice_count): | |
sum = sum + random.randint(1, face_count) | |
return sum | |
if __name__ == "__main__": | |
colour = ["Light Gray", "Brown", "Light Gray", "Brown", "Light Gray", "Azure", "Light Blue", "Light Gray", "Light Blue", "Light Blue", "Light Gray", "Pink", "Tan", "Pink", "Pink", "Azure", "Orange", "Light Gray", "Orange", "Orange", "Light Gray", "Red", "Light Gray", "Red", "Red", "Azure", "Yellow", "Yellow", "Tan", "Yellow", "Light Gray", "Green", "Green", "Light Gray", "Green", "Azure", "Light Gray", "Dark Blue", "Light Gray", "Dark Blue"] | |
name_uk = ["Go", "Old Kent Road", "Community Chest #1", "Whitechapel Road", "Income Tax", "King's Cross Station", "The Angel Islington", "Chance #1", "Euston Road", "Pentonville Road", "Jail", "Pall Mall", "Electric Company", "Whitehall", "Northumberland Avenue", "Marylebone Station", "Bow Street", "Community Chest #2", "Marlborough Street", "Vine Street", "Free Parking", "Strand", "Chance #2", "Fleet Street", "Trafalgar Square", "Fenchurch St Station", "Leicester Square", "Coventry Street", "Water Works", "Piccadilly", "Go To Jail", "Regent Street", "Oxford Street", "Community Chest #3", "Bond Street", "Liverpool Street Station", "Chance #3", "Park Lane", "Super Tax", "Mayfair"] | |
name_us = ["Go", "Mediterranean Avenue", "Community Chest #1", "Baltic Avenue", "Income Tax", "Reading Railroad", "Oriental Avenue", "Chance #1", "Vermont Avenue", "Connecticut Avenue", "Jail", "St. Charles Place", "Electric Company", "States Avenue", "Virginia Avenue", "Pennsylvania Railroad", "St. James Place", "Community Chest #2", "Tennessee Avenue", "New York Avenue", "Free Parking", "Kentucky Avenue", "Chance #2", "Indiana Avenue", "Illinois Avenue", "B. & O. Railroad", "Atlantic Avenue", "Ventnor Avenue", "Water Works", "Marvin Gardens", "Go To Jail", "Pacific Avenue", "North Carolina Avenue", "Community Chest #3", "Pennsylvania Avenue", "Short Line", "Chance #3", "Park Place", "Luxury Tax", "Boardwalk"] | |
board = [0] * 40 | |
index = 0 | |
random.seed(10) | |
for i in range(0, 1000000): | |
index = index + roll_dice(2, 6) | |
index = (index + 40) % 40 | |
board[index] += 1 | |
if index == 2 or index == 17 or index == 33: # Community Chest indexes | |
community = random.randint(0, 15) | |
if community == 0: # Advance to Go | |
index = 0 | |
elif community == 1: # Go to jail | |
index = 10 | |
board[index] += 1 | |
elif index == 7 or index == 22 or index == 36: # Chance indexes | |
chance = random.randint(0, 15) | |
if chance == 0: # Advance to Go | |
index = 0 | |
elif chance == 1: # Advance to King's Cross Station | |
index = 5 | |
elif chance == 2: # Go to jail | |
index = 10 | |
elif chance == 3: # Advance to Pall Mall | |
index = 11 | |
elif chance == 4: # Advance to Trafalgar Square | |
index = 24 | |
elif chance == 5: # Advance to Mayfair | |
index = 39 | |
elif chance == 6: # Go back 3 spaces | |
index -= 3 | |
elif chance == 7: # Advance to nearest utility | |
if index == 7: | |
index = 12 | |
elif index == 22: | |
index = 28 | |
elif index == 36: | |
index = 12 | |
elif chance == 8: # Advance to nearest railroad | |
if index == 7: | |
index = 15 | |
elif index == 22: | |
index = 25 | |
elif index == 36: | |
index = 5 | |
board[index] += 1 | |
elif index == 30: # Go to jail index (no need to wait 3 turns to exit jail, or attempt to roll a double) | |
index = 10 | |
board[index] += 1 | |
print("colour, name, visits") | |
for i in range(0, 40): | |
print("{}, {}, {}".format(colour[i], name_uk[i], board[i])) | |
input("Press <Enter>") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2); | |
library(gridExtra); | |
# Load our data set. | |
visits = read.csv("monopoly_data_set.csv", colClasses = "character"); | |
visits[, 3] = sapply(visits[, 3], as.numeric); | |
# Plot the data without modification. | |
visits_by_location = visits; | |
# ggplot likes to use factors to order. | |
visits_by_location$name = factor(visits_by_location$name, levels = visits_by_location$name, ordered = TRUE); | |
gg1 = ggplot(data = visits_by_location, aes(x = name, y = visits)) + xlab("Location - ordered by board layout") + ylab("Visits") + ggtitle("Monopoly - One player - 1,000,000 dice rolls") + geom_bar(stat = "identity", color = "Black", fill = visits_by_location$colour) + guides(fill = FALSE) + theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1)); | |
# Plot the data sorted decsending. | |
visits_by_location_sorted = visits[order(-visits[,3]),]; | |
visits_by_location_sorted$name = factor(visits_by_location_sorted$name, levels = visits_by_location_sorted$name, ordered = TRUE); | |
gg2 = ggplot(data = visits_by_location_sorted, aes(x = name, y = visits)) + xlab("Location - ordered by most visits") + ylab("Visits") + ggtitle("Monopoly - One player - 1,000,000 dice rolls") + geom_bar(stat = "identity", color = "Black", fill = visits_by_location_sorted$colour) + guides(fill = FALSE) + theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1)); | |
# Plot the property only data sorted decsending. | |
visits_property_only_sorted = visits[order(-visits[,3]),]; | |
# Remove the non property group. | |
visits_property_only_sorted = visits_property_only_sorted[visits_property_only_sorted$colour != "Light Gray",]; | |
visits_property_only_sorted$name = factor(visits_property_only_sorted$name, levels = visits_property_only_sorted$name, ordered = TRUE); | |
gg3 = ggplot(data = visits_property_only_sorted, aes(x = name, y = visits)) + xlab("Property only - ordered by most visits") + ylab("Visits") + ggtitle("Monopoly - One player - 1,000,000 dice rolls") + geom_bar(stat = "identity", color = "Black", fill = visits_property_only_sorted$colour) + guides(fill = FALSE) + theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1)); | |
visits_sum_by_group = aggregate(visits ~ colour, visits, sum); | |
# Duplicate a column, then restore the original column names. | |
visits_sum_by_group = data.frame(visits_sum_by_group$colour, visits_sum_by_group$colour, visits_sum_by_group$visits, stringsAsFactors = FALSE); | |
names(visits_sum_by_group) = c("colour", "group", "visits"); | |
visits_sum_by_group[visits_sum_by_group$group == "Tan", 2] = "Utility"; | |
visits_sum_by_group[visits_sum_by_group$group == "Azure", 2] = "Station"; | |
# Remove the non property group. | |
visits_sum_by_group[visits_sum_by_group$group=="Light Gray", 2] = "Other"; | |
visits_sum_by_group = visits_sum_by_group[visits_sum_by_group$group != "Other",]; | |
# Plot the data sorted decsending. | |
visits_sum_by_group_most_to_least = visits_sum_by_group[order(-visits_sum_by_group[,3]),]; | |
visits_sum_by_group_most_to_least$group = factor(visits_sum_by_group_most_to_least$group, levels = visits_sum_by_group_most_to_least$group, ordered = TRUE); | |
gg4 = ggplot(data = visits_sum_by_group_most_to_least, aes(x = group, y = visits)) + xlab("Property group") + ylab("Visits") + ggtitle("Monopoly - One player - 1,000,000 dice rolls") + geom_bar(stat = "identity", color = "Black", fill = visits_sum_by_group_most_to_least$colour) + guides(fill = FALSE) + theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1)); | |
grid.arrange(gg1, gg2, gg3, gg4, ncol = 2, nrow = 2); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
colour | name | visits | |
---|---|---|---|
Light Gray | Go | 31260 | |
Brown | Old Kent Road | 21651 | |
Light Gray | Community Chest #1 | 41132 | |
Brown | Whitechapel Road | 22055 | |
Light Gray | Income Tax | 23685 | |
Azure | King's Cross Station | 28433 | |
Light Blue | The Angel Islington | 22914 | |
Light Gray | Chance #1 | 34047 | |
Light Blue | Euston Road | 23576 | |
Light Blue | Pentonville Road | 23101 | |
Light Gray | Jail | 58870 | |
Pink | Pall Mall | 27451 | |
Tan | Electric Company | 26466 | |
Pink | Whitehall | 24231 | |
Pink | Northumberland Avenue | 24787 | |
Azure | Marylebone Station | 27889 | |
Orange | Bow Street | 27858 | |
Light Gray | Community Chest #2 | 54810 | |
Orange | Marlborough Street | 29235 | |
Orange | Vine Street | 30523 | |
Light Gray | Free Parking | 28771 | |
Red | Strand | 27929 | |
Light Gray | Chance #2 | 39849 | |
Red | Fleet Street | 27413 | |
Red | Trafalgar Square | 31618 | |
Azure | Fenchurch St Station | 28749 | |
Yellow | Leicester Square | 27193 | |
Yellow | Coventry Street | 26830 | |
Tan | Water Works | 28132 | |
Yellow | Piccadilly | 25885 | |
Light Gray | Go To Jail | 26347 | |
Green | Regent Street | 26527 | |
Green | Oxford Street | 26425 | |
Light Gray | Community Chest #3 | 49283 | |
Green | Bond Street | 24737 | |
Azure | Liverpool Street Station | 24290 | |
Light Gray | Chance #3 | 33423 | |
Dark Blue | Park Lane | 22195 | |
Light Gray | Super Tax | 21758 | |
Dark Blue | Mayfair | 26398 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment