DEV Community

Discussion on: Daily Challenge #170 - Pokemon Damage Calculator

Collapse
 
craigmc08 profile image
Craig McIlwrath

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.

data Type = Grass | Fire | Water | Electric deriving (Eq)

effect :: Type -> Type -> Double
effect Fire Grass = 2
effect Water Fire = 2
effect Fire Electric = 1
effect Grass Water = 2
effect Electric Water = 2
effect Grass Electric = 1
effect a b
  | a == b    = 1
  | otherwise = recip $ effect b a

totalDamage :: Type -> Type -> Double -> Double -> Double -> Double
totalDamage t1 t2 att def = (*) $ att / def * effect t1 t2