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

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!

Tagged with: , , , ,
Posted in Uncategorized
4 comments on “Little useless-useful R functions – Play rock-paper-scissors with your R engine
  1. […] by data_admin [This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page […]

    Like

  2. […] Tomaz Kastrun asks, Shall we play a game?: […]

    Like

  3. […] article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report concern concerning the content material on […]

    Like

  4. […] article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page […]

    Like

Leave a comment

Follow TomazTsql on WordPress.com
Programs I Use: SQL Search
Programs I Use: R Studio
Programs I Use: Plan Explorer
Rdeči Noski – Charity

Rdeči noski

100% of donations made here go to charity, no deductions, no fees. For CLOWNDOCTORS - encouraging more joy and happiness to children staying in hospitals (http://www.rednoses.eu/red-noses-organisations/slovenia/)

€2.00

Top SQL Server Bloggers 2018
TomazTsql

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Discover WordPress

A daily selection of the best content published on WordPress, collected for you by humans who love to read.

Revolutions

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

tenbulls.co.uk

tenbulls.co.uk - attaining enlightenment with the Microsoft Data and Cloud Platforms with a sprinkling of Open Source and supporting technologies!

SQL DBA with A Beard

He's a SQL DBA and he has a beard

Reeves Smith's SQL & BI Blog

A blog about SQL Server and the Microsoft Business Intelligence stack with some random Non-Microsoft tools thrown in for good measure.

SQL Server

for Application Developers

Business Analytics 3.0

Data Driven Business Models

SQL Database Engine Blog

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Search Msdn

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

R news and tutorials contributed by hundreds of R bloggers

Data Until I Die!

Data for Life :)

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, SQL Server BI, Data Modeling, SSAS Design, Power Pivot, Power BI, SSRS Advanced Design, Power BI, Dashboards & Visualization since 2009

Grant Fritchey

Intimidating Databases and Code

Madhivanan's SQL blog

A modern business theme

Alessandro Alpi's Blog

DevOps could be the disease you die with, but don’t die of.

Paul te Braak

Business Intelligence Blog

Sql Insane Asylum (A Blog by Pat Wright)

Information about SQL (PostgreSQL & SQL Server) from the Asylum.

Gareth's Blog

A blog about Life, SQL & Everything ...