任务描述 本关任务:编写一个能实现游戏角色变身的小程序。 相关知识 为了完成本关任务,你需要掌握:1.如何定义抽象类(接口),2.如何使用抽象类。 题目内容 已知游戏中常见人物状态State抽象类,包括站立(Idle),行走(Run),攻击(Attack),死亡(Dead)等动作。在游戏中有角色hero类,拥有人物状态同时还拥有血量,攻击力,防御力等属性,有怪物类Monster,怪物有血量,攻击力,防御力,怪物类型等属性。 已知角色可以变身为怪物,现在main函数中,模拟某个角色在正常情况下以及在变身状态下的各种状态操作(输入1为正常hero,输入2则变身为monster)。要求,使用抽象类及的基类指针实现。 编程要求 根据提示,在右侧编辑器补充代码,完成程序。 测试说明 平台会对你编写的代码进行测试: 测试输入: 1 预期输出: hero is idleling... hero is attack... hero is running... hero is dead... 测试输入: 2 预期输出: Monster is idleling... Monster is attack... Monster is running... Monster is dead... 开始你的任务吧,祝你成功!
时间: 2024-02-23 15:57:56 浏览: 137
Java小项目开发实例:简易任务管理器的设计与实现
```cpp
#include <iostream>
using namespace std;
// 抽象类State
class State {
public:
virtual void idle() = 0;
virtual void run() = 0;
virtual void attack() = 0;
virtual void dead() = 0;
};
// 角色类Hero
class Hero {
public:
Hero(State* state, int hp, int attack, int defense) {
this->state = state;
this->hp = hp;
this->attack = attack;
this->defense = defense;
}
void setState(State* state) {
this->state = state;
}
void idle() {
state->idle();
}
void run() {
state->run();
}
void attack() {
state->attack();
}
void dead() {
state->dead();
}
private:
State* state;
int hp;
int attack;
int defense;
};
// 怪物类Monster
class Monster {
public:
Monster(State* state, int hp, int attack, int defense, string type) {
this->state = state;
this->hp = hp;
this->attack = attack;
this->defense = defense;
this->type = type;
}
void setState(State* state) {
this->state = state;
}
void idle() {
cout << type << " is idling..." << endl;
}
void run() {
cout << type << " is running..." << endl;
}
void attack() {
cout << type << " is attacking..." << endl;
}
void dead() {
cout << type << " is dead..." << endl;
}
private:
State* state;
int hp;
int attack;
int defense;
string type;
};
// 站立状态类IdleState
class IdleState : public State {
public:
void idle() {
cout << "hero is idling..." << endl;
}
void run() {
cout << "hero is running..." << endl;
}
void attack() {
cout << "hero is attacking..." << endl;
}
void dead() {
cout << "hero is dead..." << endl;
}
};
// 行走状态类RunState
class RunState : public State {
public:
void idle() {
cout << "hero is idling..." << endl;
}
void run() {
cout << "hero is running..." << endl;
}
void attack() {
cout << "hero is attacking..." << endl;
}
void dead() {
cout << "hero is dead..." << endl;
}
};
// 攻击状态类AttackState
class AttackState : public State {
public:
void idle() {
cout << "hero is idling..." << endl;
}
void run() {
cout << "hero is running..." << endl;
}
void attack() {
cout << "hero is attacking..." << endl;
}
void dead() {
cout << "hero is dead..." << endl;
}
};
// 死亡状态类DeadState
class DeadState : public State {
public:
void idle() {
cout << "hero is idling..." << endl;
}
void run() {
cout << "hero is running..." << endl;
}
void attack() {
cout << "hero is attacking..." << endl;
}
void dead() {
cout << "hero is dead..." << endl;
}
};
int main() {
State* idleState = new IdleState();
State* runState = new RunState();
State* attackState = new AttackState();
State* deadState = new DeadState();
Hero hero(idleState, 100, 10, 5);
Monster monster(idleState, 100, 10, 5, "Monster");
int input;
cin >> input;
if (input == 1) {
hero.idle();
hero.attack();
hero.run();
hero.dead();
} else if (input == 2) {
monster.idle();
monster.attack();
monster.run();
monster.dead();
}
return 0;
}
```
阅读全文