【问题标题】:Functionality for a Pokemon fight in R: how to loop over attacks until one faints?R中口袋妖怪战斗的功能:如何循环攻击直到晕倒?
【发布时间】:2021-12-14 07:44:06
【问题描述】:

我正在尝试为一个编程项目在 R 中为 Mewtwo 和 Rayquaza 之间的战斗创建功能。我正在使用来自 Kaggle 的“完整的口袋妖怪数据集”。我已经成功编写了“第 1 轮”,但我的问题是,当我尝试循环它直到一个口袋妖怪晕倒时,它才重新开始。我希望它在受到攻击后“记住”口袋妖怪的 HP。我的代码如下所示:

round1 <- function(){
  M<- sample(c("Psystrike", "Aura Sphere", "Ice Punch",
               "Solar Beam"))
  R <- sample(c("Outrage", "Dragon Ascent", "Crunch",
                "Brutal Swing"))
  
  for (i in c("Ready", "Set", "Go!")){
    
    print(i)
    Sys.sleep(1.2)
    
  } 
  
  if (speedM>speedR && M == "Ice Punch"){
    print(Rextremedamage(75))
    print("Ice Punch is hyper effective!")
  }
    
  if (speedM>speedR && M== "Psystrike"){
    print(Rdamage(100))
    print("Psystrike has normal efficiency.")
    }
  if (speedM>speedR && M== "Aura Sphere"){
    print(Rlowdamage(80))
    print("Aura Sphere is not very effective...")
  }
  if (speedM>speedR && M== "Solar Beam"){
    print(Rlowdamage(100))
    print("Solar Beam is not very effective...")
  }
  
  
    Sys.sleep(1.4)
  
  
  for (a in c("Rayquaza is angry...", "Go!")){
    
    print(a)
    Sys.sleep(1.2)
    
  }
  
  if (hpRayquaza >0 && R == "Outrage"){
    print(Mdamage(120))
    print("Outrage has normal efficiency.")
  }
  if (hpRayquaza >0 && R == "Dragon Ascent"){
    print(Mdamage(120))
    print("Dragon Ascent has normal efficiency.")
  }
  if (hpRayquaza >0 && R == "Crunch"){
    print(Mhighdamage(80))
    print("Crunch is super effective!")
  }
  if (hpRayquaza >0 && R == "Brutal Swing"){
    print(Mhighdamage(60))
    print("Brutal swing is super effective!")
  }
    
}

仅供参考,“损坏”函数的编码如下:

Mhighdamage <- function(x){
  pain=hpMewTwo-x/1.8
  return(pain)
}

...其中“hpMewTwo”是数据集 (106) 中给定的 mewtwo 的 HP。我的输出如下所示:

> round1()
[1] "Ready"
[1] "Set"
[1] "Go!"
        hp
1 87.97872
[1] "Aura Sphere is not very effective..."
[1] "Rayquaza is angry..."
[1] "Go!"
        hp
1 61.55556
[1] "Crunch is super effective!"

我试图将第 1 轮放在一个 while 循环中,但它只是继续运行代码,然后从顶部开始,永远保持满 HP。有没有人知道我如何运行一个“记住”HP并运行到一个零点的循环?

【问题讨论】:

  • 仅供参考,您可能希望将所有 print 调用替换为 message
  • 从我能从你的代码中获得的信息,我建议在你的损坏函数中运行assign(hpMewTwo, hpMewTwo-x/1.8),然后运行return(hpMewTwo)。这样下次在损坏函数中调用 hpMewTwo 时,它会记住分配的值。
  • 谢谢!我应该同时更改print 调用还是只更改说明有效性的调用?
  • 谢谢维策!我会试试看。我对 R 编程相当陌生,所以这些建议真的很有帮助。
  • @WietsedeVries 我认为我们正在做一些事情,但是当我运行remhp &lt;- function(x){ au=hpRayquaza-x/4.7 assign(hpRayquaza, hpRayquaza-x/4.7) return(hpRayquaza) } remhp(50) 时,我收到错误Error in assign(hpRayquaza, hpRayquaza - x/4.7) : invalid first argument。关于如何解决这个问题的任何想法?

