I am trying to create the functionality for a fight between Mewtwo and Rayquaza in R for a programming project. I am using "The Complete Pokemon Dataset" from Kaggle. I have successfully coded "round 1", but my problem is that when i attempt to loop over it until one Pokemon faints it just starts over. I want it to "remember" the HP of the Pokemon after it has suffered an attack. My code looks like this:
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!")
}
}
Fyi, the "damage" functions are coded like this:
Mhighdamage <- function(x){
pain=hpMewTwo-x/1.8
return(pain)
}
...where "hpMewTwo" is the HP for mewtwo given in the dataset(106). My output looks like this:
> 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!"
I attempted to put round 1 in a while loop, but it just kept on running through the code and then starting from the top with full HP forever. Does anyone have a clue on how i can run a loop that "remembers" the HP and runs until one hits zero?