R 中的 if、else if、else 语句和逻辑运算符以及创建函数

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

我已经为此工作了两天,只是我陷入了困境!我正在研究在 R

中使用 if、else if 和 else 语句

我创建了一个功能,可以为两个玩家随机抽取 3 张牌来模拟游戏

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)) 

这是我为了得到他们的手而创建的功能

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

1 我把每个玩家手牌的值加起来得到他们的分数

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

2 如果手中的所有牌都是同花色(都是红心),则它们的总和将乘以 2 #3 如果所有牌都是同花色(都是 A),则总和也将乘以 2 好的,所以我为我的 if, else if, else 语句创建了一个逻辑测试

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

这是我的 if 语句

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
}

我的最终结果是编写一个函数来显示每个玩家的手牌并宣布获胜者。

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"}
}

所以我的麻烦是将这个函数序列放入一个函数中来玩我创建的这个游戏。 如果我创建一个名为

的函数
play_game<-function(){

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

这就是我被卡住的地方。我正在寻找关于这个问题的方向。

我在创建套牌时添加了 stringsAsFactors=FALSE。 Draw n random cards 一次抽牌,否则你有抽相同牌的风险(需要在不更换的情况下完成)。最后,winner 函数将 A 的手牌总和和 B 的手牌总和作为参数。

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)
}

在一场比赛中:

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