{ "cells": [ { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [], "source": [ "from abc import ABC, abstractmethod" ] }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [], "source": [ "class Hero:\n", " def __init__(self):\n", " self.positive_effects = []\n", " self.negative_effects = []\n", " \n", " self.stats = {\n", " \"HP\": 128,\n", " \"MP\": 42,\n", " \"SP\": 100,\n", " \n", " \"Strength\": 15,\n", " \"Perception\": 4,\n", " \"Endurance\": 8,\n", " \"Charisma\": 2,\n", " \"Intelligence\": 3,\n", " \"Agility\": 8,\n", " \"Luck\": 1\n", " } \n", " \n", " def get_positive_effects(self):\n", " return self.positive_effects.copy()\n", " \n", " def get_negative_effects(self):\n", " return self.negative_effects.copy()\n", " \n", " def get_stats(self):\n", " return self.stats.copy()" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'HP': 128, 'MP': 42, 'SP': 100, 'Strength': 15, 'Perception': 4, 'Endurance': 8, 'Charisma': 2, 'Intelligence': 3, 'Agility': 8, 'Luck': 1}\n", "[]\n", "[]\n" ] } ], "source": [ "h = Hero()\n", "print(h.get_stats())\n", "print(h.get_positive_effects())\n", "print(h.get_negative_effects())" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [], "source": [ "class AbstractEffect(Hero, ABC):\n", " def __init__(self, base):\n", " self.base = base\n", " \n", " @abstractmethod\n", " def get_positive_effects(self):\n", " pass\n", " \n", " @abstractmethod\n", " def get_negative_effects(self):\n", " pass\n", " \n", " @abstractmethod\n", " def get_stats(self):\n", " return self.base.get_stats()" ] }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [], "source": [ "class AbstractPositive(AbstractEffect, ABC):\n", " def get_positive_effects(self):\n", " return self.base.get_positive_effects() + [type(self).__name__]\n", " \n", " def get_negative_effects(self):\n", " return self.base.get_negative_effects()" ] }, { "cell_type": "code", "execution_count": 166, "metadata": {}, "outputs": [], "source": [ "class AbstractNegative(AbstractEffect, ABC):\n", " def get_positive_effects(self):\n", " return self.base.get_positive_effects()\n", " \n", " def get_negative_effects(self):\n", " return self.base.get_negative_effects() + [type(self).__name__]\n", " " ] }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [], "source": [ "class Berserk(AbstractPositive):\n", " def get_stats(self):\n", " stats = self.base.get_stats()\n", " stats[\"Strength\"] += 7\n", " stats[\"Endurance\"] += 7\n", " stats[\"Agility\"] += 7\n", " stats[\"Luck\"] += 7\n", " stats[\"Perception\"] -= 3\n", " stats[\"Charisma\"] -= 3\n", " stats[\"Intelligence\"] -= 3\n", " stats[\"HP\"] += 50\n", " return stats" ] }, { "cell_type": "code", "execution_count": 168, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'HP': 128, 'MP': 42, 'SP': 100, 'Strength': 22, 'Perception': 1, 'Endurance': 15, 'Charisma': -1, 'Intelligence': 0, 'Agility': 15, 'Luck': 8}\n", "['Berserk']\n", "[]\n" ] } ], "source": [ "h = Berserk(h)\n", "print(h.get_stats())\n", "print(h.get_positive_effects())\n", "print(h.get_negative_effects())" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [], "source": [ "class Blessing(AbstractPositive):\n", " def get_stats(self):\n", " stats = self.base.get_stats()\n", " stats[\"Strength\"] += 2\n", " stats[\"Endurance\"] += 2\n", " stats[\"Agility\"] += 2\n", " stats[\"Luck\"] += 2\n", " stats[\"Perception\"] += 2\n", " stats[\"Charisma\"] += 2\n", " stats[\"Intelligence\"] += 2\n", " return stats" ] }, { "cell_type": "code", "execution_count": 170, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'HP': 128, 'MP': 42, 'SP': 100, 'Strength': 24, 'Perception': 3, 'Endurance': 17, 'Charisma': 1, 'Intelligence': 2, 'Agility': 17, 'Luck': 10}\n", "['Berserk', 'Blessing']\n", "[]\n" ] } ], "source": [ "h = Blessing(h)\n", "print(h.get_stats())\n", "print(h.get_positive_effects())\n", "print(h.get_negative_effects())" ] }, { "cell_type": "code", "execution_count": 171, "metadata": {}, "outputs": [], "source": [ "class Weakness(AbstractNegative):\n", " def get_stats(self):\n", " stats = self.base.get_stats()\n", " stats[\"Strength\"] -= 4\n", " stats[\"Endurance\"] -= 4\n", " stats[\"Agility\"] -= 4\n", " return stats" ] }, { "cell_type": "code", "execution_count": 172, "metadata": {}, "outputs": [], "source": [ "class EvilEye(AbstractNegative):\n", " def get_stats(self):\n", " stats = self.base.get_stats()\n", " stats[\"Luck\"] -= 10\n", " return stats" ] }, { "cell_type": "code", "execution_count": 173, "metadata": {}, "outputs": [], "source": [ "class Curse(AbstractNegative):\n", " def get_stats(self):\n", " stats = self.base.get_stats()\n", " stats[\"Strength\"] -= 2\n", " stats[\"Endurance\"] -= 2\n", " stats[\"Agility\"] -= 2\n", " stats[\"Luck\"] -= 2\n", " stats[\"Perception\"] -= 2\n", " stats[\"Charisma\"] -= 2\n", " stats[\"Intelligence\"] -= 2\n", " return stats" ] }, { "cell_type": "code", "execution_count": 174, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'HP': 128, 'MP': 42, 'SP': 100, 'Strength': 22, 'Perception': 1, 'Endurance': 15, 'Charisma': -1, 'Intelligence': 0, 'Agility': 15, 'Luck': 8}\n", "['Berserk', 'Blessing', 'Curse']\n", "[]\n" ] } ], "source": [ "h = Curse(h)\n", "print(h.get_stats())\n", "print(h.get_positive_effects())\n", "print(h.get_negative_effects())" ] }, { "cell_type": "code", "execution_count": 175, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "Can't instantiate abstract class AbstractEffect with abstract methods get_negative_effects, get_positive_effects, get_stats", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mae\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mAbstractEffect\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mh\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mae\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: Can't instantiate abstract class AbstractEffect with abstract methods get_negative_effects, get_positive_effects, get_stats" ] } ], "source": [ "ae = AbstractEffect(h)\n", "print(ae)" ] }, { "cell_type": "code", "execution_count": 176, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "Can't instantiate abstract class AbstractPositive with abstract methods get_stats", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0map\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mAbstractPositive\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mh\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0map\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: Can't instantiate abstract class AbstractPositive with abstract methods get_stats" ] } ], "source": [ "ap = AbstractPositive(h)\n", "print(ap)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }