用C++写以下代码已知游戏中常见人物状态State抽象类,包括站立(Idle),行走(Run),攻击(Attack),死亡(Dead)等动作。在游戏中有角色hero类,拥有人物状态同时还拥有血量,攻击力,防御力等属性,有怪物类Monster,怪物有血量,攻击力,防御力,怪物类型等属性。 已知角色可以变身为怪物,现在main函数中,模拟某个角色在正常情况下以及在变身状态下的各种状态操作(输入1为正常hero,输入2则变身为monster)。 要求,使用抽象类及的基类指针实现。
时间: 2023-11-30 11:03:40 浏览: 169
以下是示例代码:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 人物状态抽象类
class State {
public:
virtual void action() = 0;
};
// 站立状态类
class IdleState : public State {
public:
void action() override {
cout << "站立" << endl;
}
};
// 行走状态类
class RunState : public State {
public:
void action() override {
cout << "行走" << endl;
}
};
// 攻击状态类
class AttackState : public State {
public:
void action() override {
cout << "攻击" << endl;
}
};
// 死亡状态类
class DeadState : public State {
public:
void action() override {
cout << "死亡" << endl;
}
};
// 人物基类
class Character {
public:
Character(int hp, int attack, int defense) : hp(hp), attack(attack), defense(defense) {}
virtual void setState(State* state) {
this->state = state;
}
void performAction() {
state->action();
}
void setHp(int hp) {
this->hp = hp;
}
int getHp() const {
return hp;
}
void setAttack(int attack) {
this->attack = attack;
}
int getAttack() const {
return attack;
}
void setDefense(int defense) {
this->defense = defense;
}
int getDefense() const {
return defense;
}
private:
int hp;
int attack;
int defense;
State* state;
};
// 英雄类
class Hero : public Character {
public:
Hero(int hp, int attack, int defense) : Character(hp, attack, defense) {
setState(new IdleState());
}
};
// 怪物类
class Monster : public Character {
public:
Monster(int hp, int attack, int defense, string type) : Character(hp, attack, defense), type(type) {
setState(new IdleState());
}
void setType(string type) {
this->type = type;
}
string getType() const {
return type;
}
private:
string type;
};
int main() {
int type;
cout << "请选择角色类型:1.英雄 2.怪物" << endl;
cin >> type;
Character* character;
if (type == 1) {
character = new Hero(100, 10, 5);
} else if (type == 2) {
character = new Monster(200, 20, 10, "普通怪物");
} else {
cout << "无效的角色类型" << endl;
return 0;
}
character->performAction();
cout << "是否变身为怪物?1.是 2.否" << endl;
int choice;
cin >> choice;
if (choice == 1) {
Monster* monster = new Monster(character->getHp(), character->getAttack(), character->getDefense(), "变身怪物");
character = monster;
}
character->performAction();
character->setState(new RunState());
character->performAction();
character->setState(new AttackState());
character->performAction();
character->setHp(0);
character->setState(new DeadState());
character->performAction();
delete character;
return 0;
}
```
在上面的代码中,我们定义了一个人物状态抽象类 `State`,以及其子类 `IdleState`、`RunState`、`AttackState`、`DeadState`,分别代表站立、行走、攻击、死亡状态。我们还定义了一个人物基类 `Character`,作为英雄类 `Hero` 和怪物类 `Monster` 的父类。在 `Character` 类中,我们使用基类指针 `State* state` 来保存当前人物的状态,通过调用 `state->action()` 来执行相应的操作。在英雄类和怪物类的构造函数中,我们都使用 `setState(new IdleState())` 来初始化默认状态为站立状态。
在 `main()` 函数中,我们通过输入来选择角色类型,并进行相应的状态操作。如果选择变身为怪物,则创建一个新的怪物对象,并赋值给 `Character* character` 指针。最后使用 `delete` 关键字释放对象内存。
阅读全文