Jupyter-ITVitae/pokémon.ipynb

6.1 KiB

None <html lang="en"> <head> gitea_input128469645 </head>
In [ ]:
import polars as ps

pokémon = ps.read_csv("./pokémon/Pokemon.csv")
pokémon
In [ ]:
pokémon.sort(ps.col("HP")).tail(1)
In [ ]:
from polars import col

pokémon.select(ps.col("Type 2").fill_null(ps.col("Type 1")))
pnn = pokémon.with_columns([
    ps.when(col("Type 2").is_null()).then(col("Type 1")).otherwise(col("Type 2")).alias("Type 2")
])

pnn
In [ ]:
gen1 = pnn.filter(col("Generation") == 1)
gen1
In [ ]:
no_megas = pnn.filter(~col("Name").str.contains("Mega "))
no_megas
In [ ]:
from plotnine import ggplot, aes, geom_histogram, labs

(
    ggplot(no_megas.to_pandas())
    + aes(x="Attack") 
    + geom_histogram(bins=(200/25))
    + labs(title="Distribution of attack stat for all Pokémon", y="Amount of Pokémon")
)
In [ ]:
only_fairy = pnn.filter(col("Type 1").str.contains("Fairy") | col("Type 2").str.contains("Fairy"))

(
    ggplot(only_fairy.to_pandas())
    + aes(x="Generation") 
    + geom_histogram(bins=(6), color='#EF9FEF', fill='#EF6FEF')
    + labs(title="Amount of fairy Pokémon in each generation", y="Amount of Pokémon")
)
In [ ]:
types = pokémon.select(["Type 1"]).unique().to_series().to_list()
In [ ]:
pokétype = ps.read_csv("./pokémon/Pokemon Type Chart.csv")
pokétype
In [ ]:
from dataclasses import dataclass
from enum import Enum
from typing import TypeVar

TPokémon = TypeVar("TPokémon", bound="Pokémon")

pokémon_list: list = []

class Effective(Enum):
    """
    Enum Description: 
    """
    SUPER = "It's super effective!"
    NORMAL = ""
    NOTVERY = "It's not very effective..."
    NOT = "But it has no effect!"


@dataclass
class Pokémon:
    name: str
    types: list[str]
    hp: int
    curr_hp: int
    attack: int
    defense: int
    sp_atk: int
    sp_def: int

    def attack_opponent(self, opponent: TPokémon, typeatk: str, atkpwr: int):
        if opponent.curr_hp == 0:
            print("You cannot attack a fainted Pokémon!")
            return

        type_modifier = pokétype.filter(pokétype[""].str.contains(typeatk))[opponent.types[0]][0]
        if (opponent.types[1] is not None):
            type_modifier *= pokétype.filter(pokétype[""].str.contains(typeatk))[opponent.types[1]][0]
        
        damage = self.attack * type_modifier * (atkpwr / 100)
        

        if(typeatk in self.types):
            damage = damage * 1.5
        opponent.curr_hp = opponent.curr_hp - damage
        print(f"{self.name} attacks {opponent.name} for {int(damage)} damage!")
        print(f"{Effective.SUPER.value if type_modifier > 1.0 else Effective.NORMAL.value}")
        print(f"{Effective.NOTVERY.value if ((type_modifier < 1.0) and (type_modifier > 0.0))  else Effective.NORMAL.value}")
        print(f"{Effective.NOT.value if type_modifier == 0.0 else Effective.NORMAL.value}")

        if (opponent.curr_hp <= 0):
            opponent.curr_hp = 0
            print(f"{opponent.name} fainted!")
            return
        print(f"It now has {int(opponent.curr_hp)} HP")

    def heal(self):
        self.curr_hp = self.hp

for row in pokémon.rows(named=True):
    pokémon_list.append(Pokémon(
        name = row["Name"],
        types = [row["Type 1"], row["Type 2"]],
        hp = row["HP"],
        curr_hp = row["HP"],
        attack = row["Attack"],
        defense = row["Defense"],
        sp_atk = row["Sp. Atk"],
        sp_def = row["Sp. Def"],
    ))
In [ ]:
pokémon_list[0].attack_opponent(pokémon_list[256], "Poison", 30)
In [ ]:
for poke in pokémon_list:
    poke.heal()
</html>