• YTU 2635: P4 游戏中的Human角色


    2635: P4 游戏中的Human角色

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 524  解决: 328

    题目描述

    在一个平面打斗游戏中,任何的角色(Role)都有血量(blood)和位置loc(此处loc是Location类的实例)属性。有了Role类,可以派生出不同的角色,如人、神仙、怪兽等。如下程序中,定义了Location类和Role类,人类(Human)中新增了姓名和攻击力数据成员,请为Human类设计成员函数,并实现Role类中的moveTo和addBlood两个成员函数。
    请在begin和end中间写下需要的代码。你只能编辑并提交begin和end之间的代码。
    #include <iostream>
    using namespace std;
    class Location
    {
    private:
        int x, y;
    public:
        Location(int a, int b):x(a),y(b) {}
        int getX(){return x;}
        int getY(){return y;}
        void setXY(int a,int b) {x=a;y=b;};  //设置位置坐标
    };
    class Role
    {
    public:
        Role(int rblood, int rx, int ry):blood(rblood),loc(rx,ry) {}
        void moveTo(int rx, int ry);  //移动到(rx, ty)处,要改变loc的值
        void addBlood(int b); //增加血量,参数为负时,代表减少
    protected:
        int blood;
        Location loc;
    };
    void Role::moveTo(int rx, int ry)
    {
    loc.setXY(rx,ry);
    }
    void Role::addBlood(int b) 
    {
    blood+=b;
    }
    //************* begin *****************
    class Human: public Role
    {
    public:
    private:
        string name;  // 姓名
        int attack;   // 攻击力
    };
    //************* end *****************
    int main()
    {
        string name;
        int att, blood, x, y;
        cin>>name>>att>>blood>>x>>y;
        Human hum(name,att,blood,x,y); //人name的攻击力att,血量blood,在(x,y)处
        hum.show();
        int incAtt, incBlood, newx, newy ;
        cin>>incAtt;
        cin>>incBlood;
        cin>>newx>>newy;
        hum.addAttack(incAtt);  //攻击力增加incAtt
        hum.addBlood(incBlood); //血量增加incBlood
        hum.moveTo(newx,newy);  //人移到了(news,newy)处
        hum.show();
        return 0;
    }

    输入

    第一行:名字 血量 攻击力 位置,其中位置处是两个整数,代表平面x、y坐标
    第二行:增加的攻击力
    第三行:要增加的血量
    第四行:新位置,用两个整数代表
    输入的各部分之间用空格隔开

    输出

    分别用两行显示游戏角色的初始状态和调整参数后的状态
    如“Avanda has 500 attack and 1000 blood in (4,3)”表示Avanda有500攻击力1000血量,在(4,3)位置处

    样例输入

    Avanda 500 1000 4 3
    -300
    200
    2 5

    样例输出

    Avanda has 500 attack and 1000 blood in (4,3)
    Avanda has 200 attack and 1200 blood in (2,5)

    迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……

    #include <iostream>
    using namespace std;
    class Location
    {
    private:
        int x, y;
    public:
        Location(int a, int b):x(a),y(b) {}
        int getX()
        {
            return x;
        }
        int getY()
        {
            return y;
        }
        void setXY(int a,int b)
        {
            x=a;
            y=b;
        };  //设置位置坐标
    };
    class Role
    {
    public:
        Role(int rblood, int rx, int ry):blood(rblood),loc(rx,ry) {}
        void moveTo(int rx, int ry);  //移动到(rx, ty)处,要改变loc的值
        void addBlood(int b); //增加血量,参数为负时,代表减少
    protected:
        int blood;
        Location loc;
    };
    void Role::moveTo(int rx, int ry)
    {
        loc.setXY(rx,ry);
    }
    void Role::addBlood(int b)
    {
        blood+=b;
    }
    class Human:public Role,public Location
    {
    public:
        Human(string na,int att,int rblood,int rx,int ry):Location(rx,ry),Role(rblood,rx,ry),name(na),attack(att) {}
        void show()
        {
            cout<<name<<" has "<<attack<<" attack and "<<blood<<" blood in ("<<getX()<<","<<getY()<<")"<<endl;
        }
        void addAttack(int a)
        {
            attack+=a;
        }
        void addBlood(int b)
        {
            Role::addBlood(b);
        }
        void moveTo(int rx,int ry)
        {
            Location::setXY(rx,ry);
        }
    private:
        string name;  // 姓名
        int attack;   // 攻击力
    };
    int main()
    {
        string name;
        int att, blood, x, y;
        cin>>name>>att>>blood>>x>>y;
        Human hum(name,att,blood,x,y); //人name的攻击力att,血量blood,在(x,y)处
        hum.show();
        int incAtt, incBlood, newx, newy ;
        cin>>incAtt;
        cin>>incBlood;
        cin>>newx>>newy;
        hum.addAttack(incAtt);  //攻击力增加incAtt
        hum.addBlood(incBlood); //血量增加incBlood
        hum.moveTo(newx,newy);  //人移到了(news,newy)处
        hum.show();
        return 0;
    }
    

  • 相关阅读:
    深入理解JVM内幕:从基本结构到Java 7新特性
    通过Java反射做实体查询
    Hadoop教程(一)
    很不错的js特效
    java utf8字符 导出csv 文件的乱码问题。
    spring MVC使用Interceptor做用户登录判断
    Bootstrap--全局css样式之图片
    Bootstrap-全局css样式之按钮
    Bootstrap--全局css样式之表单
    Bootstarp--全局CSS样式之表格
  • 原文地址:https://www.cnblogs.com/im0qianqian/p/5989509.html
Copyright © 2020-2023  润新知