DeathManager
题目背景
死神先生负责掌管部分特定人群的命运。每当一个人出生时,死神会记录下该人的姓名(name)、生命值(health)、力量(strength)、灵巧(dexterity)、智力(intelligence)、信仰(faith)以及法力值(mana)。每个人都可以通过physicalAttack和magicalAttack两种方式来攻击其他人。
在基础physicalAttack中,如果攻击者的力量不小于防御者的灵巧值,那么防御者的生命值将减少,减少的数值为攻击者的力量与防御者灵巧值的乘积。
在基础magicalAttack中,攻击者将消耗 20 点法力值(mana),对防御者造成的生命值伤害为攻击者的智力与信仰值的乘积。如果攻击者的法力不足 20 点,则该魔法攻击不会产生任何效果。
当某人的生命值降为 0 时,他会死亡,并且不能再攻击别人。攻击已死亡的人,或者让已死亡的人进行攻击,均不会产生任何效果。
请实现一个Person类,以及DeathManager类中的 progressYear和deathRecord方法。我们会使用newborn方法来创建新的人。progressYear方法表示时间流逝一年,deathRecord方法需要返回已死亡的人的记录以及他们死亡时的年龄,按死亡时间的先后顺序排列。
死神也会为某些人提供装备。每个装备物品都由一个继承自item类的子类实例来定义。有些物品可能带有主动技能,可以通过调用activeSkill方法来使用。物品可以被授予给某人,也可以被移除。在physicalAttack和magicalAttack时,如果人具有特定装备,会在
你需要实现grantItem和removeItem两个方法。我们已经定义了一些物品,你需要添加相应的代码以实现它们的功能:
Blade:使佩戴者的灵巧值增加 5 点,装备被移除时增加的灵巧值也被扣除。active skill:对对手造成三次伤害(依次执行),每次伤害为 10 + 力量值。当具有该装备时,
physicalAttack使用该active skill进行替代(即使用blade的技能,而不会再执行基础物理攻击)。Shield:受到攻击时,所受伤害减少 20 点。此效果先于 CalamityRing 的效果计算。
Wand:active skill:消耗 100 点法力值,对对手造成 200 点伤害,如果当前法力值不足100则无法使用。当具有该装备时,
magicalAttack用该active skill进行替代(即使当前mana不够使用wand但够执行基础魔法攻击时,也无法再执行基础的魔法攻击)。魔杖的主动技能最多只能使用 5 次,之后魔杖将被移除。RingOfSacrifice:在佩戴者即将因致命伤害而死亡时,不会死亡,而是将生命值保留为 1 点,同时戒指破碎(被移除)。
CalamityRing:佩戴者将承受双倍伤害。
RingOfTheEvilEye:当对手死亡时,佩戴者将吸收 +30 点生命值。
为了实现上述功能,可自由考虑为类添加成员变量和函数。但请不要修改已有代码中的函数/变量/类名称,可能会导致测试运行出错!
框架代码
题目给出的框架代码如下:
JAVA
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class DeathManager {
public class Person {
// if you need, you can add other members or methods
public String name;
public int health;
public int strength;
public int dexterity;
public int intelligence;
public int faith;
public int mana;
public Person(String name, int health, int strength, int dexterity, int intelligence, int faith, int mana) {
this.name = name;
this.health = health;
this.strength = strength;
this.dexterity = dexterity;
this.intelligence = intelligence;
this.faith = faith;
this.mana = mana;
/** WRITE YOUR CODE HERE **/
}
public void physicalAttack(Person other){
/** WRITE YOUR CODE HERE **/
}
public void magicalAttack(Person other){
/** WRITE YOUR CODE HERE **/
}
}
public Person newborn(String name,int health, int strength, int dexterity, int intelligence, int faith, int mana){
return new Person(name, health, strength, dexterity, intelligence, faith, mana);//Do not modify
}
public void progressYear() {
/** WRITE YOUR CODE HERE **/
}
public Map<String, Integer> deathRecord() {
/** WRITE YOUR CODE HERE **/
return null;
}
public void grantItem(Person person, Item item) {
/** WRITE YOUR CODE HERE **/
}
public void removeItem(Person person, Item item) {
/** WRITE YOUR CODE HERE **/
}
private static class Item {
void activeSkill(Person other) {
}
}
public static class Blade extends Item {
/** WRITE YOUR CODE HERE **/
}
public static class Shield extends Item {
/** WRITE YOUR CODE HERE **/
}
public static class Wand extends Item {
/** WRITE YOUR CODE HERE **/
}
public static class RingOfSacrifice extends Item {
/** WRITE YOUR CODE HERE **/
}
public static class CalamityRing extends Item {
/** WRITE YOUR CODE HERE **/
}
public static class RingOfTheEvilEye extends Item {
/** WRITE YOUR CODE HERE **/
}
}
参考代码
JAVA
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
class DeathManager {
public class Person {
public String name;
public int health;
public int strength;
public int dexterity;
public int intelligence;
public int faith;
public int mana;
public int age;
public boolean isDead;
public List<Item> items;
public Person(String name, int health, int strength, int dexterity, int intelligence, int faith, int mana) {
this.name = name;
this.health = health;
this.strength = strength;
this.dexterity = dexterity;
this.intelligence = intelligence;
this.faith = faith;
this.mana = mana;
this.age = 0;
this.isDead = false;
this.items = new ArrayList<>();
}
public boolean hasItem(Class<? extends Item> itemClass) {
for (Item item : items) {
if (itemClass.isInstance(item)) {
return true;
}
}
return false;
}
@SuppressWarnings("unchecked")
public <T extends Item> T getItem(Class<T> itemClass) {
for (Item item : items) {
if (itemClass.isInstance(item)) {
return (T) item;
}
}
return null;
}
public void physicalAttack(Person other) {
// 检查攻击者或防御者是否已死亡
if (this.isDead || other.isDead) {
return;
}
// 检查是否有Blade装备,如果有则使用Blade的主动技能
if (this.hasItem(Blade.class)) {
Blade blade = this.getItem(Blade.class);
blade.performSkill(this, other);
// 不执行基础物理攻击
return;
}
// 基础物理攻击
if (this.strength >= other.dexterity) {
int damage = this.strength * other.dexterity;
applyDamage(this, other, damage);
}
}
public void magicalAttack(Person other) {
// 检查攻击者或防御者是否已死亡
if (this.isDead || other.isDead) {
return;
}
// 检查是否有Wand装备
if (this.hasItem(Wand.class)) {
Wand wand = this.getItem(Wand.class);
wand.performSkill(this, other);
// 替代基础魔法攻击,即使法力不足100也不能使用基础魔法攻击
return;
}
// 基础魔法攻击
if (this.mana >= 20) {
this.mana -= 20;
int damage = this.intelligence * this.faith;
applyDamage(this, other, damage);
}
}
void applyDamage(Person attacker, Person target, int damage) {
// Shield效果先于CalamityRing
if (target.hasItem(Shield.class)) {
damage -= 20;
if (damage < 0) {
damage = 0;
}
}
// CalamityRing双倍伤害
if (target.hasItem(CalamityRing.class)) {
damage *= 2;
}
// 应用伤害
target.health -= damage;
// 检查目标是否死亡
checkTargetDeath(attacker, target);
}
void checkTargetDeath(Person attacker, Person target) {
if (target.health <= 0 && !target.isDead) {
// 检查RingOfSacrifice
if (target.hasItem(RingOfSacrifice.class)) {
target.health = 1;
// 戒指破碎
RingOfSacrifice ring = target.getItem(RingOfSacrifice.class);
target.items.remove(ring);
} else {
target.health = 0;
target.isDead = true;
recordDeath(target);
// RingOfTheEvilEye效果:攻击者吸收生命值
if (attacker != null && attacker.hasItem(RingOfTheEvilEye.class)) {
attacker.health += 30;
}
}
}
}
}
private List<Person> allPersons;
private Map<String, Integer> deathRecords;
public DeathManager() {
this.allPersons = new ArrayList<>();
this.deathRecords = new LinkedHashMap<>();
}
public Person newborn(String name, int health, int strength, int dexterity, int intelligence, int faith, int mana) {
Person person = new Person(name, health, strength, dexterity, intelligence, faith, mana);
allPersons.add(person);
return person;
}
public void progressYear() {
for (Person person : allPersons) {
if (!person.isDead) {
person.age++;
}
}
}
public Map<String, Integer> deathRecord() {
return new LinkedHashMap<>(deathRecords);
}
public void grantItem(Person person, Item item) {
if (person.isDead) {
return;
}
if (!person.items.contains(item)) {
person.items.add(item);
item.onEquip(person);
}
}
public void removeItem(Person person, Item item) {
if (person.items.contains(item)) {
item.onRemove(person);
person.items.remove(item);
}
}
private void recordDeath(Person person) {
if (!deathRecords.containsKey(person.name)) {
deathRecords.put(person.name, person.age);
}
}
public abstract static class Item {
public void onEquip(Person person) {}
public void onRemove(Person person) {}
public void activeSkill(Person user, Person target) {}
}
public static class Blade extends Item {
@Override
public void onEquip(Person person) {
person.dexterity += 5;
}
@Override
public void onRemove(Person person) {
person.dexterity -= 5;
}
@Override
public void activeSkill(Person user, Person target) {
performSkill(user, target);
}
public void performSkill(Person user, Person target) {
if (user.isDead || target.isDead) {
return;
}
// 三次伤害,每次为10 + 力量值
for (int i = 0; i < 3; i++) {
if (target.isDead) {
break;
}
int damage = 10 + user.strength;
user.applyDamage(user, target, damage);
}
}
}
public static class Shield extends Item {
}
public static class Wand extends Item {
private int useCount = 0;
@Override
public void activeSkill(Person user, Person target) {
performSkill(user, target);
}
public void performSkill(Person user, Person target) {
if (user.isDead || target.isDead) {
return;
}
// 检查使用次数
if (useCount >= 5) {
// 移除魔杖
user.items.remove(this);
return;
}
// 检查法力值
if (user.mana < 100) {
// 法力不足,不产生效果,但也不使用基础魔法攻击
return;
}
// 消耗法力并造成伤害
user.mana -= 100;
useCount++;
int damage = 200;
user.applyDamage(user, target, damage);
// 如果使用次数达到5次,移除魔杖
if (useCount >= 5) {
user.items.remove(this);
}
}
}
public static class RingOfSacrifice extends Item {
}
public static class CalamityRing extends Item {
}
public static class RingOfTheEvilEye extends Item {
}
}
代码解析
这个代码实现了一个带有装备系统的角色战斗模拟器,主要包含三个层次:
DeathManager - 游戏管理器
Person - 角色类
Item及其子类 - 装备系统
DeathManager 类
JAVA
private List<Person> allPersons; // 存储所有角色
private Map<String, Integer> deathRecords; // 存储死亡记录(使用LinkedHashMap保持插入顺序)核心方法:
newborn(): 创建新角色并添加到管理列表progressYear(): 增加所有存活角色的年龄deathRecord(): 返回死亡记录的副本
Person 类
属性设计
JAVA
public int age; // 年龄
public boolean isDead; // 死亡状态
public List<Item> items; // 装备列表
装备检测机制
使用泛型和类型检查来检测特定装备:
JAVA
public boolean hasItem(Class<? extends Item> itemClass) // 检查是否拥有某类装备
public <T extends Item> T getItem(Class<T> itemClass) // 获取特定装备实例
攻击系统
物理攻击流程:
PLAINTEXT
physicalAttack(other)
↓
检查是否有Blade?
↓ 是
使用Blade技能(三次10+力量值伤害)
↓ 否
基础物理攻击:力量 ≥ 灵巧?
↓ 是
伤害 = 力量 × 灵巧
魔法攻击流程:
PLAINTEXT
magicalAttack(other)
↓
检查是否有Wand?
↓ 是
使用Wand技能(消耗100法力,造成200伤害)
↓ 否
法力 ≥ 20?
↓ 是
消耗20法力,伤害 = 智力 × 信仰
伤害计算系统
JAVA
void applyDamage(Person attacker, Person target, int damage) {
// 1. Shield效果:伤害-20(优先计算)
if (target.hasItem(Shield.class)) {
damage = max(0, damage - 20);
}
// 2. CalamityRing效果:伤害×2
if (target.hasItem(CalamityRing.class)) {
damage *= 2;
}
// 3. 应用伤害并检查死亡
target.health -= damage;
checkTargetDeath(attacker, target);
}
死亡处理系统
JAVA
void checkTargetDeath(Person attacker, Person target) {
if (target.health <= 0 && !target.isDead) {
// 1. RingOfSacrifice检查:保留1点生命
if (target.hasItem(RingOfSacrifice.class)) {
target.health = 1;
target.items.remove(ring); // 戒指破碎
}
// 2. 真正死亡处理
else {
target.isDead = true;
recordDeath(target);
// 3. RingOfTheEvilEye:攻击者+30生命
if (attacker.hasItem(RingOfTheEvilEye.class)) {
attacker.health += 30;
}
}
}
}
装备系统详解
Blade(剑)
JAVA
装备效果:灵巧 +5
移除效果:灵巧 -5
主动技能:三次攻击,每次 10 + 力量值
替代效果:替代基础物理攻击Shield(盾)
JAVA
被动效果:受到伤害 -20(最小值0)
优先级:先于CalamityRing计算Wand(魔杖)
JAVA
主动技能:消耗100法力,造成200伤害
限制:最多使用5次,之后自动移除
特殊:即使法力不足100,也不能使用基础魔法攻击RingOfSacrifice(牺牲戒指)
JAVA
效果:致命伤害时保留1点生命
代价:戒指破碎移除
CalamityRing(灾祸戒指)
JAVA
效果:受到双倍伤害
RingOfTheEvilEye(邪眼戒指)
JAVA
效果:击杀敌人时获得30生命值
关键设计要点
伤害计算顺序:Shield → CalamityRing,确保减伤和增伤的正确交互
装备替代机制:Blade和Wand会完全替代基础攻击,不能回退
死亡处理:使用状态标记
isDead防止重复触发记录保存:LinkedHashMap保持死亡顺序,只记录首次死亡
边界情况处理:
已死亡角色不能攻击和被攻击
伤害不能为负
装备不能重复添加
法力不足时的特殊处理

