第8周项目4——游戏中的角色类增强版

来源:互联网 发布:db数据库 编辑:程序博客网 时间:2024/05/17 23:20
/**Copyright (c) 2016,烟台大学计算机学院*All rights reserved.*文件名称 :*作    者 : 刘云*完成日期 : 2016年4月17号*版 本 号 : v1.0**问题描述 :  数组做数据成员*输入描述 :   无*程序输出 :*/<p>#include<iostream>#include<cmath>using namespace std;#define N 3class Role;class Weapon{public: friend class Role; Weapon(); string select_weapon();private: string weapon[5];};Weapon::Weapon(){    string w1="AK47-黑武士";string w2="MA41-死神";string w3="M4A1-牡丹";string w4="M14EBR-金牛座";string w5="AK-47-牡丹";    weapon[0]=w1;    weapon[1]=w2;    weapon[2]=w3;    weapon[3]=w4;    weapon[4]=w5;}string Weapon::select_weapon(){</p><p>    int n;    cout<<"Please select your weapon number: ";    cin>>n;    return weapon[n];</p><p>}class Point{public:</p><p> void setpoint(); double distance(Point &p1,Point &p2);private:    int x; int y;</p><p>};</p><p>/*void Point::setpoint(Point &p){    int a,b;    cin>>a>>b;    p.x=a;    p.y=b;}*/double Point::distance(Point &p1,Point &p2){    double dx,dy,distance1;    dx=p1.x-p2.x;    dy=p1.y-p2.y;    distance1=sqrt(dx*dx+dy*dy);    return distance1;}class Role{public: Role();//构造函数 ~Role(); void boss_attack(Role &r); void eat(int d);//吃东西,涨d血(死了以后吃上东西可以复活) void attack(Role &r); void beattack();//被别人攻击    void judge_alive();    bool isAlived();//是否活着 void show();//显示 void role_hold_weapon(Role &r); void show_weapon(Role &r); int judge_attack_range();private: string name;//角色名称 int blood;  //当前血量 bool life; //是否活着    int holdnum;    Weapon role_weapon;    string irole_weapon[5];    Point p1,p2;    double distance;};//***************************************************************/*int Role::judge_attack_range(){    cout<<"Please put in the position of both role: "<<endl;    cout<<"Please put in the position of the first point:";    p1.setpoint(p1);    p2.setpoint(p2);    if(p1.distance(p1,p2)<50)        return 1;    else        return 0;</p><p>}*///**************************************************************Role::Role(){    cout<<"Please put in role's name:";    cin>>name;    blood=10;}bool Role::isAlived(){    if(blood>0)        life=true;    else        life=false;    return life;}void Role::judge_alive(){    if(!isAlived())    {        cout<<name;        cout<<"   you have no energy . you must need to get things to  eat !!!"<<endl;    }</p><p>}Role::~Role(){    cout<<"***"<<name<<"***已经退出江湖!"<<endl;}void Role::beattack(){    blood=blood-2;    cout<<name<<"   You are be attack!!!"<<"   "<<"You have lose 2 blood!!!"<<endl;}void Role::attack(Role &r){    blood=blood-1;    r.blood=r.blood+1;    cout<<name<<" is so strong!!!  "<<r.name<<" has been hurt"<<endl;}void Role::show(){</p><p>    if(isAlived())    {        cout<<"the role is alived!!!"<<endl;        cout<<name<<"  的血量为:"<<blood<<endl;    }    else        cout<<name<<"has been died..."<<endl;</p><p>}void Role::eat(int d){    blood=blood+d;    cout<<name<<" has put in "<<d<<" blood"<<endl;}void Role::boss_attack(Role &r){    r.blood=0;}void Role::role_hold_weapon(Role &r){    int number,i;    cout<<"Please put in your role hold the number of weapon :";    cin>>number;    holdnum=number;    cout<<" 0 : AK47-黑武士 2 : MA41-死神 3 : M4A1-牡丹 \n 4 : M14EBR-金牛座 5 : AK-47-牡丹"<<endl;    for(i=0;i<number;i++)    {        r.irole_weapon[i]=role_weapon.select_weapon();            cout<<" select successfully!!!"<<endl;    }}void Role::show_weapon(Role &r){    int i;    cout<<name<<" have the weapons are ";    for(i=0;i<holdnum;i++)    {        cout<<r.irole_weapon[i]<<" ";    }    cout<<endl;}int main(){    int i=0;    Role role[5];    role[1].attack(role[2]);    role[4].beattack();    role[3].eat(3);    for(i=0;i<5;i++)        role[i].beattack();    role[0].boss_attack(role[0]);    role[0].judge_alive();    for(i=0;i<5;i++)          role[i].show();    role[0].role_hold_weapon(role[0]);    role[0].show_weapon(role[0]);    return 0;}
心得;

学会了对函数的切实应用。

运行结果:



1 0
原创粉丝点击