请用C++编程语言,利用面向对象的程序设计方法中多态的编程方法实现一个机器人混战模拟程。 程序功能:模拟实现5种机器人互相攻击的场景(输出机器人初始时的名称、生命值、 弹药数量、攻击力等信息,然后让机器人互相各攻击至少一次,再输出机器人互相攻击后的 名称、生命值、弹药数量、攻击力等信息),其中每个机器人包括机器人名称、生命值、弹 药数量、攻击力等属性,每个机器人还要具备攻击和受到攻击的行为,攻击力指每攻击一次 使对方生命值减少的值,这个值可自行制定。 参考建议:机器人可分为英雄机器人、步兵机器人、哨兵机器人、侦查机器人、巡逻机 器人。机器人生命值默认100。
时间: 2023-12-22 07:04:38 浏览: 26
以下是一个C++实现的机器人混战模拟程序,利用了面向对象的多态编程方法:
```
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
// 机器人基类
class Robot {
public:
Robot(string n, int h, int a, int b) : name(n), health(h), ammo(a), attack(b) {}
virtual void attackRobot(Robot& r) {
int damage = rand() % attack + 1; // 计算攻击造成的伤害值
cout << name << " attacks " << r.name << " and deals " << damage << " damage." << endl;
r.health -= damage;
ammo--;
}
virtual void receiveAttack(int damage) {
cout << name << " is attacked and suffers " << damage << " damage." << endl;
health -= damage;
}
virtual void showStatus() {
cout << name << " (" << health << " health, " << ammo << " ammo, " << attack << " attack)" << endl;
}
protected:
string name;
int health;
int ammo;
int attack;
};
// 英雄机器人类
class HeroRobot : public Robot {
public:
HeroRobot(string n) : Robot(n, 100, 10, 20) {}
};
// 步兵机器人类
class InfantryRobot : public Robot {
public:
InfantryRobot(string n) : Robot(n, 100, 30, 5) {}
};
// 哨兵机器人类
class SentryRobot : public Robot {
public:
SentryRobot(string n) : Robot(n, 100, 20, 10) {}
};
// 侦查机器人类
class ScoutRobot : public Robot {
public:
ScoutRobot(string n) : Robot(n, 100, 15, 15) {}
};
// 巡逻机器人类
class PatrolRobot : public Robot {
public:
PatrolRobot(string n) : Robot(n, 100, 25, 8) {}
};
int main() {
srand(time(0)); // 初始化随机数生成器
// 创建5个不同类型的机器人
HeroRobot hero("Hero");
InfantryRobot infantry("Infantry");
SentryRobot sentry("Sentry");
ScoutRobot scout("Scout");
PatrolRobot patrol("Patrol");
// 输出机器人信息
cout << "Initial robot status:" << endl;
hero.showStatus();
infantry.showStatus();
sentry.showStatus();
scout.showStatus();
patrol.showStatus();
// 让机器人互相攻击至少1次
cout << endl << "Robot battle starts:" << endl;
for (int i = 0; i < 5; i++) {
Robot* attacker;
Robot* receiver;
// 随机选择攻击者和受攻击者
int a = rand() % 5;
int b = rand() % 5;
while (b == a) {
b = rand() % 5;
}
switch (a) {
case 0:
attacker = &hero;
break;
case 1:
attacker = &infantry;
break;
case 2:
attacker = &sentry;
break;
case 3:
attacker = &scout;
break;
case 4:
attacker = &patrol;
break;
}
switch (b) {
case 0:
receiver = &hero;
break;
case 1:
receiver = &infantry;
break;
case 2:
receiver = &sentry;
break;
case 3:
receiver = &scout;
break;
case 4:
receiver = &patrol;
break;
}
attacker->attackRobot(*receiver);
if (receiver->health <= 0) {
cout << receiver->name << " is destroyed!" << endl;
}
receiver->attackRobot(*attacker);
if (attacker->health <= 0) {
cout << attacker->name << " is destroyed!" << endl;
}
}
// 输出机器人信息
cout << endl << "Final robot status:" << endl;
hero.showStatus();
infantry.showStatus();
sentry.showStatus();
scout.showStatus();
patrol.showStatus();
return 0;
}
```
以上程序定义了一个Robot基类和5个派生类,每个派生类对应一个机器人类型,它们分别实现了攻击和受攻击的行为。程序随机选择两个机器人进行攻击,直到每个机器人都至少攻击了一次。最后输出每个机器人的最终状态。