PokéLLMon 源码解析(三)(1)https://developer.aliyun.com/article/1483644
.\PokeLLMon\poke_env\environment\pokemon_gender.py
""" This module defines the PokemonGender class, which represents the gender of a Pokemon. """ # 导入必要的模块 from __future__ import annotations from enum import Enum, auto, unique from poke_env.exceptions import ShowdownException # 定义 PokemonGender 枚举类 @unique class PokemonGender(Enum): """Enumeration, represent a pokemon's gender.""" # 定义枚举值 FEMALE = auto() MALE = auto() NEUTRAL = auto() # 定义对象的字符串表示 def __str__(self) -> str: return f"{self.name} (pokemon gender) object" # 根据接收到的性别信息返回对应的 PokemonGender 对象 @staticmethod def from_request_details(gender: str) -> PokemonGender: """Returns the PokemonGender object corresponding to the gender received in a message. :param gender: The received gender to convert. :type gender: str :return: The corresponding PokemonGenre object. :rtype: PokemonGenre """ if gender == "M": return PokemonGender.MALE elif gender == "F": return PokemonGender.FEMALE # 抛出异常,表示未处理的请求性别 raise ShowdownException("Unmanaged request gender: '%s'", gender)
.\PokeLLMon\poke_env\environment\pokemon_type.py
"""This module defines the PokemonType class, which represents a Pokemon type. PokemonTypes are mainly associated with Pokemons and moves. """ # 导入必要的模块 from __future__ import annotations from enum import Enum, auto, unique from typing import Dict, Optional # 定义 PokemonType 枚举类 @unique class PokemonType(Enum): """A Pokemon type This enumeration represents pokemon types. Each type is an instance of this class, whose name corresponds to the upper case spelling of its english name (ie. FIRE). """ # 定义不同的 Pokemon 类型 BUG = auto() DARK = auto() DRAGON = auto() ELECTRIC = auto() FAIRY = auto() FIGHTING = auto() FIRE = auto() FLYING = auto() GHOST = auto() GRASS = auto() GROUND = auto() ICE = auto() NORMAL = auto() POISON = auto() PSYCHIC = auto() ROCK = auto() STEEL = auto() WATER = auto() THREE_QUESTION_MARKS = auto() # 返回 Pokemon 类型对象的字符串表示 def __str__(self) -> str: return f"{self.name} (pokemon type) object" # 计算该类型对于具有 type_1 和 type_2 类型的宝可梦的伤害倍率 def damage_multiplier( self, type_1: PokemonType, type_2: Optional[PokemonType] = None, *, type_chart: Dict[str, Dict[str, float]], ) -> float: """Computes the damage multiplier from this type on a pokemon with types `type_1` and, optionally, `type_2`. :param type_1: The first type of the target. :type type_1: PokemonType :param type_2: The second type of the target. Defaults to None. :type type_2: PokemonType, optional :return: The damage multiplier from this type on a pokemon with types `type_1` and, optionally, `type_2`. :rtype: float """ # 如果类型为 THREE_QUESTION_MARKS,则返回伤害倍率为 1 if ( self == PokemonType.THREE_QUESTION_MARKS or type_1 == PokemonType.THREE_QUESTION_MARKS ): return 1 # 计算伤害倍率 damage_multiplier = type_chart[type_1.name][self.name] if type_2 is not None: return damage_multiplier * type_chart[type_2.name][self.name] return damage_multiplier # 从给定名称返回对应的 PokemonType 对象 @staticmethod def from_name(name: str) -> PokemonType: """Returns a pokemon type based on its name. :param name: The name of the pokemon type. :type name: str :return: The corresponding type object. :rtype: PokemonType """ # 如果名称为 "???",返回特定的 PokemonType 对象 if name == "???": return PokemonType.THREE_QUESTION_MARKS # 否则根据名称在 PokemonType 枚举中查找对应的对象并返回 return PokemonType[name.upper()]
PokéLLMon 源码解析(三)(3)https://developer.aliyun.com/article/1483646