DEV Community

Discussion on: Daily Challenge #170 - Pokemon Damage Calculator

Collapse
 
kerldev profile image
Kyle Jones • Edited

As there's no mention of the value, I set the base damage to 50 in each scenario.

Code

def compare_attack_types(first_type, second_type):
    if first_type == "fire":
        if second_type == "grass":
            return 2
        if second_type == "water":
            return 0.5
    if first_type == "grass":
        if second_type == "fire":
            return 0.5
        if second_type == "water":
            return 2
    if first_type == "electric":
        if second_type == "water":
            return 2
    if first_type == "water":
        if second_type == "fire":
            return 2
        if second_type == "grass":
            return 0.5
    return 1

def calculate_damage(base_damage, own_type, opponent_type, attack, defense):
    return base_damage * (attack / defense) * compare_attack_types(own_type, opponent_type)

print(calculate_damage(50, "grass", "electric", 57, 19))
print(calculate_damage(50, "grass", "water", 40, 40))
print(calculate_damage(50, "grass", "fire", 35, 5))
print(calculate_damage(50, "fire", "electric", 10, 2))

Results

150
100
175
250

Collapse
 
candidateplanet profile image
lusen / they / them 🏳️‍🌈🥑

Ok cool. I'll use 50, too.

Here'a another Python implementation:

SUPER_EFFECTIVE = 'super_effective'
NEUTRAL = 'neutral'
NOT_VERY_EFFECTIVE = 'not_very_effective'

EFFECTIVENESS_MULTIPLIER = {
  SUPER_EFFECTIVE: 2,
  NEUTRAL: 1,
  NOT_VERY_EFFECTIVE: .5}

FIRE = 'fire'
GRASS = 'grass'
WATER = 'water'
ELECTRIC = 'electric'

ATTACK_ON_DEFENSE_EFFECTIVENESS = {
  FIRE: {
    FIRE: NOT_VERY_EFFECTIVE,
    GRASS: SUPER_EFFECTIVE,
    WATER: NOT_VERY_EFFECTIVE,
    ELECTRIC: NEUTRAL,
  },
  GRASS: {
    GRASS: NOT_VERY_EFFECTIVE,
    WATER: SUPER_EFFECTIVE,
    ELECTRIC: NEUTRAL,
    FIRE: NOT_VERY_EFFECTIVE
  },
  WATER: {
    WATER: NOT_VERY_EFFECTIVE,
    GRASS: NOT_VERY_EFFECTIVE,
    ELECTRIC: NOT_VERY_EFFECTIVE,
    FIRE: SUPER_EFFECTIVE
  },
  ELECTRIC: {
    ELECTRIC: NOT_VERY_EFFECTIVE,
    GRASS: NEUTRAL,
    WATER: SUPER_EFFECTIVE,
    FIRE: SUPER_EFFECTIVE
  }
}

def _calculate_effectiveness(attack_type, defense_type):
  effectiveness_name = ATTACK_ON_DEFENSE_EFFECTIVENESS[attack_type][defense_type]
  return EFFECTIVENESS_MULTIPLIER[effectiveness_name]

def _total_damage(base_damage, attack, defense, effectiveness):
  return base_damage * (attack / defense) * effectiveness

def calculate_damage(base_damage, attack_type, defense_type, attack_power, defense_power):
  return _total_damage(
    base_damage,
    attack_power,
    defense_power,
    _calculate_effectiveness(attack_type, defense_type)
  )

print(calculate_damage(50, "grass", "electric", 57, 19))
print(calculate_damage(50, "grass", "water", 40, 40))
print(calculate_damage(50, "grass", "fire", 35, 5))
print(calculate_damage(50, "fire", "electric", 10, 2))