As you can see, when I am calculating the damage dealt I repeat the typing of the Pokemon. Just wondering if there is a simpler way of writing this code. I was wondering if you could use a table of some sort but am not sure how that can be implemented. I have provided the majority of my class just in case.
class Pokemon:
def __init__(self, name, types, max_health, health, attack, defense):
self.name = name
self.types = types
self.max_health = max_health
self.health = health
self.attack = attack
self.defense = defense
#Calculates the damage output depending on their attack and defense attributes
def move(self, opponent):
raw_dmg = 0
choice = int(input("Choose your move: "))
if choice == 1:
raw_dmg = int((self.attack / opponent.defense) * 25)
elif choice == 2:
if accuracy(70):
raw_dmg = int((self.attack / opponent.defense) * 40)
else:
raw_dmg = 0
elif choice == 3:
if accuracy(20):
raw_dmg = int((self.attack / opponent.defense) * 200)
else:
raw_dmg = 0
#Increases or decereases the damage depending on the type of both pokemon
if self.types == "Fire":
if opponent.types == "Water":
raw_dmg = raw_dmg * 0.5
elif opponent.types == "Grass":
raw_dmg = raw_dmg * 2
elif self.types == "Water":
if opponent.types == "Grass":
raw_dmg = raw_dmg * 0.5
elif opponent.types == "Fire":
raw_dmg = raw_dmg * 2
elif self.types == "Grass":
if opponent.types == "Fire":
raw_dmg = raw_dmg * 0.5
elif opponent.types == "Water":
raw_dmg = raw_dmg * 2
elif self.types == "Light":
if opponent.types == "Dark":
raw_dmg = raw_dmg * 2
elif self.types == "Dark":
if opponent.types == "Light":
raw_dmg = raw_dmg * 2
#The final damage is randomised by multiplying it by a value between 0.8 and 1.2
final_dmg = round(raw_dmg * (random.randint(80, 120)) / 100)
print(final_dmg)
opponent.health -= final_dmg
if opponent.health <= 0:
opponent.health = 0
return final_dmg
CodePudding user response:
Whenever it's reasonably possible, it's often good to transform code into data:
Before:
if self.types == "Fire":
if opponent.types == "Water":
raw_dmg = raw_dmg * 0.5
elif opponent.types == "Grass":
raw_dmg = raw_dmg * 2
elif self.types == "Water":
if opponent.types == "Grass":
raw_dmg = raw_dmg * 0.5
elif opponent.types == "Fire":
raw_dmg = raw_dmg * 2
elif self.types == "Grass":
if opponent.types == "Fire":
raw_dmg = raw_dmg * 0.5
elif opponent.types == "Water":
raw_dmg = raw_dmg * 2
elif self.types == "Light":
if opponent.types == "Dark":
raw_dmg = raw_dmg * 2
elif self.types == "Dark":
if opponent.types == "Light":
raw_dmg = raw_dmg * 2
After:
# ahead of time in the global namespace
SUPER_EFFECTIVE = {
("Fire", "Grass"),
("Water", "Fire"),
...
}
NOT_VERY_EFFECTIVE = {
("Fire", "Water"),
("Water", "Grass"),
...
}
...
# in the move function
if (self.types, opponent.types) in SUPER_EFFECTIVE:
raw_dmg *= 2.0
elif (self.types, opponent.types) in NOT_VERY_EFFECTIVE:
raw_dmg *= 0.5
CodePudding user response:
Following from Dennis's answer, I would go a step further and make a dictionary where you can just lookup the multiplier according to the type combination. Then with .get(), you can use a default of 1. That makes the code inside the class flat instead of nested.
TYPE_MULTIPLIERS = {
0.5: [
("Fire", "Water"),
("Water", "Grass"),
("Grass", "Fire"),
],
2: [
("Fire", "Grass"),
("Water", "Fire"),
("Grass", "Water"),
("Light", "Dark"),
("Dark", "Light"),
],
}
_MULTIPLIER_LOOKUP = {
types: multiplier
for multiplier, type_combos in TYPE_MULTIPLIERS.items()
for types in type_combos}
# ... later
raw_dmg *= _MULTIPLIER_LOOKUP.get((self.types, opponent.types), 1)
CodePudding user response:
If you needed to scale the code to include more options, you could create a list in the class definition that corresponds to the type. I called it types here. Then create and an attack function that receives the two opponents as parameters.
if self.type == 'fire':
self.types = ['water','fire','grass']
if self.type == 'water':
self.types = ['grass','water','fire']
if self.type == 'grass':
self.types = ['fire','grass','water']
mod = [0.5,1,2]
and then the attack function:
def attack_dmg(pokemon, opponent):
return mod[pokemon.types[opponent.types]] * pokemon.attack
