(4-1)阅读博文“C++游戏系列4:杀伤距离有限制,回答下面的问题:
(1)Role类的数据成员中,有哪些成员上类的对象?
loaction和weapon
(2)观察Role类的构造函数,说出:若某类的“数据成员为别的类的对象”,其构造函数定义时的要点有什么?
构造函数的形参列表中必须包含别的类中构造函数的形参
(3) 仔细研读Role::attack成员函数,说出攻击行为的条件,以及攻击行为中要做的事情。
活着且在杀伤范围内
知识点:对象数组作为数据成员
改进:每个角色所持有的武器不只一件,故持有的武器,用了对象数组来表示,当然,也可以是空手。由此而带来的,还得记录一共有几件武器,当前手持哪种武器。
(1)game.h:类声明
#ifndef GAME_H_INCLUDED #define GAME_H_INCLUDED #include <string> using namespace std; const int N=10; const int NOWEAPON=-1; class Weapon { private: string wname; int force; double killingRange; public: Weapon(){}; Weapon(string nam,int f,double k):wname(nam),force(f),killingRange(k){} Weapon(const Weapon &w):wname(w.wname),force(w.force),killingRange(w.killingRange){} string get_name() { return wname; } int get_force() { return force; } double get_killingRange() { return killingRange; } }; class Point { private: int x,y; public: Point(int xx=0,int yy=0):x(xx),y(yy){} int get_x() { return x; } int get_y() { return y; } void move_to(int x,int y); void move(int dx,int dy); double distance(const Point &p); }; class Role { private: string name; int blood; bool life; Point location; Weapon weapons[N]; int weaponNum; int holdWeapon; public: Role(string nam,int b,Point l,Weapon w[],int n); void eat(int n); bool is_lived(); void attack(Role &); void be_attacked(int f); void move(int dx,int dy); void move_to(int x,int y); double distance(Role &); void changeWeapon(int wno); void show(); }; #endif // GAME_H_INCLUDED
(2)point.cpp
#include"game.h" #include<cmath> void Point::move(int dx,int dy) { this->x+=dx; this->y+=dy; } void Point::move_to(int x,int y) { this->x=x; this->y=y; } double Point::distance(const Point &p) { return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)); }(3)Role .cpp
#include<iostream> #include"game.h" 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]; } } void Role::eat(int n) { if(is_lived()) blood+=n; } bool Role::is_lived() { if(blood>0) return true; else return false; } void Role::attack(Role &r) { if(is_lived()&&holdWeapon>NOWEAPON&&r.weapons[weaponNum].get_killingRange()>this->distance(r)) { blood+=weapons[holdWeapon].get_force(); r.blood-=weapons[holdWeapon].get_force(); } } void Role::be_attacked(int f) { if(is_lived()) { blood-=f; } } void Role::move(int dx,int dy) { if(is_lived()) { location.move(dx,dy); } } void Role::move_to(int x,int y) { if(is_lived()) { location.move_to(x,y); } } double Role::distance(Role &r) { return location.distance(r.location); } void Role::changeWeapon(int wno) { if(wno<weaponNum) holdWeapon=wno; } void Role::show() { cout<<name<<" has "<<blood<<" bloods,hold "; if(holdWeapon==NOWEAPON) cout<<"no weapon."; else cout<<weapons[holdWeapon].get_name()<<"."; cout<<"He is "; if(is_lived()) cout<<"alived"<<endl; else cout<<"died"<<endl; }
#include <iostream> #include "game.h" using namespace std; 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(); return 0; }运行结果:
测试函数(2)
#include <iostream> #include "game.h" using namespace std; 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.move_to(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类和Point类分别定义move和move_to函数,在求与另一角色的距离时只需调用Point类成员location的成员函数distance
注意武器类对象数组的初始化{Weapon(),Weapon()}