实现一个weapon类和如下功能,现有warrior类和其派生类dragon类,iceman,ninja,wolf,lion编号为n的dragon降生时即获得编号为n%3 的武器,编号为n的ninjia降生时即获得编号为n%3 和(n+1)%3的武器,编号为n的iceman降生时即获得编号为n%3 的武器,编号为n的lion降生时即获得编号为n%3 的武器,在红蓝双方中间的城市会发生战斗,每对相遇的武士会按照它们手中武器的顺序战斗,bomb用一次就没了,arrow用两次就没了,还有sword武器,优先用没用过的arrowc++具体实现
时间: 2024-02-19 11:01:00 浏览: 19
好的,根据你的要求,我来为你设计实现一个weapon类,并实现上述功能。
首先,我们需要定义weapon类,它应该具有以下属性:
- weapon_type:武器类型,可以是sword,bomb或arrow。
- weapon_strength:武器强度,可以是任意数字。
- remaining_times:剩余使用次数,对于bomb和arrow武器,使用一次后就没了;对于sword武器,可以无限使用。
代码如下:
```cpp
enum WeaponType {
SWORD,
BOMB,
ARROW
};
class Weapon {
public:
WeaponType weapon_type;
int weapon_strength;
int remaining_times;
Weapon(WeaponType weapon_type, int weapon_strength, int remaining_times) {
this->weapon_type = weapon_type;
this->weapon_strength = weapon_strength;
this->remaining_times = remaining_times;
}
};
```
接下来,我们需要修改Warrior类和它的派生类,使它们能够获得对应编号的武器。我们可以在Warrior的构造函数中实现这个功能。代码如下:
```cpp
class Warrior {
public:
int id;
int strength;
int loyalty;
int pos;
Weapon* weapons[3];
int num_weapons;
Warrior(int id, int strength, int loyalty, int pos) {
this->id = id;
this->strength = strength;
this->loyalty = loyalty;
this->pos = pos;
this->num_weapons = 0;
}
void add_weapon(Weapon* weapon) {
if (num_weapons < 3) {
weapons[num_weapons++] = weapon;
}
}
};
class Dragon : public Warrior {
public:
Dragon(int id, int strength, int loyalty, int pos) : Warrior(id, strength, loyalty, pos) {
WeaponType weapon_types[3] = { SWORD, BOMB, ARROW };
int weapon_ids[3] = { id % 3, (id + 1) % 3, (id + 2) % 3 };
for (int i = 0; i < 3; i++) {
add_weapon(new Weapon(weapon_types[weapon_ids[i]], strength * 0.2, 1));
}
}
};
class Ninja : public Warrior {
public:
Ninja(int id, int strength, int loyalty, int pos) : Warrior(id, strength, loyalty, pos) {
WeaponType weapon_types[3] = { SWORD, BOMB, ARROW };
int weapon_ids[2] = { id % 3, (id + 1) % 3 };
for (int i = 0; i < 2; i++) {
add_weapon(new Weapon(weapon_types[weapon_ids[i]], strength * 0.2, 1));
}
}
};
class Iceman : public Warrior {
public:
Iceman(int id, int strength, int loyalty, int pos) : Warrior(id, strength, loyalty, pos) {
WeaponType weapon_types[3] = { SWORD, BOMB, ARROW };
int weapon_ids[3] = { id % 3, (id + 1) % 3, (id + 2) % 3 };
for (int i = 0; i < 3; i++) {
add_weapon(new Weapon(weapon_types[weapon_ids[i]], strength * 0.2, 1));
}
}
};
class Lion : public Warrior {
public:
Lion(int id, int strength, int loyalty, int pos) : Warrior(id, strength, loyalty, pos) {
WeaponType weapon_types[3] = { SWORD, BOMB, ARROW };
int weapon_ids[3] = { id % 3, (id + 1) % 3, (id + 2) % 3 };
for (int i = 0; i < 3; i++) {
add_weapon(new Weapon(weapon_types[weapon_ids[i]], strength * 0.2, 1));
}
}
};
```
最后,我们需要在战斗过程中按照武器顺序进行攻击。我们可以将Warrior类中的weapons数组按照武器的优先级排序,然后在攻击的时候依次使用武器。代码如下:
```cpp
bool cmp_weapon(const Weapon* weapon1, const Weapon* weapon2) {
if (weapon1->weapon_type != weapon2->weapon_type) {
return weapon1->weapon_type < weapon2->weapon_type;
} else {
return weapon1->weapon_strength > weapon2->weapon_strength;
}
}
class City {
public:
Warrior* red_warrior;
Warrior* blue_warrior;
void fight() {
sort(red_warrior->weapons, red_warrior->weapons + red_warrior->num_weapons, cmp_weapon);
sort(blue_warrior->weapons, blue_warrior->weapons + blue_warrior->num_weapons, cmp_weapon);
for (int i = 0; i < red_warrior->num_weapons; i++) {
Weapon* weapon = red_warrior->weapons[i];
if (weapon->remaining_times > 0) {
switch (weapon->weapon_type) {
case SWORD:
blue_warrior->strength -= weapon->weapon_strength;
weapon->remaining_times = -1;
break;
case BOMB:
if (blue_warrior->num_weapons > 0 && blue_warrior->weapons[0]->weapon_type == SWORD) {
blue_warrior->strength -= min(2 * weapon->weapon_strength, blue_warrior->strength);
red_warrior->strength -= min(2 * blue_warrior->weapons[0]->weapon_strength, red_warrior->strength);
blue_warrior->weapons[0]->remaining_times = -1;
weapon->remaining_times = -1;
} else {
blue_warrior->strength -= min(weapon->weapon_strength, blue_warrior->strength);
weapon->remaining_times = -1;
}
break;
case ARROW:
blue_warrior->strength -= min(weapon->weapon_strength, blue_warrior->strength);
weapon->remaining_times--;
break;
}
}
}
for (int i = 0; i < blue_warrior->num_weapons; i++) {
Weapon* weapon = blue_warrior->weapons[i];
if (weapon->remaining_times > 0) {
switch (weapon->weapon_type) {
case SWORD:
red_warrior->strength -= weapon->weapon_strength;
weapon->remaining_times = -1;
break;
case BOMB:
if (red_warrior->num_weapons > 0 && red_warrior->weapons[0]->weapon_type == SWORD) {
red_warrior->strength -= min(2 * weapon->weapon_strength, red_warrior->strength);
blue_warrior->strength -= min(2 * red_warrior->weapons[0]->weapon_strength, blue_warrior->strength);
red_warrior->weapons[0]->remaining_times = -1;
weapon->remaining_times = -1;
} else {
red_warrior->strength -= min(weapon->weapon_strength, red_warrior->strength);
weapon->remaining_times = -1;
}
break;
case ARROW:
red_warrior->strength -= min(weapon->weapon_strength, red_warrior->strength);
weapon->remaining_times--;
break;
}
}
}
}
};
```
这样,我们就完成了题目要求的功能。当然,这只是一个简单的实现,实际上还有很多细节需要考虑,比如武器的使用顺序、武器的损耗、战斗胜负的判断等等。