Problem 2.1: Buff and Equipment System (50pts)
Problem
In this problem, you will implement helper classes for the RPG game: the buff system and equipment system.
在本问题中,您将为 RPG 游戏实现辅助类:Buff 系统和装备系统。
Buff Class
The
Buffclass represents temporary stat modifications (buffs or debuffs) that affect characters during combat. In this class, you need to implement thedecrease_duration()method, it should:
- Decrease duration by 1
- Return
Trueif expired (duration ≤ 0),Falseotherwise
Buff 类表示在战斗中影响角色的临时属性修改(增益或减益)。
在此类中,您需要实现 decrease_duration() 方法,它应该:
- 将持续时间减 1
- 如果过期(持续时间 ≤ 0),返回
True,否则返回False
class Buff:
"""
Represents a temporary stat modification (buff or debuff).
Duration decreases each turn until it expires.
"""
def decrease_duration(self):
"""Decrease duration by 1. Returns True if expired.
>>> buff = Buff("Test Buff", attack_bonus=5, defense_bonus=3, duration=2)
>>> buff.decrease_duration()
False
>>> buff.decrease_duration()
True
"""
"""YOUR CODE HERE"""
Equipment Classes
The equipment system uses inheritance to model weapons and armor. Both inherit from the
Equipmentbase class.Equipment Base Class:
- Stores
name,attack_bonus,defense_bonus, andprice- Provides common interface for all equipment
Weapon Class:
- Specialized for attack-focused equipment
- Only needs to specify
name,attack_bonus, andprice- Defense bonus defaults to 0
Armor Class:
- Specialized for defense-focused equipment
- Only needs to specify
name,defense_bonus, andprice- Attack bonus defaults to 0
装备系统使用继承来建模武器和护甲。两者都继承自 Equipment 基类。
装备基类 (Equipment Base Class):
- 存储
name(名称)、attack_bonus(攻击加成)、defense_bonus(防御加成)和price(价格) - 为所有装备提供通用接口
武器类 (Weapon Class):
- 专注于攻击的装备
- 只需要指定
name、attack_bonus和price - 防御加成默认为 0
护甲类 (Armor Class):
- 专注于防御的装备
- 只需要指定
name、defense_bonus和price - 攻击加成默认为 0
class Weapon(Equipment):
def __init__(self, name, attack_bonus, price=0):
"""Initialize a Weapon with the given name, attack bonus, and price.
>>> sword = Weapon("Sword", attack_bonus=10, price=100)
>>> print(sword)
Sword (ATK +10)
>>> print(sword.price)
100
"""
"""YOUR CODE HERE"""
class Armor(Equipment):
def __init__(self, name, defense_bonus, price=0):
"""Initialize an Armor with the given name, defense bonus, and price.
>>> shield = Armor("Shield", defense_bonus=8, price=80)
>>> print(shield)
Shield (DEF +8)
>>> print(shield.price)
80
"""
"""YOUR CODE HERE"""
Hints
Hint: Use
super().__init__()to call the parent class constructor with appropriate parameters.
- 使用
super().__init__()调用父类的构造函数并传入适当的参数。