Skip to content

Instantly share code, notes, and snippets.

@pdparker
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdparker/9907514 to your computer and use it in GitHub Desktop.
Save pdparker/9907514 to your computer and use it in GitHub Desktop.
Rock Paper Scissors Lizard Spock
play <- function(){
possibilities <- c("rock", "Spock", "paper", "lizard", "scissors")
cat(possibilities, sep="\n")
cat("\n")
x <- readline("What is your choice? ")
if(x %in% possibilities == FALSE) {cat("FOOL! That is not one of the options")
x<- readline("Try Again: ")
}
y <- sample(possibilities, 1)
z <- which(possibilities==x)-which(possibilities==y)
if(x==y) {outcome <- "Draw"
}else{
outcome <- ifelse(z%%5==3|z%%5==2, "Win", "Lose")
}
cat("You choose", x,"\nComputer choose", y, "\nYou ",outcome, "\n")
}
rpsls <- function(){
again <- "yes"
outcomes <-c()
while (again=="yes"){
play()
again <- readline("Play again?")
}
}
rpsls()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment