######################################################################
#
# In the game Trouble the goal is to move a bunch of markers from a
# starting point all the way to the ending point. Ignoring some
# subtleties of the game, the basic move is very simple:
#
# - You roll a standard 6-sided die.
# - You move your marker the number of spaces equal to the value
# on the die.
# - If you rolled a six, you roll and move again. You keep doing
# this as a long as you roll sixes.
#
# For example, if you roll a 3, then you move 3 spaces. If you roll
# a 6 followed by a 3, then you move 6 + 3 = 9 spaces. If you roll a
# 6, another 6, and then a 3, then you move 6 + 6 + 3 = 15 spaces.
#
######################################################################
##### Simulating Trouble #####
# Prite some code that simulates a move in the game Trouble.
# Print the number of spaces moved.
move <- function(){
roll <- sample(1:6, 1)
total <- roll
while(roll == 6){
roll <- sample(1:6, 1)
total <- total + roll
}
total
}
move()
##### What is the average length of a move? #####
#one way to do it
sim <- function(){
sum <- 0
M <- 1000000
for(i in 1:M){
sum <- sum + move()
}
sum/M #this is the average length
}
sim()
system.time(sim())
#another way
M <- 1000000
mean(replicate(M, move()))
system.time(mean(replicate(M, move())))
#could maybe use apply(), but this isn't great because move() takes no arguments
##### What happens if you change the roll-again value? #####
# For example, f you change the rules so that you roll again after rolling
# a 1, what is the average length of a move?
##### How does the roll-again value affect the chance of winning? #####
# Suppose you are playing a head-to-head game with another person.
# You use the "go again on 6" rule, and the other person uses the "go
# again on 1" rule. You both roll your die to complete a move.
# The winner is who ever has a the larger move.
# (If there is a tie, repeat until there is a winner.)
# Is this a fair game, in the sense that both players have an equal probability of winning?
# If not, who has the advantage, and what is the corresponding probability?