Pokemon battle exercise

PHOTO EMBED

Thu Nov 10 2022 10:42:53 GMT+0000 (Coordinated Universal Time)

Saved by @ericggDev #kotlin

/*BATALLA POKÉMON
 * Fecha publicación enunciado: 29/08/22
 * Fecha publicación resolución: 06/09/22
 * Dificultad: MEDIA
 *
 * Enunciado: Crea un programa que calcule el daño de un ataque durante una batalla Pokémon.
 * - La fórmula será la siguiente: daño = 50 * (ataque / defensa) * efectividad
 * - Efectividad: x2 (súper efectivo), x1 (neutral), x0.5 (no es muy efectivo)
 * - Sólo hay 4 tipos de Pokémon: Agua, Fuego, Planta y Eléctrico (buscar su efectividad)
 * - El programa recibe los siguientes parámetros:
 *  - Tipo del Pokémon atacante.
 *  - Tipo del Pokémon defensor.
 *  - Ataque: Entre 1 y 100.
 *  - Defensa: Entre 1 y 100.
 */

private fun main() {
        println(battle(PokemonType.WATER, PokemonType.FIRE, 50, 30))
        println(battle(PokemonType.WATER, PokemonType.FIRE, 101, -10))
        println(battle(PokemonType.FIRE, PokemonType.WATER, 50, 30))
        println(battle(PokemonType.FIRE, PokemonType.FIRE, 50, 30))
        println(battle(PokemonType.GRASS, PokemonType.ELECTRIC, 30, 50))
    }

    private fun battle(attacker: PokemonType, defender: PokemonType, attack: Int, defense: Int): Double? {

        if (attack <= 0 || attack > 100 || defense <= 0 || defense > 100) {
            println("Ataque o defensa incorrecto")
            return null
        }

        val typeEffects = mapOf(
            PokemonType.WATER to PokemonChart(PokemonType.FIRE, PokemonType.GRASS),
            PokemonType.FIRE to PokemonChart(PokemonType.GRASS, PokemonType.WATER),
            PokemonType.GRASS to PokemonChart(PokemonType.WATER, PokemonType.FIRE),
            PokemonType.ELECTRIC to PokemonChart(PokemonType.WATER, PokemonType.GRASS)
        )

        val effectivity = when {
            (attacker == defender || typeEffects[attacker]?.notEffective == defender) -> {
                println("No es muy efectivo")
                0.5
            }
            (typeEffects[attacker]?.effective == defender) -> {
                println("Es súper efectivo")
                2.0
            }
            else -> {
                println("Es neutro")
                1.0
            }
        }

        return 50 * attack.toDouble() / defense.toDouble() * effectivity
    }
    
        enum class PokemonType {
        WATER,
        FIRE,
        GRASS,
        ELECTRIC
    }

    private data class PokemonChart(val effective: PokemonType, val notEffective: PokemonType)
content_copyCOPY