I'm writing a function that can be used to play "rock, paper, scissors, lizard, spock". I use menu()
to ask the user to choose an option, sample()
to give the computer an option, and define a win as occurring if the two choices match one of 10 winning pairs. The code works (as in, doesn't throw errors), but produces unexpected output. I can't figure out why this doesn't work, I think it has to do with %in%
or perhaps a nuance of list
that I don't understand.
choices <- c("rock", "paper", "scissors", "lizard", "spock")
# Scissors [3] beats paper [2],
# paper [2] beats rock [1],
# rock [1] beats lizard [4],
# lizard [4] beats Spock [5],
# Spock [5] beats scissors [3],
# scissors [3] beats lizard [4],
# lizard [4] beats paper [2],
# paper [2] beats Spock [5],
# Spock [5] beats rock [1],
# rock [1] beats scissors [3].
wins <- list(c(3, 2), c(2, 1), c(1, 4), c(4, 5), c(5, 3), c(3, 4), c(4, 2), c(2, 5), c(5, 1), c(1, 3))
fun <- function(){
input <- menu(choices, title="Choose one:")
if (input == 0){
message("You have to pick a valid choice to play this. Try again.")
}
if (input >= 1 && input <= 5){
comp_choice <- sample(1:5, 1)
message(paste0("you chose ", choices[input]))
message(paste0("computer chose ", choices[comp_choice]))
print(list(c(input, comp_choice)) %in% wins)
if (input != comp_choice){
if (list(c(input, comp_choice)) %in% wins) {
message("you won")
}
else message("you lost")
}
else message("it's a draw")
}
}
fun()
Example of unexpected output (should be a win):
Selection: 3
you chose scissors
computer chose lizard
[1] FALSE
you lost