Setup
For this challenge, you'll be thrown into a Pokemon battle! Calculate the damage that a particular move would do using the following formula:
total damage
= base damage
* (attack
/ defense
) * effectiveness
base damage
= the original power of the attack
attack
= your attack stat
defense
= the opponent's defense stat
effectiveness
= the effectiveness of the attack based on the type matchup
Attacks can be super effective
, neutral
, or not very effective
depending on the matchup.
-
super effective
: 2x damage -
neutral
: 1x damage -
not very effective
: 0.5x damage
To prevent the challenge from being too tedious, you'll only deal with four types: fire, water, grass, and electric. Here are the matchups:
fire > grass fire < water fire = electric water < grass water < electric grass = electric **Any type against itself is not very effective
Overall, the function you implement must take in:
- your type
- the opponent's type
- attack power
- the opponent's defense
... and must output the total damage
of the attack.
Tests
What are the total damage
outputs of these attacks?
- "grass", "electric", 57, 19
- "grass", "water", 40, 40
- "grass", "fire", 35, 5
- "fire", "electric", 10, 2
Good luck!
This challenge comes from yaphi1 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (5)
As there's no mention of the value, I set the base damage to 50 in each scenario.
Code
Results
150
100
175
250
Ok cool. I'll use 50, too.
Here'a another Python implementation:
PHP ::)
Haskell solution. The last argument of the function is the base damage. I'm also changing the function to accept an ADT Type type instead of strings for the PokΓ©mon type.
Pattern matching for the win!
I didn't know what to do with the
base_power
thing, so I left it as one.Elixir