執行一回合的函式
現在,讓我們使用上一個練習中的函式,撰寫一個名為 evaluate_first_roll 的整體函式,來完成 Craps 的一個回合。如下方範例程式碼所示,此函式會將射手的第一次擲骰作為輸入,並據此往下進行。roll_dice 與 roll_after_point 函式已為你預先載入。
本練習屬於課程
R 的機率謎題
練習說明
- 使用
%in%運算子檢查擲出的點數是否為 7 或 11,若是則立即獲勝。 - 使用
%in%運算子檢查擲出的點數是否為 2、3 或 12,若是則立即失敗。 - 若建立了 point(點數),則持續擲骰,直到再次擲出該點數(獲勝),或擲出 7(失敗)。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
evaluate_first_roll <- function(roll){
# Check whether the first roll gives an immediate win
if(___){
won <- TRUE
}
# Check whether the first roll gives an immediate loss
if(___){
won <- FALSE
}
if(roll %in% c(4,5,6,8,9,10) ){
# Roll until the point or a 7 is rolled and store the win/lose outcome
won <- ___
}
return(won)
}