###################################################################### # # This file contains a completed function that simulates a (simplified) Yahtzee game, # as well as some questions to explore and plots to produce with ggplot2. # # Rules of Yahtzee (Simplified) # # * Roll five dice, and re-roll some of them until you get a Yahtzee (meaning that all five dice show the same number). # * Roll five dice. Suppose we get 1,2,1,3,1. # * Keep the values that occur the most. In our example, we have three 1s. This is our "current Yahtzee." # * Re-roll the other dice. In this case, we re-roll the two dice that were not 1s. Suppose we get 1 and 4. # * Add to the current Yahtzee. In our example, we have 1,1,1,1,4. Return to second step and repeat until all the values are the same, in which case we have a Yahtzee. # # ###################################################################### ##### Rolling Dice ##### # We previously wrote a function to simulate rolling n dice myDice <- function(n){ sample(1:6, n, replace=T) } ##### Simulating a game of Yahtzee ##### # This function simulates a game and counts the number of rolls. # It returns the number of rolls, the number of individual dice rolled, and the final Yahtzee (5 matching dice). rollYahtzee <- function(){ totRolls <- 0 # we haven't yet rolled indivDice <- 0 # counts rolls of individual dice currMatches <- c() # no matches to start totMatches <- length(currMatches) # total matches while(totMatches < 5){ # repeat while totMatches is less than 5 # keep track of the number of rolls totRolls <- totRolls + 1 # roll the correct number of dice indivDice <- indivDice + (5 - totMatches) currMatches <- c(currMatches, myDice(5 - totMatches)) #check for matches matches <- rep(0,6) for(i in 1:6){ # this counts how often each value i occurs matches[i] <- sum(currMatches == i) } max.roll <- which.max(matches) # this is a value with the most matches totMatches <- max(matches) # how many matches currMatches <- rep(max.roll, totMatches) # keep only the current matches } # return the result c(totRolls, indivDice, currMatches) } # try it out: rollYahtzee() rollYahtzee()[1] ##### Now simulate Yahtzee many times and record the number of rolls for each simulation. ##### M <- 1000 rollList <- c() # total number of rolls in each game dieList <- c() # total number of individual dice rolled in each game for(i in 1:M){ result <- rollYahtzee() rollList[i] <- result[1] dieList[i] <- result[2] } ##### Questions ##### # What is the mean number of rolls? rollList mean(rollList) # What proportion of the time do you get Yahtzee in three or fewer rolls? rollList <= 3 sum(rollList <= 3)/length(rollList) mean(rollList <= 3) # Make a histogram of the number of rolls. # Do this with the built-in hist() function, and then with ggplot2. # You might need to use Google or ask a friend about how to use ggplot2. hist(rollList) #make a data frame df <- data.frame(rollList, dieList) #make a ggplot and add a plot ggplot(df, aes(x=rollList)) + geom_histogram(color="purple", fill="white", binwidth=3) #try a dotplot ggplot(df, aes(x=rollList)) + geom_dotplot(color="purple", fill="white", binwidth=3) #try a density plot ggplot(df, aes(x=rollList)) + geom_density(color="purple", fill="white") # Add a vertical line at the mean value to your histogram ggplot(df, aes(x=rollList)) + geom_histogram(color="purple", fill="white", binwidth=3) + geom_vline(aes(xintercept = mean(rollList)), color="blue", linetype="dashed") + ggtitle("Number of Rolls") ##### Extension: Count the number of rolls of individual dice ##### # We previously counted each round of the game as 1 roll, regardless of how many dice were rolled simultaneously. # Now, modify the simulation so that it counts the total number of dice that are rolled. # What is the man number of rolls of individual dice to get Yahtzee? # Make a histogram of the number of rolls until you get Yahtzee # Display both histograms (total rolls and total rolls of individual dice) simultaneously (they should overlap). ggplot(df) + geom_histogram(aes(rollList), color="#333333", fill="blue", alpha=0.3, binwidth=3) + geom_histogram(aes(dieList), color="#333333", fill="orange", alpha=0.3, binwidth=3) ggplot(df) + geom_histogram(aes(rollList), binwidth=5, color="black", fill="green", alpha=0.3) + geom_histogram(aes(dieList), binwidth=5, color="black", fill="blue", alpha=0.3) # Make a scatterplot of total rolls vs. total rolls of individual dice ggplot(df, aes(x=rollList, y=dieList)) + geom_point(color="purple", alpha=0.2) ggplot(df, aes(x=rollList, y=dieList)) + geom_point(color="blue", alpha=0.1) + geom_smooth(method=lm, color="green") ggplot(df, aes(x=rollList, y=dieList)) + geom_jitter(color="blue") ##### Another Extension: Frequencies of outcomes ##### # How often does each number 1,2,...,6 appear as the matched number? # The proportions won't be 1/6, because sometimes there is a tie for matches, and one number (the smaller one?) is chosen. # What sort of plots could you make to visualize this?