已知游戏中常见人物状态State抽象类,包括站立(Idle),行走(Run),攻击(Attack),死亡(Dead)等动作。在游戏中有角色hero类,拥有人物状态同时还拥有血量,攻击力,防御力等属性,有怪物类Monster,怪物有血量,攻击力,防御力,怪物类型等属性。 已知角色可以变身为怪物,现在main函数中,模拟某个角色在正常情况下以及在变身状态下的各种状态操作(输入1为正常hero,输入2则变身为monster)。 要求,使用抽象类及的基类指针实现。
时间: 2024-02-22 09:55:09 浏览: 35
好的,这是一个关于多态的题目。我先给你一份基本的代码框架:
```C++
#include<iostream>
using namespace std;
class State {
public:
virtual void idle() = 0;
virtual void run() = 0;
virtual void attack() = 0;
virtual void dead() = 0;
};
class Character {
protected:
int hp;
int attack;
int defense;
State* state; // 指向状态的指针
public:
Character(int hp, int attack, int defense) :hp(hp), attack(attack), defense(defense), state(nullptr) {}
virtual ~Character() {}
void setState(State* state) {
this->state = state;
}
virtual void show() = 0;
virtual void normalState() = 0;
virtual void transform() = 0; // 变身
};
class Hero : public Character {
public:
Hero(int hp, int attack, int defense) :Character(hp, attack, defense) {}
void show() {
cout << "I am a hero." << endl;
}
void normalState() {
setState(new Idle);
}
void transform() {
setState(new MonsterIdle);
}
};
class Monster : public Character {
protected:
string type;
public:
Monster(int hp, int attack, int defense, string type) :Character(hp, attack, defense), type(type) {}
void show() {
cout << "I am a " << type << " monster." << endl;
}
void normalState() {
setState(new Idle);
}
void transform() {} // 怪物不能变身
};
class Idle : public State {
public:
void idle() {
cout << "I am idle." << endl;
}
void run() {
cout << "I am running." << endl;
}
void attack() {
cout << "I am attacking." << endl;
}
void dead() {
cout << "I am dead." << endl;
}
};
class Run : public State {
public:
void idle() {
cout << "I am idle." << endl;
}
void run() {
cout << "I am running." << endl;
}
void attack() {
cout << "I am attacking." << endl;
}
void dead() {
cout << "I am dead." << endl;
}
};
class Attack : public State {
public:
void idle() {
cout << "I am idle." << endl;
}
void run() {
cout << "I am running." << endl;
}
void attack() {
cout << "I am attacking." << endl;
}
void dead() {
cout << "I am dead." << endl;
}
};
class Dead : public State {
public:
void idle() {
cout << "I am idle." << endl;
}
void run() {
cout << "I am running." << endl;
}
void attack() {
cout << "I am attacking." << endl;
}
void dead() {
cout << "I am dead." << endl;
}
};
class MonsterIdle : public Idle {
public:
void idle() {
cout << "I am a monster idle." << endl;
}
};
int main() {
Character* character = nullptr;
int choice;
cout << "1. Choose hero." << endl;
cout << "2. Choose monster." << endl;
cin >> choice;
if (choice == 1) {
character = new Hero(100, 10, 5);
}
else if (choice == 2) {
character = new Monster(100, 8, 3, "Goblin");
}
character->show(); // 输出角色信息
character->normalState(); // 设置为正常状态
character->setState(new Run); // 设置状态为跑步
character->state->run(); // 输出状态信息
character->setState(new Attack); // 设置状态为攻击
character->state->attack(); // 输出状态信息
character->setState(new Dead); // 设置状态为死亡
character->state->dead(); // 输出状态信息
character->transform(); // 变身
character->show(); // 输出角色信息
character->setState(new Run); // 设置状态为跑步
character->state->run(); // 输出状态信息
delete character; // 释放内存
return 0;
}
```
这是一个基本的框架,你可以根据需要进行修改和扩展。