In the simple dice game called ‘Greedy’, you roll the dice as many times as you like, accumulatng your score. But if you get too greedy and don’t stop before you roll a 6, you lose everything!
In this simulation, you can set the following:
The point of the simulation is to demonstrate the best strategy to do well in this game.
Being too greedy (taking too much risk) results in poor reward.
A single game is simulated with the following code:
repeat{
roll <- sample(1:6,1) #Dice roll
if (roll == 6) { # Greedy, game over, score is zero
score <- 0
break
} else {
score <- score + roll # Accumulate score
}
if ((!beatPrevious & score >= risk) | (score >= risk & score > best)){
break
}
}
This code is within the greedyResults function
A call to this function, with a risk (minumum score aimed for) of 12 and 10 simulations:
greedyResults(12, 10, FALSE)
## [1] 0 12 0 12 0 13 13 0 12 12
In the shiny app, the results are:
set.seed(123)
gScores <- greedyResults(12, 50, FALSE)
# Form data.frame with scores
greedyScores <- data.frame(score = gScores)
# Add end outcome
greedyScores$Outcome <- ifelse(greedyScores$score == 0, "Greedy", "Winner")
library(ggplot2)
meanscore <- mean(greedyScores$score)
ggplot(greedyScores, aes(x = factor(score), fill = Outcome)) +
geom_bar() +
scale_fill_manual(values=c("#BB2222", "#22BB22")) +
labs(title = paste("Scores for", 50, "simulated games"), x = "Score", y = "Count", subtitle = paste("Mean score = ", meanscore)) +
theme(plot.title = element_text(size = 18, face = "bold"), plot.subtitle = element_text(size = 14))