6.25 被虐記 9 期末測試 玩遊戲

來源:互聯網
上載者:User

 


題目描述
在一個平面打鬥遊戲中,任何的角色(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)


class Human: public Role,public Location
{
public:
    Human(string n,int a,int b,int x,int y):Location(x,y),Role(b,x,y),name(n),attack(a) {}
    void show()
    {
        cout<<name<<" has "<<attack<<" attack and "<<blood<<" blood in ("<<getX()<<","<<getY()<<")";
        cout<<endl;
    }
    void addAttack(int n)
    {
        attack+=n;
    }
    void addBlood(int n)
    {
        blood+=n;
    }
    void moveTo(int xx,int yy)
    {
        Location::setXY(xx,yy);
    }
private:
    string name;  // 姓名
    int attack;   // 攻擊力
};

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.