diff --git a/WeeklyChallenge2022.playground/Pages/Challenge35.xcplaygroundpage/Contents.swift b/WeeklyChallenge2022.playground/Pages/Challenge35.xcplaygroundpage/Contents.swift index cb10e54..a0eab76 100644 --- a/WeeklyChallenge2022.playground/Pages/Challenge35.xcplaygroundpage/Contents.swift +++ b/WeeklyChallenge2022.playground/Pages/Challenge35.xcplaygroundpage/Contents.swift @@ -24,3 +24,65 @@ import Foundation * https://retosdeprogramacion.com/semanales2022. * */ + +enum PokemonType { + Fuego = "Fuego", + Agua = "Agua", + Electicidad = "Electicidad", + Planta = "Planta", +} +type Efectividad = { + [key in PokemonType]: number; +}; + +type map = { + [key in PokemonType]: Efectividad; +}; + + +const TABLADEEFECTIVIDAD: map = { + [PokemonType.Fuego]: { + [PokemonType.Fuego]: 0.5, + [PokemonType.Agua]: 0.5, + [PokemonType.Electicidad]: 1, + [PokemonType.Planta]: 2 + }, + [PokemonType.Agua]: { + [PokemonType.Fuego]: 2, + [PokemonType.Agua]: 0.5, + [PokemonType.Electicidad]: 0.5, + [PokemonType.Planta]: 0.5 + }, + [PokemonType.Electicidad]: { + [PokemonType.Fuego]: 1, + [PokemonType.Agua]: 2, + [PokemonType.Electicidad]: 0.5, + [PokemonType.Planta]: 1 + }, + [PokemonType.Planta]: { + [PokemonType.Fuego]: 0.5, + [PokemonType.Agua]: 2, + [PokemonType.Electicidad]: 1, + [PokemonType.Planta]: 0.5 + }, +} + +function Batalla(atacante: PokemonType, defensor: PokemonType, daño: number, defenza: number): number { + if ((daño > 100 || daño < 1) || (defenza > 100 || defenza < 1)) { + throw new DOMException("El daño y la defenza debe de estar entre 1 y 100"); + } + let dañoTotal: number; + dañoTotal = 50 * (daño / defenza) * TABLADEEFECTIVIDAD[atacante][defensor]; + return dañoTotal; +} + +console.log(Batalla(PokemonType.Agua, PokemonType.Fuego, 20, 50)); +console.log(Batalla(PokemonType.Agua, PokemonType.Agua, 1, 50)); +console.log(Batalla(PokemonType.Planta, PokemonType.Electicidad, 1, 25)); + +try { + console.log(Batalla(PokemonType.Agua, PokemonType.Fuego, 0, 50)); + console.log(Batalla(PokemonType.Agua, PokemonType.Fuego, 20, 0)); +} catch (error) { + console.log(error) +}