已知游戏中常见人物状态State抽象类,包括站立(Idle),行走(Run),攻击(Attack),死亡(Dead)等动作。在游戏中有角色hero类,拥有人物状态同时还拥有血量,攻击力,防御力等属性,有怪物类Monster,怪物有血量,攻击力,防御力,怪物类型等属性。 已知角色可以变身为怪物,现在main函数中,模拟某个角色在正常情况下以及在变身状态下的各种状态操作(输入1为正常hero,输入2则变身为monster)。 要求,使用抽象类及的基类指针实现。
时间: 2024-02-20 22:57:28 浏览: 37
好的,我理解了你的问题。以下是实现该需求的一种可能的方式:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 人物状态抽象类
class State {
public:
virtual void idle() = 0;
virtual void run() = 0;
virtual void attack() = 0;
virtual void dead() = 0;
};
// 角色类
class Hero {
public:
Hero(int hp, int attack, int defense) : m_hp(hp), m_attack(attack), m_defense(defense), m_state(nullptr) {}
void setState(State* state) {
m_state = state;
}
void idle() {
m_state->idle();
}
void run() {
m_state->run();
}
void attack() {
m_state->attack();
}
void dead() {
m_state->dead();
}
void setHp(int hp) {
m_hp = hp;
}
void setAttack(int attack) {
m_attack = attack;
}
void setDefense(int defense) {
m_defense = defense;
}
int getHp() const {
return m_hp;
}
int getAttack() const {
return m_attack;
}
int getDefense() const {
return m_defense;
}
private:
int m_hp;
int m_attack;
int m_defense;
State* m_state;
};
// 怪物类
class Monster {
public:
Monster(int hp, int attack, int defense, const string& type) : m_hp(hp), m_attack(attack), m_defense(defense), m_type(type) {}
void setHp(int hp) {
m_hp = hp;
}
void setAttack(int attack) {
m_attack = attack;
}
void setDefense(int defense) {
m_defense = defense;
}
void setType(const string& type) {
m_type = type;
}
int getHp() const {
return m_hp;
}
int getAttack() const {
return m_attack;
}
int getDefense() const {
return m_defense;
}
const string& getType() const {
return m_type;
}
private:
int m_hp;
int m_attack;
int m_defense;
string m_type;
};
// 站立状态
class IdleState : public State {
public:
virtual void idle() {
cout << "角色/怪物处于站立状态" << endl;
}
virtual void run() {
cout << "角色/怪物开始奔跑" << endl;
}
virtual void attack() {
cout << "角色/怪物开始攻击" << endl;
}
virtual void dead() {
cout << "角色/怪物已死亡" << endl;
}
};
// 行走状态
class RunState : public State {
public:
virtual void idle() {
cout << "角色/怪物停止奔跑" << endl;
}
virtual void run() {
cout << "角色/怪物正在奔跑" << endl;
}
virtual void attack() {
cout << "角色/怪物在奔跑中无法攻击" << endl;
}
virtual void dead() {
cout << "角色/怪物已死亡" << endl;
}
};
// 攻击状态
class AttackState : public State {
public:
virtual void idle() {
cout << "角色/怪物停止攻击" << endl;
}
virtual void run() {
cout << "角色/怪物在攻击中无法奔跑" << endl;
}
virtual void attack() {
cout << "角色/怪物正在攻击" << endl;
}
virtual void dead() {
cout << "角色/怪物已死亡" << endl;
}
};
// 死亡状态
class DeadState : public State {
public:
virtual void idle() {
cout << "角色/怪物已死亡,无法进行任何操作" << endl;
}
virtual void run() {
cout << "角色/怪物已死亡,无法进行任何操作" << endl;
}
virtual void attack() {
cout << "角色/怪物已死亡,无法进行任何操作" << endl;
}
virtual void dead() {
cout << "角色/怪物已死亡,无法进行任何操作" << endl;
}
};
int main() {
// 创建角色和怪物
Hero hero(100, 10, 5);
Monster monster(80, 8, 4, "小怪兽");
// 创建状态对象
IdleState idleState;
RunState runState;
AttackState attackState;
DeadState deadState;
// 正常情况下的角色操作
hero.setState(&idleState);
hero.idle();
hero.setState(&runState);
hero.run();
hero.setState(&attackState);
hero.attack();
hero.setState(&deadState);
hero.dead();
// 变身为怪物后的操作
hero.setState(&attackState);
hero.attack();
hero.setState(&idleState);
hero.idle();
hero.setState(&runState);
hero.run();
hero.setState(&deadState);
hero.dead();
return 0;
}
```
在这个程序中,我们使用了状态模式来表示角色和怪物的不同状态。首先定义了一个人物状态抽象类 `State`,其中包含了四种状态:站立(`Idle`)、行走(`Run`)、攻击(`Attack`)和死亡(`Dead`)。然后我们定义了角色类 `Hero` 和怪物类 `Monster`,它们都包含了一些属性(如血量、攻击力、防御力等)以及一个指向 `State` 对象的指针 `m_state`,表示当前的状态。通过 `setState` 方法可以改变当前状态。最后我们定义了四个具体状态类 `IdleState`、`RunState`、`AttackState` 和 `DeadState`,分别实现了 `State` 中的四个操作。
在 `main` 函数中,我们首先创建了一个角色和一个怪物,并创建了四个状态对象。然后我们模拟了正常情况下的角色操作,以及角色变身为怪物后的操作。在程序运行时,输出了相应的状态信息。
需要注意的是,在这个程序中,我们使用了基类指针来实现多态。在 `Hero` 和 `Monster` 类中,使用了 `State*` 类型的指针来指向不同的状态对象。通过使用基类指针和虚函数,我们可以在运行时动态地决定调用哪个具体子类的方法,从而实现了状态模式的核心功能。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)