Last active
August 29, 2015 13:57
-
-
Save pdparker/9907514 to your computer and use it in GitHub Desktop.
Rock Paper Scissors Lizard Spock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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