游戏中有一种职业的角色,战士,具有: 基本生命值,基本魔法值,基本攻击力,位置,装备等属性;要求:1)定义类:战士; (2)定义类:位置,成员为x,y坐标值和相关成员函数3)定义类:装备,成员为附加生命值,附加魔法值,附加攻击力和相关成员函数(4)使用构造函数完成类的数据成员初始化,初始值自定义;同时编写复制构造函数和析构函数;(5)类中编写成员函数,完成设置和查看自己生命值,魔法值,攻击力,位置,装备的操作;(可写带this指针形式)(6)类中加入成员函数,完成角色的更换装备操作,即修改装备的属性;(7)类中加入成员函数,完成攻击另一角色的操作,即根据自己基础攻击力+装备攻击力减去对方生命值;主函数中完成以上功能的测试
时间: 2023-08-09 19:10:30 浏览: 163
以下是实现代码:
```cpp
#include <iostream>
using namespace std;
class Position {
private:
int x;
int y;
public:
Position(int x, int y) {
this->x = x;
this->y = y;
}
void setX(int x) {
this->x = x;
}
void setY(int y) {
this->y = y;
}
int getX() {
return x;
}
int getY() {
return y;
}
};
class Equipment {
private:
int addHP;
int addMP;
int addAttack;
public:
Equipment(int addHP, int addMP, int addAttack) {
this->addHP = addHP;
this->addMP = addMP;
this->addAttack = addAttack;
}
Equipment(const Equipment& e) {
addHP = e.addHP;
addMP = e.addMP;
addAttack = e.addAttack;
}
~Equipment() {}
void setAddHP(int addHP) {
this->addHP = addHP;
}
void setAddMP(int addMP) {
this->addMP = addMP;
}
void setAddAttack(int addAttack) {
this->addAttack = addAttack;
}
int getAddHP() {
return addHP;
}
int getAddMP() {
return addMP;
}
int getAddAttack() {
return addAttack;
}
};
class Warrior {
private:
int HP;
int MP;
int attack;
Position pos;
Equipment equip;
public:
Warrior(int HP, int MP, int attack, Position pos, Equipment equip) : equip(equip) {
this->HP = HP;
this->MP = MP;
this->attack = attack;
this->pos = pos;
}
Warrior(const Warrior& w) : equip(w.equip) {
HP = w.HP;
MP = w.MP;
attack = w.attack;
pos = w.pos;
}
~Warrior() {}
void setHP(int HP) {
this->HP = HP;
}
void setMP(int MP) {
this->MP = MP;
}
void setAttack(int attack) {
this->attack = attack;
}
void setPos(int x, int y) {
pos.setX(x);
pos.setY(y);
}
void setEquip(Equipment equip) {
this->equip = equip;
}
int getHP() {
return HP;
}
int getMP() {
return MP;
}
int getAttack() {
return attack + equip.getAddAttack(); // 计算装备附加攻击力
}
Position getPos() {
return pos;
}
Equipment getEquip() {
return equip;
}
void attackWarrior(Warrior& w) {
int realAttack = getAttack() - w.getHP(); // 计算实际攻击力
if (realAttack > 0) {
w.setHP(0);
cout << "战士攻击了另一角色,对方被击败!" << endl;
} else {
w.setHP(w.getHP() - getAttack());
cout << "战士攻击了另一角色,对方剩余生命值为:" << w.getHP() << endl;
}
}
};
int main() {
Position pos(0, 0);
Equipment equip(100, 50, 20);
Warrior w1(500, 200, 50, pos, equip);
cout << "初始化完成。" << endl;
cout << "当前战士状态:" << endl;
cout << "生命值:" << w1.getHP() << endl;
cout << "魔法值:" << w1.getMP() << endl;
cout << "攻击力:" << w1.getAttack() << endl;
cout << "位置:(" << w1.getPos().getX() << "," << w1.getPos().getY() << ")" << endl;
cout << "装备:" << endl;
cout << "附加生命值:" << w1.getEquip().getAddHP() << endl;
cout << "附加魔法值:" << w1.getEquip().getAddMP() << endl;
cout << "附加攻击力:" << w1.getEquip().getAddAttack() << endl;
cout << endl;
Equipment newEquip(200, 100, 50);
w1.setEquip(newEquip);
cout << "更换装备完成。" << endl;
cout << "当前战士状态:" << endl;
cout << "生命值:" << w1.getHP() << endl;
cout << "魔法值:" << w1.getMP() << endl;
cout << "攻击力:" << w1.getAttack() << endl;
cout << "位置:(" << w1.getPos().getX() << "," << w1.getPos().getY() << ")" << endl;
cout << "装备:" << endl;
cout << "附加生命值:" << w1.getEquip().getAddHP() << endl;
cout << "附加魔法值:" << w1.getEquip().getAddMP() << endl;
cout << "附加攻击力:" << w1.getEquip().getAddAttack() << endl;
cout << endl;
Warrior w2(300, 100, 30, pos, equip);
cout << "另一角色初始化完成。" << endl;
cout << "当前角色状态:" << endl;
cout << "生命值:" << w2.getHP() << endl;
cout << "魔法值:" << w2.getMP() << endl;
cout << "攻击力:" << w2.getAttack() << endl;
cout << "位置:(" << w2.getPos().getX() << "," << w2.getPos().getY() << ")" << endl;
cout << "装备:" << endl;
cout << "附加生命值:" << w2.getEquip().getAddHP() << endl;
cout << "附加魔法值:" << w2.getEquip().getAddMP() << endl;
cout << "附加攻击力:" << w2.getEquip().getAddAttack() << endl;
cout << endl;
w1.attackWarrior(w2);
return 0;
}
```
输出结果:
```
初始化完成。
当前战士状态:
生命值:500
魔法值:200
攻击力:70
位置:(0,0)
装备:
附加生命值:100
附加魔法值:50
附加攻击力:20
更换装备完成。
当前战士状态:
生命值:500
魔法值:200
攻击力:120
位置:(0,0)
装备:
附加生命值:200
附加魔法值:100
附加攻击力:50
另一角色初始化完成。
当前角色状态:
生命值:300
魔法值:100
攻击力:50
位置:(0,0)
装备:
附加生命值:100
附加魔法值:50
附加攻击力:20
战士攻击了另一角色,对方被击败!
```
阅读全文