Jupyter-ITVitae/pokémon.ipynb

230 lines
6.1 KiB
Plaintext
Raw Permalink Normal View History

2024-03-07 12:26:30 +01:00
{
"cells": [
{
"cell_type": "code",
2024-03-08 10:36:51 +01:00
"execution_count": null,
2024-03-07 12:26:30 +01:00
"metadata": {},
2024-03-08 10:36:51 +01:00
"outputs": [],
2024-03-07 12:26:30 +01:00
"source": [
"import polars as ps\n",
"\n",
"pokémon = ps.read_csv(\"./pokémon/Pokemon.csv\")\n",
"pokémon"
]
},
{
"cell_type": "code",
2024-03-08 10:36:51 +01:00
"execution_count": null,
2024-03-07 12:26:30 +01:00
"metadata": {},
2024-03-08 10:36:51 +01:00
"outputs": [],
2024-03-07 12:26:30 +01:00
"source": [
"pokémon.sort(ps.col(\"HP\")).tail(1)"
]
},
{
"cell_type": "code",
2024-03-08 10:36:51 +01:00
"execution_count": null,
2024-03-07 12:26:30 +01:00
"metadata": {},
2024-03-08 10:36:51 +01:00
"outputs": [],
2024-03-07 12:26:30 +01:00
"source": [
"from polars import col\n",
"\n",
"pokémon.select(ps.col(\"Type 2\").fill_null(ps.col(\"Type 1\")))\n",
"pnn = pokémon.with_columns([\n",
" ps.when(col(\"Type 2\").is_null()).then(col(\"Type 1\")).otherwise(col(\"Type 2\")).alias(\"Type 2\")\n",
"])\n",
"\n",
"pnn"
]
},
{
"cell_type": "code",
2024-03-08 10:36:51 +01:00
"execution_count": null,
2024-03-07 12:26:30 +01:00
"metadata": {},
2024-03-08 10:36:51 +01:00
"outputs": [],
2024-03-07 12:26:30 +01:00
"source": [
"gen1 = pnn.filter(col(\"Generation\") == 1)\n",
"gen1"
]
},
{
"cell_type": "code",
2024-03-08 10:36:51 +01:00
"execution_count": null,
2024-03-07 12:26:30 +01:00
"metadata": {},
2024-03-08 10:36:51 +01:00
"outputs": [],
2024-03-07 12:26:30 +01:00
"source": [
"no_megas = pnn.filter(~col(\"Name\").str.contains(\"Mega \"))\n",
"no_megas"
]
},
{
"cell_type": "code",
2024-03-08 10:36:51 +01:00
"execution_count": null,
2024-03-07 12:26:30 +01:00
"metadata": {},
2024-03-08 10:36:51 +01:00
"outputs": [],
2024-03-07 12:26:30 +01:00
"source": [
"from plotnine import ggplot, aes, geom_histogram, labs\n",
"\n",
"(\n",
" ggplot(no_megas.to_pandas())\n",
" + aes(x=\"Attack\") \n",
" + geom_histogram(bins=(200/25))\n",
" + labs(title=\"Distribution of attack stat for all Pokémon\", y=\"Amount of Pokémon\")\n",
")\n"
]
},
{
"cell_type": "code",
2024-03-08 10:36:51 +01:00
"execution_count": null,
2024-03-07 12:26:30 +01:00
"metadata": {},
2024-03-08 10:36:51 +01:00
"outputs": [],
2024-03-07 12:26:30 +01:00
"source": [
"only_fairy = pnn.filter(col(\"Type 1\").str.contains(\"Fairy\") | col(\"Type 2\").str.contains(\"Fairy\"))\n",
"\n",
"(\n",
" ggplot(only_fairy.to_pandas())\n",
" + aes(x=\"Generation\") \n",
" + geom_histogram(bins=(6), color='#EF9FEF', fill='#EF6FEF')\n",
" + labs(title=\"Amount of fairy Pokémon in each generation\", y=\"Amount of Pokémon\")\n",
")"
]
2024-03-07 15:31:20 +01:00
},
{
"cell_type": "code",
2024-03-08 10:36:51 +01:00
"execution_count": null,
2024-03-07 15:31:20 +01:00
"metadata": {},
"outputs": [],
"source": [
"types = pokémon.select([\"Type 1\"]).unique().to_series().to_list()"
]
},
{
"cell_type": "code",
2024-03-08 10:36:51 +01:00
"execution_count": null,
2024-03-07 15:31:20 +01:00
"metadata": {},
2024-03-08 10:36:51 +01:00
"outputs": [],
2024-03-07 15:31:20 +01:00
"source": [
"pokétype = ps.read_csv(\"./pokémon/Pokemon Type Chart.csv\")\n",
"pokétype"
]
},
{
"cell_type": "code",
2024-03-08 10:36:51 +01:00
"execution_count": null,
2024-03-07 15:31:20 +01:00
"metadata": {},
"outputs": [],
"source": [
"from dataclasses import dataclass\n",
"from enum import Enum\n",
"from typing import TypeVar\n",
"\n",
"TPokémon = TypeVar(\"TPokémon\", bound=\"Pokémon\")\n",
"\n",
"pokémon_list: list = []\n",
"\n",
"class Effective(Enum):\n",
" \"\"\"\n",
" Enum Description: \n",
" \"\"\"\n",
" SUPER = \"It's super effective!\"\n",
" NORMAL = \"\"\n",
" NOTVERY = \"It's not very effective...\"\n",
" NOT = \"But it has no effect!\"\n",
"\n",
"\n",
"@dataclass\n",
"class Pokémon:\n",
" name: str\n",
2024-03-08 10:36:51 +01:00
" types: list[str]\n",
2024-03-07 15:31:20 +01:00
" hp: int\n",
" curr_hp: int\n",
" attack: int\n",
" defense: int\n",
" sp_atk: int\n",
" sp_def: int\n",
"\n",
" def attack_opponent(self, opponent: TPokémon, typeatk: str, atkpwr: int):\n",
" if opponent.curr_hp == 0:\n",
" print(\"You cannot attack a fainted Pokémon!\")\n",
" return\n",
"\n",
" type_modifier = pokétype.filter(pokétype[\"\"].str.contains(typeatk))[opponent.types[0]][0]\n",
" if (opponent.types[1] is not None):\n",
" type_modifier *= pokétype.filter(pokétype[\"\"].str.contains(typeatk))[opponent.types[1]][0]\n",
" \n",
" damage = self.attack * type_modifier * (atkpwr / 100)\n",
" \n",
"\n",
" if(typeatk in self.types):\n",
" damage = damage * 1.5\n",
" opponent.curr_hp = opponent.curr_hp - damage\n",
2024-03-08 10:36:51 +01:00
" print(f\"{self.name} attacks {opponent.name} for {int(damage)} damage!\")\n",
" print(f\"{Effective.SUPER.value if type_modifier > 1.0 else Effective.NORMAL.value}\")\n",
" print(f\"{Effective.NOTVERY.value if ((type_modifier < 1.0) and (type_modifier > 0.0)) else Effective.NORMAL.value}\")\n",
" print(f\"{Effective.NOT.value if type_modifier == 0.0 else Effective.NORMAL.value}\")\n",
2024-03-07 15:31:20 +01:00
"\n",
" if (opponent.curr_hp <= 0):\n",
" opponent.curr_hp = 0\n",
" print(f\"{opponent.name} fainted!\")\n",
" return\n",
" print(f\"It now has {int(opponent.curr_hp)} HP\")\n",
"\n",
" def heal(self):\n",
" self.curr_hp = self.hp\n",
"\n",
"for row in pokémon.rows(named=True):\n",
" pokémon_list.append(Pokémon(\n",
" name = row[\"Name\"],\n",
" types = [row[\"Type 1\"], row[\"Type 2\"]],\n",
" hp = row[\"HP\"],\n",
" curr_hp = row[\"HP\"],\n",
" attack = row[\"Attack\"],\n",
" defense = row[\"Defense\"],\n",
" sp_atk = row[\"Sp. Atk\"],\n",
" sp_def = row[\"Sp. Def\"],\n",
2024-03-08 10:36:51 +01:00
" ))\n"
2024-03-07 15:31:20 +01:00
]
},
{
"cell_type": "code",
2024-03-08 10:36:51 +01:00
"execution_count": null,
2024-03-07 15:31:20 +01:00
"metadata": {},
2024-03-08 10:36:51 +01:00
"outputs": [],
2024-03-07 15:31:20 +01:00
"source": [
2024-03-08 10:36:51 +01:00
"pokémon_list[0].attack_opponent(pokémon_list[256], \"Poison\", 30)"
2024-03-07 15:31:20 +01:00
]
},
{
"cell_type": "code",
2024-03-08 10:36:51 +01:00
"execution_count": null,
2024-03-07 15:31:20 +01:00
"metadata": {},
"outputs": [],
"source": [
"for poke in pokémon_list:\n",
" poke.heal()"
]
2024-03-07 12:26:30 +01:00
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}