有基类warrior和其派生类ninja dragon iceman wolf lion,现在有红蓝两个阵营,每个阵营都按照自己的顺序循环产生武士,并且红蓝之间有城市,武士会按照城市前进,如何保存武士的阵营信息并且更新武士的城市信息,在遍历城市时能找到处于该城市的武士,并且保证能够调用武士的成员c++
时间: 2024-02-17 08:00:27 浏览: 14
可以在Warrior类中添加一个camp属性,表示武士所属的阵营,同时在City类中添加两个指针,分别指向红方武士和蓝方武士,表示该城市中的武士。在遍历城市时,可以根据这两个指针判断该城市是否有武士,并且可以通过指针调用武士的成员函数。
以下是修改过的C++代码示例:
```c++
class Warrior {
public:
string name; // 武士名称
int hp; // 血量
int attack; // 攻击力
int defense; // 防御力
int camp; // 所属阵营
int id; // 武士ID
Warrior(string n, int h, int a, int d, int c, int i) {
name = n;
hp = h;
attack = a;
defense = d;
camp = c;
id = i;
}
void attack(Warrior* enemy) {
// 攻击敌人
}
void defense(Warrior* enemy) {
// 防御
}
void die() {
// 死亡
}
// 其他成员函数
};
class Ninja : public Warrior {
public:
// 特有属性和方法
};
class Dragon : public Warrior {
public:
// 特有属性和方法
};
class Iceman : public Warrior {
public:
// 特有属性和方法
};
class Wolf : public Warrior {
public:
// 特有属性和方法
};
class Lion : public Warrior {
public:
// 特有属性和方法
};
class RedCamp {
public:
int order[5]; // 武士产生顺序
int warriors[5]; // 各武士数量
int cur; // 当前产生的武士序号
RedCamp() {
// 初始化
}
Warrior* createWarrior() {
// 产生武士
}
// 其他成员函数
};
class BlueCamp {
public:
int order[5]; // 武士产生顺序
int warriors[5]; // 各武士数量
int cur; // 当前产生的武士序号
BlueCamp() {
// 初始化
}
Warrior* createWarrior() {
// 产生武士
}
// 其他成员函数
};
class City {
public:
int id; // 城市ID
int flag; // 旗帜所属阵营
Warrior* redWarrior; // 红方武士
Warrior* blueWarrior; // 蓝方武士
City(int i) {
id = i;
redWarrior = nullptr;
blueWarrior = nullptr;
}
// 其他成员函数
};
```