補充程序之遊戲系列— 4 遊戲中的角色類增強版 (2)



*Copyright(c) 2016.煙臺大學計算機與控制工程學院
*ALL rights  reserved.
*文件名稱:main.cpp
*作者:孫亞茹
*完成日期:2016年6月8日
*問題描述:在(1)的基礎上,將Weapon類的數據成員改爲數組,以支持一個角色可以帶多件武器的需求,改造和增加數據成員和成員函數。
*//

#include <iostream>
#include<cmath>
const int N=10; //每個角色最多擁有的武器
const int NOWEAPON=-1;
using namespace std;

class Point     //Point類聲明
{
public: //外部接口
    Point(int x=0, int y=0);
    int getX();
    int getY();
    double distance(const Point &p);  //返回與另外一點p之間的距離
    void moveTo(int x, int y); //移到另外一點
    void move(int dx, int dy); //從當前位置移動
private:
    int x, y;  //座標
};

class Weapon
{
public:
    Weapon() {};
    Weapon(string wnam, int f, double k);
    Weapon(const Weapon&);
    string getWname();
    int getForce();         //返回殺傷力
    double getKillRange();  //返回殺傷距離
private:
    string wname;   //名稱
    int force;       //殺傷力
    double killRange;   //殺傷距離
};

class Role
{
public:
    Role(string nam, int b, Point l, Weapon w[], int n); //構造函數
    ~Role(); //析構函數
    void eat(int d); //喫東西,漲d血(死了後喫上東西可以復活)
    void attack(Role &r); //攻擊別人,自己漲血,同時對方被攻擊失血。血量取決於當前用的武器
    void beAttack(int f); //被別人攻擊,參數f是承受的攻擊力
    double distance(Role &r); //返回與另一角色的距離
    bool isAlived(); //是否活着
    void moveTo(int x, int y); //移到另外一點
    void move(int dx, int dy); //從當前位置移動
    void show(); //顯示
    void changeWeapon(int wno);
private:
    string name;  //角色名稱
    int blood;    //當前血量
    bool life;    //是否活着
    Point location;  //位置
    Weapon weapons[N];
    int weaponNum;
    int holdWeapon;
};

Point::Point(int x, int y): x(x), y(y) { }
int Point::getX()
{
    return x;
}
int Point::getY()
{
    return y;
}
//移到另外一點
void Point::moveTo(int x, int y)
{
    this->x=x;
    this->y=y;
}
//從當前位置移動
void Point::move(int dx, int dy)
{
    this->x+=dx;
    this->y+=dy;
}
double Point::distance(const Point& p)
{
    double dx = this->x - p.x;
    double dy = this->y - p.y;
    return (sqrt(dx * dx + dy * dy));
}

Weapon::Weapon(string wnam, int f, double k):wname(wnam),force(f),killRange(k) {}
Weapon::Weapon(const Weapon &w):wname(w.wname),force(w.force),killRange(w.killRange) {}
string Weapon::getWname()
{
    return wname;
}

//返回殺傷力
int Weapon::getForce()
{
    return force;
}
//返回殺傷距離
double Weapon::getKillRange()
{
    return killRange;
}


Role::Role(string nam, int b, Point l, Weapon w[], int n)
    :name(nam),blood(b),location(l),weaponNum(n),holdWeapon(NOWEAPON)
{
    if(blood>0)
        life=true;
    else
        life=false;
    for(int i=0; i<n; i++)
        weapons[i]=w[i];
}

Role::~Role()
{
    cout<<name<<"退出江湖..."<<endl;
}

//喫東西,漲d血(死了後喫上東西可以復活)
void Role::eat(int d) //喫東西,漲d血(死了也能喫,別人喂的,以使能復活)
{
    blood+=d;
    if(blood>0)
        life=true;
}

