# # this was our guesser function from class # guesser <- function(hidden,verbose=T) { g <- -1 i <- 0 repeat { g <- sample(1:100,1) i <- i+1 if (verbose == T) {cat("My guess now is",g,"\n")} if (g == hidden) break } if (verbose == T) { cat("I got it! Your # was", g, ".") cat("\nIt took",i,"guesses.\n") } return(i) } " Below is a function that runs ONE trial of the Monty Hall three-curtain game from the show 'Let's Make a Deal' Run this with the command > MH() " MH <- function() { # get the winning curtain into prize prize = sample(1:3,1) cat("Aside: [[ The prize is behind",prize,"]]\n") # get the user's curtain into user cat("Welcome! Choose a curtain: 1, 2, or 3:\n") user <- scan(n=1) # this is how to get one input cat("OK, you chose curtain", user,"\n") # get the garbage curtain into g g = 6-user-prize # trick to getting garbage curtain # that trick needs to be fixed if prize == user if (prize == user) { g = prize+1 # if g gets too big, wrap around if (g > 3) { g = 1 } } # chatter from Monty Hall cat("Aha! It's good you didn't choose curtain",g,"\n") cat("Because that had a consolation prize...\n") cat("Do you want to switch or stay?\n") cat("You may type \"switch\" or \"stay\":\n") sors <- scan(what=" ",n=1) # this takes _text_ input # Let's find the other curtain switch_curtain = 6-prize-g cat("You choose to",sors,".\n") if (sors == "stay") { cat("Your curtain is still", user, "\n") } else { cat("Your curtain is now", switch_curtain,"\n") } # Announce the prize curtain cat("The prize was behind curtain", prize, "... ") # Now, we decide whether it's a win or loss # Losing cases if ((sors == "switch" & user==prize) | # or (sors == "stay" & user!=prize)) { cat("You LOSE!\n") return(0) } # winning cases else { cat("You WIN!\n") return(1) } }