######################################################################
#
# 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 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. #####
##### Questions #####
# What is the mean number of rolls?
# What proportion of the time do you get Yahtzee in three or fewer rolls?
# 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.
# Add a vertical line at the mean value to your histogram
##### 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).
# Make a scatterplot of total rolls vs. total rolls of individual dice
##### 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?