【问题标题】:If, else if, else statements and logical operators in R and creating functionsR 中的 if、else if、else 语句和逻辑运算符以及创建函数
【发布时间】:2021-05-30 16:25:09
【问题描述】:

我已经为此工作了两天,只是我陷入了困境!我正在研究在 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
}

这是我一直卡住的地方。我正在寻找这个问题的方向。

【问题讨论】:

    标签: r dataframe if-statement logical-operators notation


    【解决方案1】:

    我在创建卡片组时添加了 stringsAsFactors=FALSE。抽 n 张随机牌一次抽牌,否则您可能会抽到相同的牌(需要在不更换的情况下完成)。最后,获胜者函数将 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"
    

    【讨论】:

    • 这是有道理的。您创建了一个获取卡片的函数,该函数将返回 6 张卡片。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 2018-03-12
    • 1970-01-01
    相关资源
    最近更新 更多