*Copyright(c) 2016.煙臺大學計算機與控制工程學院
*ALL rights reserved.
*文件名稱:main.cpp
*作者:孫亞茹
*完成日期:2016年6月8日
*問題描述:閱讀博文”C++遊戲系列4:殺傷距離有限制“,回答相關問題。
*//
#include <iostream>
#include<cmath>
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(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); //構造函數
~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(); //顯示
private:
string name; //角色名稱
int blood; //當前血量
bool life; //是否活着
Point location; //位置
Weapon weapon; //武器
};
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):name(nam),blood(b),location(l),weapon(w)
{
if(blood>0)
life=true;
else
life=false;
}
Role::~Role()
{
cout<<name<<"退出江湖..."<<endl;
}
//吃東西,漲d血(死了後吃上東西可以復活)
void Role::eat(int d) //吃東西,漲d血(死了也能吃,別人喂的,以使能復活)
{
blood+=d;
if(blood>0)
life=true;
}
//攻擊別人,自己漲血,同時對方被攻擊失血,血量取決於當前用的武器
//在武器的攻擊範圍內纔可以攻擊
void Role::attack(Role &r) //攻擊別人,漲1血
{
if(isAlived()&&weapon.getKillRange()>this->distance(r)) //活着且在殺傷範圍內
{
blood+=weapon.getForce();
r.beAttack(weapon.getForce());
}
}
//被別人攻擊,參數f是承受的攻擊力
void Role::beAttack(int f)
{
blood-=f;
if(blood<=0)
life=false;
}
//返回與另一角色的距離
double Role::distance(Role &r)
{
return location.distance(r.location);
}
//是否活着
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 "<<weapon.getWname();
cout<<". He is in ("<<location.getX()<<", "<<location.getY()<<") and ";
if(isAlived())
cout<<"alived.";
else
cout<<"dead.";
cout<<endl;
}
int main( )
{
Weapon w1("Gold stick",200, 100), w2("Fire point gun",180,300);
Role wuKong("WuKong", 500, Point(0, 0), w1);
Role neZha("NeZha", 210, Point(30,30), w2);
cout<<"---begin---"<<endl;
wuKong.show();
neZha.show();
cout<<"---1st round---"<<endl;
wuKong.attack(neZha);
wuKong.show();
neZha.show();
cout<<"---2nd round---"<<endl;
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;
}
回答:
(1) location是Point類的對象;weapon是Weapon類的對象;
(2)由Role::Role(string nam, int b, Point l, Weapon w):name(nam),blood(b),location(l),weapon(w);可以看出在形參列表中也要有類的對象,而類的對象的數據類型是類的名稱,在定義時要列出類對象所屬類的形參表。
(3)攻擊行爲條件是:角色活着且在攻擊範圍內,攻擊行爲中攻擊者血數要增多,被攻擊者血數要減少,減少,增加血滴數取決於武器殺傷力。