在確立 point 後持續擲骰的函式
現在來撰寫一個函式,用來處理在確立 point 之後的所有擲骰。也就是說,它應該持續擲骰,直到再次擲出該 point,或是擲出 7。發生其中任一情況後就應該停止,並回報結果是贏還是輸。
注意:在這個函式中,我們會再次使用先前定義的 roll_dice 函式。
本練習屬於課程
R 的機率謎題
練習說明
- 在 while 迴圈中填入條件,讓擲骰持續進行,直到擲出 7 或擲出 point。
- 填入條件,以檢查最新一次的擲骰是否代表贏。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
roll_after_point <- function(point){
new_roll <- 0
# Roll until either a 7 or the point is rolled
while( ___ ){
new_roll <- roll_dice(2)
if(new_roll == 7){
won <- FALSE
}
# Check whether the new roll gives a win
if(___ == ___){
won <- TRUE
}
}
return(won)
}