【发布时间】: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 <- 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。关于如何解决这个问题的任何想法?