OOP双人决斗(多重继承)

题目描述

写一个Node2D基类,属性有位置location(string)

一个Body子类继承自Node2D,属性当前生命值health(int),防御力defense(int)

一个Weapon子类也继承自Node2D,属性有武器名w_name(string),伤害damage(int)

一个Player多继承自Body和Weapon,属性有名字name(string)。方法有attack,对目标造成伤害

在主函数创建两个Player,p1、p2,判断在p1首先开始攻击的情况下谁会获胜。

我们规定attack中,每次对目标生命力造成的伤害等于damage减去目标的defense。

输入

输入:地点location,玩家1的名字、生命值、防御力、武器名、武器伤害,玩家2的名字、生命值、防御力、武器名、武器伤害

输出

输出:获胜信息

----------------------------------------------

输入示例:

palace
p1 30 5 bow 30
p2 50 10 sword 20

输出示例:

p1 deal 20 damage to p2
p2 still have 30 health

p2 deal 15 damage to p1
p1 still have 15 health

p1 deal 20 damage to p2
p2 still have 10 health

p2 deal 15 damage to p1
p2 defeated p1 by sword in palace

AC代码

#include "bits/stdc++.h"
using namespace std;

class Node2D
{
protected:
    string location;

public:
    Node2D(string loc) : location(loc) {}
};

class Body : virtual public Node2D
{
private:
    int health;
    int defense;

public:
    Body(string loc, int h, int d) : Node2D(loc), health(h), defense(d) {}

    int getHealth() const { return health; }
    void setHealth(int h) { health = h; }
    int getDefense() const { return defense; }
};
class Weapon : virtual public Node2D
{
protected:
    string w_name;
    int damage;

public:
    Weapon(string loc, string name, int dmg) : Node2D(loc), w_name(name), damage(dmg) {}
};

class Player : public Body, public Weapon
{
private:
    string name;

public:
    Player(string loc, string n, int h, int d, string wn, int dmg)
        : Node2D(loc), Body(loc, h, d), Weapon(loc, wn, dmg), name(n) {}

    void attack(Player &target)
    {
        int actual_damage = damage - target.getDefense();
        actual_damage = actual_damage > 0 ? actual_damage : 0;
        target.setHealth(target.getHealth() - actual_damage);
        cout << name << " deal " << actual_damage << " damage to " << target.name << endl;
        if (target.getHealth() > 0)
        {
            cout << target.name << " still have " << target.getHealth() << " health" << endl;
        }
        else
        {
            cout << name << " defeated " << target.name << " by " << w_name << " in " << location << endl;
        }
    }
};

int main()
{
    string location, p1_name, p1_weapon, p2_name, p2_weapon;
    int p1_health, p1_defense, p1_damage, p2_health, p2_defense, p2_damage;

    cin >> location;
    cin >> p1_name >> p1_health >> p1_defense >> p1_weapon >> p1_damage;
    cin >> p2_name >> p2_health >> p2_defense >> p2_weapon >> p2_damage;

    Player p1(location, p1_name, p1_health, p1_defense, p1_weapon, p1_damage);
    Player p2(location, p2_name, p2_health, p2_defense, p2_weapon, p2_damage);

    while (p1.getHealth() > 0 && p2.getHealth() > 0)
    {
        p1.attack(p2);
        cout << endl;
        if (p2.getHealth() > 0)
        {
            p2.attack(p1);
            cout << endl;
        }
    }

    return 0;
}

  • 19
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

szuzhan.gy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值