Little useless-useful R functions – Play rock-paper-scissors with your R engine

[This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Naaah! 🙂 not a joke. But quarantine restrictions are getting tighter and you might want to spent playing some useless game with your R engine, whilst programming, doing machine learning or other learning.

Source: Creative Commons

Without complications, the simplest version of this game is to play against R Engine. Here is how to:

##### Input bet as a function
play_RPS <- function(bet) {
  bets <- c("R","P", "S") 
  if(bet %in% bets){
     
  solution_df <- data.frame(combo=c("RP", "PR", "PS", "SP", "RS", "SR", "PP", "RR", "SS"), win = c("01","10", "01","10", "10", "01", "00","00","00") )
  REngine <- sample(bets,1)  
  combo <- paste0(REngine,bet, collapse="")
  res <-solution_df[ which(solution_df$combo==combo),2]
  if (res=="10"){
    print(paste0("You lost. Computer draw: ", REngine), collapse="")
  } else if(res=="00"){
    print(paste0("It's a tie! Computer draw: ", REngine), collapse="")
    }else {
    print(paste0("You win! Computer draw: ", REngine), collapse="")
    }
   }
  else {
    print("Please input valid bet!")
  }
}

And by running the function with your bet:

play_RPS("R")

Getting the results (in my case / run).

Game-play could use some refinement. I have decided to use x11 function, I have already mentioned it in one of my previous posts. In this case, I will be building a loop with x11 function for continuous selection of Rock-Paper-Scissors. With x11 you can get little bit better interface:

As always, the code is available at Github.

Happy R-coding! And stay healthy!

To leave a comment for the author, please follow the link and comment on their blog: R – TomazTsql.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)