If, else if, else statements and logical operators in R and creating functions

ts3n_mt

I have worked on this for two days and simply I am stuck in the mud! I am working on using if, else if and else statements in R

I have created a function to draw 3 random cards for two players to simulate a game

face=c("king", "queen", "jack", "ten", "nine", "eight", "seven", "six", "five", "four", "three", "two", "ace")
value=c(13, 12,11,10,9,8,7,6,5,4,3,2,1) 
deck <-data.frame(face=rep(face,4),
                  suit=c(rep("spades", 13), rep("clubs", 13), rep("diamonds", 13), rep("hearts", 
                  13)),
                  value=rep(value,4)) 

This is the function I created to get their hands

get_cards<-function(){
  Player.A<-draw_n_random_cards(deck, 3)
  Player.B<-draw_n_random_cards(deck, 3)
}

1 I added up the values of each player's hand to get their scores

Sum.Player.A<-sum(Player.A$value)
Sum.Player.B<-sum(Player.B$value)

2 If all the cards in the hand are the same suit (all hearts) their sum will be multiplied by 2 #3 If all the cards are the same suit (all aces) there sum will be multiplied by 2 also Ok, so I created a logical test for my if, else if, else statement

combo.1=c("ace", "ace", "ace")
combo.2=c("heart", "heart", "heart")

This is my if statement

if (Sum.Player.A==combo.1){
  Sum.Player.A<-Sum.Player.A*2
}else{
  Sum.Player.A
}

if(Sum.Player.A==combo.2){
  Sum.Player.A<-Sum.Player.A*2
}else{
  Sum.Player.A
}

if (Sum.Player.B==combo.1){
  Sum.Player.B<-Sum.Player.B*2
}else{
  Sum.Player.B
}

if(Sum.Player.B==combo.2){
  Sum.Player.B<-Sum.Player.B*2
}else{
  Sum.Player.B
}

My end result is to write a function that displays each player's hand and declares a winner.

winner<-function(){
if(Sum.Player.A<Sum.Player.B){
          "Player B is the winner"
        }else if (Sum.Player.A>Sum.Player.B){
          "Player A is the winner"}else {
           "tie"}
}

So my trouble is putting this sequence of functions into a single function to play this game I created. If I create a function called

play_game<-function(){

#1 draw 3 random cards for each player
#2 score their cards
#3 compare their score to delcare a winner
}

This is where I have been stuck. I am looking for direction on this question.

Stacker

I added stringsAsFactors=FALSE to the creation of the deck. Draw n random cards draws cards at once, otherwise you risk drawing the same cards (it needs to be done without replacement). Lastly, the winner function takes the sum of A's hand and the sum of B's hand as arguments.

face=c("king", "queen", "jack", "ten", "nine", "eight", "seven", "six", "five", "four", "three", "two", "ace")
value=c(13, 12,11,10,9,8,7,6,5,4,3,2,1) 
deck <-data.frame(face=rep(face,4),
                  suit=c(rep("spades", 13), rep("clubs", 13), rep("diamonds", 13), rep("hearts", 
                                                                                       13)),
                  value=rep(value,4), stringsAsFactors = FALSE) 

get_cards<-function(){
  return(draw_n_random_cards(6))
}

draw_n_random_cards=function(n) {
  s=sample(1:52, 6, replace=FALSE)
  return(deck[s,])
}

winner<-function(A, B){
  if(A<B){
    "Player B is the winner"
  }else if (A>B){
    "Player A is the winner"}else {
      "tie"}
}

play_game=function() {
  cards=draw_n_random_cards()
  Player.A=cards[1:3,]
  Player.B=cards[4:6,]
  Sum.Player.A<-sum(Player.A$value)
  Sum.Player.B<-sum(Player.B$value)
  
  combo.1=c("ace", "ace", "ace")
  combo.2=c("heart", "heart", "heart")
  
  if (identical(Player.A$face, combo.1) | identical(Player.A$suit, combo.2)) {
    Sum.Player.A<-Sum.Player.A*2
  }
  if (identical(Player.B$face, combo.1) | identical(Player.B$suit, combo.2)) {
    Sum.Player.B<-Sum.Player.B*2
  }
  winner(Sum.Player.A, Sum.Player.B)
}

On one play of the game:

> play_game()
[1] "Player B is the winner"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

IF-THEN-ELSE statements in postgresql

From Dev

Return a function for IF ELSE IF statements

From Dev

Using namespace in if / else statements

From Dev

Angularjs if/else statements

From Dev

If else statements with size of split

From Dev

Is there an alternative to using IF / ELSE statements

From Dev

Pythonic alternatives to if/else statements

From Dev

Expressing If then else via Logical && and ||

From Dev

Prolog without if and else statements

From Dev

One 'else' for nested 'if' statements

From Dev

Matlab: Remove IF-ELSE Conditional statements using Logical Indexing

From Dev

QUEUE and 'IF' 'ELSE' 'WHILE' statements from scratch and running functions

From Dev

(Beginners Python) Creating if/else statements dependent on user input?

From Dev

Haskell if then else with "two statements"

From Dev

If/Else Logical Flow

From Dev

Working with multiple if else statements

From Dev

Using multiple else if statements

From Dev

Alternative to if-then-else with logical variables in R

From Dev

Creating functions in Python with "if else" statements and named constants

From Dev

If Else VS Multiple If Statements

From Dev

Bash script - if and else if statements

From Dev

Multiple if and else statements

From Dev

Operators in If- Else Condition

From Dev

Laravel Query with if/else operators

From Dev

If and if else statements working, but not else

From Dev

Ternary Operators for else if

From Dev

If/Else Logical Flow

From Dev

Issues creating an if else statement for rounding with a loop in R

From Dev

IF statements made redundant by ELSE statements