//攻擊別人,自己漲血,同時對方被攻擊失血,血量取決於當前用的武器
//在武器的攻擊範圍內纔可以攻擊
void Role::attack(Role &r)
{
    if(isAlived()&&holdWeapon>NOWEAPON&&weapons[holdWeapon].getKillRange()>this->distance(r)) //活着且在殺傷範圍內
    {
        blood+=weapons[holdWeapon].getForce();
        r.beAttack(weapons[holdWeapon].getForce());
    }
}


//被別人攻擊,參數f是承受的攻擊力
void Role::beAttack(int f)
{
    blood-=f;
    if(blood<=0)
        life=false;
}

//返回與另一角色的距離
double Role::distance(Role &r)
{
    return location.distance(r.location);
}
void Role::changeWeapon(int wno)
{
    if(wno<weaponNum)
        holdWeapon=wno;
}

//是否活着
bool Role::isAlived()
{
    return life;
}
//移到另外一點
void Role::moveTo(int x, int y)
{
    if(isAlived())
        location.moveTo(x,y);
}
//從當前位置移動
void Role::move(int dx, int dy)
{
    if(isAlived())
        location.move(dx,dy);
}
//顯示
void Role::show()
{
    cout<<name<<" has "<<blood<<" blood, hold ";
    if(holdWeapon==NOWEAPON)
        cout<<"no weapon";
    else
        cout<<weapons[holdWeapon].getWname();
    cout<<". He is in ("<<location.getX()<<", "<<location.getY()<<") and ";
    if(isAlived())
        cout<<"alived.";
    else
        cout<<"dead.";
    cout<<endl;
}


int main( )
{
    Weapon w1[1]= {Weapon("Gold stick",200, 100)}; //金箍棒
    Weapon w2[3]= {Weapon("Fire-Tip Lance",180,300), //火尖槍
                   Weapon("Universal Ring",100,500), //乾坤圈
                   Weapon("Sky Muddling Damask",50,1000) //混天綾
                  };

    Role wuKong("WuKong", 500, Point(0, 0), w1,1);
    Role neZha("NeZha", 210, Point(30,30), w2,3);
    wuKong.changeWeapon(0);
    neZha.changeWeapon(0);
    cout<<"---begin---"<<endl;
    wuKong.show();
    neZha.show();
    cout<<"---1st round---"<<endl;
    wuKong.attack(neZha);
    wuKong.show();
    neZha.show();
    cout<<"---2nd round---"<<endl;
    neZha.changeWeapon(2);
    neZha.attack(wuKong);
    wuKong.show();
    neZha.show();
    cout<<"---3rd round---"<<endl;
    neZha.moveTo(100,100); //哪吒走開,悟空打不到哪吒
    wuKong.attack(neZha);
    wuKong.show();
    neZha.show();
    cout<<"---4th round---"<<endl; //這個距離在火尖槍的射程內
    neZha.attack(wuKong);
    wuKong.show();
    neZha.show();
    cout<<"---then---"<<endl;  //哪吒一直打,悟空慘了
    neZha.attack(wuKong);
    neZha.attack(wuKong);
    wuKong.attack(neZha);
    wuKong.show();
    neZha.show();
    cout<<"---end---"<<endl;
    return 0;
}

總結:

          考查的是對象數組做數據成員,這時構造函數的定義要改變:  Role::Role(string nam, int b, Point l, Weapon w[], int n)
    :name(nam),blood(b),location(l),weaponNum(n),holdWeapon(NOWEAPON)
{
    if(blood>0)
        life=true;
    else
        life=false;
    for(int i=0; i<n; i++)
        weapons[i]=w[i];
}

           在對象數組創建時要初始化數組中的每一個對象:   Weapon w1[1]= {Weapon("Gold stick",200, 100)}; //金箍棒
    Weapon w2[3]= {Weapon("Fire-Tip Lance",180,300), //火尖槍
                   Weapon("Universal Ring",100,500), //乾坤圈
                   Weapon("Sky Muddling Damask",50,1000) //混天綾
                  };

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章