第8週プロジェクト4-キャラクターには多様な武器がある

5417 ワード

/* 

*Copyright(c)2016,               

*All rights reserved 

*    :123.cpp 

*   :   

*    :2016 4 19  

*     :v1.0 

* 

*    :        。 

*/

1.123.h:   
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#include <string>
using namespace std;
const int N=10; 
const int NOWEAPON=-1;  
class 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); 
    void attack(Role &r); 
    void beAttack(int f); 
    double distance(Role &r); 
    bool isAlived();
    void moveTo(int x, int y); 
    void move(int dx, int dy);
    void changeWeapon(int wno); 
    void show(); 
private:
    string name;  
    int blood;    
    bool life;    
    Point location;  
    Weapon weapons[N]; 
    int weaponNum;      
    int holdWeapon;     
};
#endif // GAME_H_INCLUDED

2.point.cpp,    ,    
#include "123.h"
#include <cmath>
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));
}

3.weapon.cpp,     
#include "123.h"
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;
}

4.role.cpp,     ,         
#include <iostream>
#include "123.h"
using namespace std;
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;
}
void Role::eat(int 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());
    }
}
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;
}

5.main.cpp,    ,    
#include <iostream>
#include "123.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.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;
}


学習の心得:マルチファイルでプログラムを組織する.