代码拉取完成,页面将自动刷新
// 2个随机械兽
// 3种行为
// 更新=>
// 新增速度属性
// 解决创建机械兽是不能随机的问题
// 准备阶段不临时提升属性值
// 战斗阶段按照速度决定先后顺序
// 战斗阶段按动作提示属性值
// 临时提升的属性值,只有使用才会在结束阶段回落
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;
class MechanicalAnimal {
public:
MechanicalAnimal(int hp, int at, int de ,int sp)
: health(hp), attack(at), defense(de), speed(sp){
setUpToZero();
}
MechanicalAnimal()
{
health = rand()%6 + 15;
attack = rand()%7 + 2;
defense = 10 - attack;
speed = rand()%6;
setUpToZero();
}
void prepare() {
int r = rand() % 9;
cout << " 选择";
if (r < 2)
{
action = 0;
cout << "攻击";
}
else if (r< 6)
{
action = 1;
cout << "防御";
}
else if (r<7)
{
action = 2;
cout << "恢复";
}
else
{
action = 4;
cout << "加速";
}
cout << endl;
}
void attackTo(MechanicalAnimal& target) {
cout << "机械兽进行了攻击" << endl;
int value = rand()%6 + 2;
up_attack += value;
attack += value;
cout << " 攻击准备,攻击力临时提升"
<< value << endl;
int damage = attack - target.defense;
// cout <<"damage = atk - target.def = "
// << attack << " - " << target.defense << " = "
// << damage << endl;
if (target.action!=1)
{
cout << " 攻击成功,目标没有防御!" << endl;
if (damage>0)
{
target.health -= damage;
cout << " 攻击大于目标防御," << endl
<< " 目标扣除"
<< damage << "血量" << endl;
}
else
{
health += -damage;
cout << " 攻击小于目标防御," << endl
<< " 击破防御,没有伤害,攻击保留!" << endl;
target.defense -= target.up_defense;
target.up_defense = 0;
}
}
if (target.action==1)
{
cout << " 攻击失败,目标进行了防御!" << endl;
if (damage>0)
{
cout << " 攻击大于目标防御," << endl
<< " 击破防御,没有伤害,攻击回落!" << endl;
target.defense -= target.up_defense;
target.up_defense = 0;
attack -= up_attack;
up_attack = 0;
}
else
{
health -= -damage;
cout << " 攻击小于目标防御," << endl
<< " 目标反击,机械兽扣除"
<< -damage << "血量" << endl;
}
}
}
void defendFrom(MechanicalAnimal &target) {
cout << "机械兽进行了防御" << endl;
int value = rand()%6 + 2;
up_defense += value;
defense += value;
cout << " 防御准备,防御力临时提升"
<< value << endl;
}
void recover() {
int heal = rand()%8 + 3;
health += heal;
cout << "机械兽进行了恢复," << endl
<< " 生命值恢复了"
<< heal << "点!" << endl;
}
void speedUp() {
cout << "机械兽进行了加速" << endl;
int value = rand()%6 + 2;
up_speed += value;
speed += value;
cout << " 速度增加了" << value
<< ",变成" << speed << endl;
}
void fightWith(MechanicalAnimal &target)
{
switch (action)
{
case 0:
attackTo(target);
break;
case 1:
defendFrom(target);
break;
default:
recover();
break;
}
}
void end()
{
}
int getAttack() const {
return attack;
}
int getDefense() const {
return defense;
}
int getHealth() const {
return health;
}
void show()
{
cout << "[HP" << health << "]["
<< "AT/DE SP" << attack
<< "/" << defense
<< " "<< speed << "]"
<<"[UA/UD US][" << up_attack
<< "/" << up_defense
<< " " << up_speed
<< "]";
}
int action; // 0攻击, 1防御, 2恢复
int speed;
private:
int attack;
int defense;
int health;
int up_attack;
int up_defense;
int up_speed;
void setUpToZero()
{
up_defense = 0;
up_attack = 0;
up_speed = 0;
}
};
int main() {
MechanicalAnimal A, B;
MechanicalAnimal *p, *t;
string p1Name, p2Name;
int ifSpSwapped = 0;
int round = 1;
int stage = 1;
MechanicalAnimal *p1, *p2;
while (A.getHealth() > 0 && B.getHealth() > 0) {
cout << "[---------第" << round << "回合-----]" << endl;
cout << "A";
A.show();
cout << "\nB";
B.show();
cout << endl;
cout << "=======准备阶段========" << endl;
cout << "A";
A.prepare();
cout << "B";
B.prepare();
/*
cout << "A";
A.show();
cout << "\nB";
B.show();
cout << endl;
*/
cout << "=======战斗阶段=======" <<endl;
if (A.speed > B.speed)
{
p1 = &A;
p2 = &B;
p1Name = "A";
p2Name = "B";
}
else
{
p2 = &A;
p1 = &B;
p1Name = "B";
p2Name = "A";
}
cout << p1Name;
p1->fightWith(*p2);
cout << p2Name;
p2->fightWith(*p1);
cout << "A";
A.show();
cout << "\nB";
B.show();
cout << endl;
cout << "=======结束阶段=======" << endl;
A.end();
B.end();
cout << endl;
if (round>60)
{
cout << "回合数>60,超时!" << endl;
break;
}
round++;
}
if (A.getHealth()<=0 && B.getHealth()<=0)
cout << "两败俱伤!" << endl;
else if (A.getHealth()>B.getHealth())
cout << "机械兽A赢了!" << endl;
else if (A.getHealth()<B.getHealth())
cout << "机械兽B赢了!" << endl;
else
cout << "平局!" << endl;
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。