标签: r function loops


【解决方案1】:

我猜你可以使用 while 来做到这一点。 一般来说,如果您想制作 ABM,我会建议您使用某种代理结构(例如 setClass(Class="Pokemon") 或简单的列表或向量) )类型的口袋妖怪。口袋妖怪可以有不同的字段(id,name,lvl,hp,attacks,type)然后构建一个对所有代理(口袋妖怪)通用的战斗功能,并接受输入poke1poke2而不是hpmewtown... 然后,您可以使用 listapply 之类的工具进行多场战斗,甚至可以通过将口袋妖怪采样到双人组来建立锦标赛。 提示 尽量保持函数的通用性和可重用性。

## Damage system
highdamage <- function(x){
  dmg=-x/0.8
  return(dmg)
}

extremedamage <- function(x){
  dmg=-x/0.5
  return(dmg)
}

lowdamage<- function(x){
  dmg=-x/2
  return(dmg)
}


damage<- function(x){
  dmg=-x/1
  return(dmg)
}
####

round1 <- function(hpRayquaza,hpMewtwo){

# START
  for (i in c("Ready", "Set", "Go!")){
    print(i)
    Sys.sleep(1)
  } 
# ALIVE
  while (hpRayquaza>0 && hpMewtwo>0){
# WHO ATTACKS FASTER
    speedM<-rnorm(1,mean = speedM_v,3)
    speedR<-rnorm(1,mean = speedR_v,3)
    M<- sample(c("Psystrike", "Aura Sphere", "Ice Punch",
                 "Solar Beam"),1)
    R <- sample(c("Outrage", "Dragon Ascent", "Crunch",
                  "Brutal Swing"),1)
    Sys.sleep(0.5)
    if(speedM > speedR ){  
      for(a in c("Mewtwo attacks...", "Go!")){
        print(a)
        Sys.sleep(1)
      }
      if(M == "Ice Punch"){
        dmg<-extremedamage(75)
        print("Ice Punch is hyper effective!")
      }
      if(M== "Psystrike"){
        dmg<-damage(100)
        print("Psystrike has normal efficiency.")
      }
      if(M== "Aura Sphere"){
        dmg<-lowdamage(80)
      print("Aura Sphere is not very effective...")
      }
      if(M== "Solar Beam"){
      dmg<-lowdamage(100)
      print("Solar Beam is not very effective...")
      }
    hpRayquaza <- (hpRayquaza + dmg)
    print(paste("hpRayquaza:", hpRayquaza))
    }
    if(speedM<speedR){  
  for(a in c("Rayquaza is angry...", "Go!")){
      print(a)
      Sys.sleep(1)
    }
     if(R == "Outrage"){
      dmg<-damage(120)
      print("Outrage has normal efficiency.")
    }
     if(R == "Dragon Ascent"){
      dmg<-damage(120)
      print("Dragon Ascent has normal efficiency.")
    }
     if(R == "Crunch"){
      dmg<-highdamage(80)
      print("Crunch is super effective!")
      }
     if(R == "Brutal Swing"){
      dmg<-highdamage(60)
      print("Brutal swing is super effective!")
       }
    hpMewtwo <- (hpMewtwo + dmg)
    print(paste("hpMewtwo:",hpMewtwo))
    }
    res<-c(hpMewtwo,hpRayquaza)
  }
  
  return(res)
}

## Some values

hpMewtwo = 300
hpRayquaza = 600
speedM_v = 10
speedR_v = 8

## safe as a result
result_round1<-round1(hpRayquaza=hpRayquaza,hpMewtwo = hpMewtwo)
result_round1[1]

如果您想分析战斗中的步骤,我会在回合函数开始时创建一个数据框,其中包含 hp_pke1, hp_pke2, akk, dmg

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    相关资源
    最近更新 更多