Skip to content

Problem 2.2: Character Base Class (100pts)

Problem

In this problem, you will implement the Character base class, which provides common functionality for all characters in the game (both players and enemies).

A character has the following attributes:

  • name: Character's name
  • max_hp: Maximum health points
  • current_hp: Current health points (starts at max)
  • base_attack: Base attack power
  • base_defense: Base defense power
  • buffs: List of active buffs/debuffs

In this class, you need to implement the following methods:

在本问题中,您将实现 Character 基类,它为游戏中的所有角色(包括玩家和敌人)提供通用功能

一个角色具有以下属性:

  • name:角色的名字
  • max_hp:最大生命值
  • current_hp:当前生命值(初始等于最大生命值)
  • base_attack:基础攻击力
  • base_defense:基础防御力
  • buffs:活跃的增益/减益列表

在此类中,您需要实现以下方法:

1. get_attack() and get_defense()

Calculate total stats including buff bonuses:

计算包括 Buff 加成在内的总属性

def get_attack(self):
    """Calculate total attack including buffs.
    >>> char = Character("Student", max_hp=100, attack=20, defense=10)
    >>> buff = Buff("Might", attack_bonus=5, duration=2)
    >>> char.buffs.append(buff)
    >>> char.get_attack()
    25
    """
    """YOUR CODE HERE"""

def get_defense(self):
    """Calculate total defense including buffs.
    >>> char = Character("Student", max_hp=100, attack=20, defense=10)
    >>> buff = Buff("Shield", defense_bonus=3, duration=2)
    >>> char.buffs.append(buff)
    >>> char.get_defense()
    13
    """
    """YOUR CODE HERE"""

2. take_damage(damage)

Apply damage after accounting for defense: 在考虑防御后施加伤害

def take_damage(self, damage):
    """Apply damage, accounting for defense. Returns actual damage taken.
    >>> char = Character("Student", max_hp=100, attack=20, defense=10)
    >>> char.take_damage(25)
    15
    >>> char.current_hp
    85
    """
    """YOUR CODE HERE"""

3. heal(amount)

Restore HP without exceeding maximum: 恢复 HP,但不超过最大值

def heal(self, amount):
    """Heal the character. Returns actual amount healed.
    >>> char = Character("Student", max_hp=100, attack=20, defense=10)
    >>> char.current_hp = 50
    >>> char.heal(30)
    30
    >>> char.current_hp
    80
    """
    """YOUR CODE HERE"""

4. update_buffs()

Update all buffs and remove expired ones:

更新所有 Buff移除过期的:

def update_buffs(self):
    """Update all buffs (decrease duration, remove expired).
    >>> char = Character("Student", max_hp=100, attack=20, defense=10)
    >>> buff1 = Buff("Might", attack_bonus=5, duration=1)
    >>> buff2 = Buff("Shield", defense_bonus=3, duration=2)
    >>> char.buffs.extend([buff1, buff2])
    >>> len(char.buffs)
    2
    >>> char.update_buffs()
    >>> len(char.buffs)
    1
    >>> char.update_buffs()
    >>> len(char.buffs)
    0
    """
    """YOUR CODE HERE"""

5. magic_attack(target)

Perform a magic attack with 1.5x damage but gain "Exhaustion" debuff:

执行魔法攻击,造成 1.5 倍伤害,但获得“精疲力尽”debuff

def magic_attack(self, target):
    """Perform a magic attack. Can be overridden by subclasses.
    Return a string describing the attack.
    >>> char = Character("Mage", max_hp=80, attack=15, defense=5)
    >>> enemy = Character("Goblin", max_hp=50, attack=10, defense=2)
    >>> result = char.magic_attack(enemy)
    >>> print(result)
    Mage uses magic attack on Goblin for 20 damage!
    >>> enemy.current_hp
    30
    >>> char.buffs[0]
    Exhaustion: ATK -5, DEF -3 (2 turns)
    """
    """YOUR CODE HERE"""
    return f"{self.name} uses magic attack on {target.name} for {actual_damage} damage!"

Requirements:

  1. Calculate damage as int(get_attack() * 1.5)
  2. Apply damage to target using take_damage()
  3. Add "Exhaustion" buff to self: Buff("Exhaustion", attack_bonus=-5, defense_bonus=-3, duration=2)

要求:

  1. 计算伤害,公式为 int(get_attack() * 1.5)
  2. 使用 take_damage() 对目标施加伤害
  3. 对自身施加“疲劳”效果:Buff("Exhaustion", attack_bonus=-5, defense_bonus=-3, duration=2)

Hints

Hint:

Attack cannot heal your enemy; ensure that when your damage is less than the enemy's defense, no damage is taken. Also, damage cannot reduce HP below 0; so the actual damage taken should always be at most the current HP.

Healing cannot exceed max HP; ensure that the healed amount does not push current HP beyond max HP.

  • 攻击不能治愈你的敌人;确保当你的伤害小于敌人的防御时,不造成任何伤害。

  • 此外,伤害不能将生命值降至 0 以下;因此,实际承受的伤害始终最多等于当前生命值。

  • 治疗不能超过最大生命值;确保治疗量不会使当前生命值超过最大生命值。

  • magic_attack 应模仿 basic_attack 来写,并使用 take_damage() 方法来造成伤害,不要打破抽象。

  • get_attack()get_defense() 的返回值可能为负数,不要想多了把负数变成 0。