您的位置:首页 > 其它

第八周上机实践项目——项目4-游戏中的角色类增强版-问题(3)

2016-04-22 21:48 162 查看
/*
*Copyright (c)2016,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:main.cpp
*作    者:郭永恒
*完成日期:2016年4月22日
*版 本 号:v1.0
*
*问题描述:在(2)的基础上,添加更多角色,让任意的两个对象间都有可能互相攻击
*/


//game.h
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

#include <vector>
#include <string>
const int N=5; //每个角色最多拥有的武器
const int M=30; //游戏人物的最大数
using namespace std;
const int NOWEAPON=-1; //表示手中无武器

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() = default;
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() = default;
Role(string nam, int b, Point l, vector<Weapon> w); //构造函数
~Role(); //析构函数
void eat(int d); //吃东西,涨d血(死了后吃上东西可以复活)
void attack(Role &r); //攻击别人,自己涨血,同时对方被攻击失血。血量取决于当前用的武器
void beAttack(Role &r); //被别人攻击,r是攻击者
double distance(Role &r); //返回与另一角色的距离
bool isAlived(); //是否活着
void moveTo(int x, int y); //移到另外一点
void move(int dx, int dy); //从当前位置移动
void changeWeapon(int wno); //换手中的武器
void vAddWeapon();//增加一件武器
void vAddWeapon(Weapon);
void vDelWeapon();//删除一件武器
void show(); //显示
void vShowWeapons();//显示武器库
void setBaseInfo(string, int);//角色名称和初始血量
void setLocation(int,int);//设置位置
int getWeaponNum();
string getName();
string getCurWeapon();
private:
string name; //角色名称
int blood; //当前血量
bool life; //是否活着
Point location; //位置
vector<Weapon> weapons; //武器库
int holdWeapon; //现在手持哪一件武器(空手为NOWEAPON,初始时空手)
};

#endif // GAME_H_INCLUDED

//point.cpp
#include "game.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));
}

//weapon.cpp
#include "game.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;
}

//role.cpp
#include <iostream>
#include "game.h"
using namespace std;

Role::Role(string nam, int b, Point l, vector<Weapon> w)
:name(nam),blood(b),location(l),weapons(w),holdWeapon(NOWEAPON)
{
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)
{
if(isAlived()&&holdWeapon>NOWEAPON&&weapons[holdWeapon].getKillRange()>this->distance(r)) //活着且在杀伤范围内
{
blood += weapons[holdWeapon].getForce();
r.blood -= weapons[holdWeapon].getForce();
}
}

//被别人攻击,r是攻击者
void Role::beAttack(Role &r)
{
if(r.isAlived() && r.holdWeapon>NOWEAPON && r.weapons[holdWeapon].getKillRange()>this->distance(r))
{
blood -= r.weapons[holdWeapon].getForce();
r.blood += r.weapons[holdWeapon].getForce();
if(blood<=0)
life=false;
}
}

//返回与另一角色的距离
double Role::distance(Role &r)
{
return location.distance(r.location);
}
//换手中的武器
void Role::changeWeapon(int wno)
{
if(wno<(int)weapons.size())
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::vAddWeapon()
{
string name;
int force;
double range;
cout << "输入要添加武器的名字:";
cin >> name;
cout << "威力:";
cin >> force;
cout << "杀伤范围:";
cin >> range;
weapons.push_back(Weapon(name,force,range));
}

void Role::vAddWeapon(Weapon w)
{
if(weapons.size() <= N)
weapons.push_back(w);
}
//删除武器
void Role::vDelWeapon()
{
string wnam;
cout <<"输入要删除武器的名称:";
cin.clear();
cin.sync();
getline(cin,wnam);
for(auto beg = weapons.begin(); beg != weapons.end(); ++beg)
{
if(beg->getWname() == wnam)
{
weapons.erase(beg);
return ;
}
}
cout << "找不到武器" << endl;
}

//显示
void Role::show()
{
cout<<name<<" has "<<blood<<" blood, hold ";
if(holdWeapon==NOWEAPON)
cout<<"no weapon";
else
cout<<weapons[holdWeapon].getWname();

cout<<"(";
for(int i = 0; i < (int)weapons.size(); i++)
cout<<weapons[i].getWname()<<",";
cout<<"\b)";

cout<<". He is in ("<<location.getX()<<", "<<location.getY()<<") and ";
if(isAlived())
cout<<"alived.";
else
cout<<"dead.";
cout<<endl;
}
//显示武器库信息
void Role::vShowWeapons()
{
cout << name << "'s arsenal:" << endl;
for(auto &temp : weapons)
cout << "name:" << temp.getWname() << "  force:" << temp.getForce() <<"  kill range:" << temp.getKillRange() << endl;
}
//角色名称和初始血量
void Role::setBaseInfo(string nam, int b)
{
name=nam;
blood=b;
if(blood>0)
life=true;
}
//设置位置
void Role::setLocation(int x,int y)
{
location.moveTo(x,y);
}
//返回角色名称
string Role::getName()
{
return name;
}
//返回角色当前武器名称
string Role::getCurWeapon()
{
return weapons[holdWeapon].getWname();
}
//返回角色武器个数
int Role::getWeaponNum()
{
return (int)weapons.size();
}
//测试函数
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include "game.h"
using namespace std;

void IniRoles(vector<Role> &roles);//初始化角色,由计算机随机产生
int ReadWeaInfo(vector<Weapon> &WeaponBase);//从文件中读取武器信息
int randBetween(int low, int high); //产生一定范围内的随机数
void play(vector<Role> &roles, int n);

//通过随机数来生成人物名字
string NameLib[16] = {"AD","ED","RF","RT","TG","TY","YH","BN","BH","YH","YQ","WA","YU","KO","LM","VF"};

int main()
{
srand(time(0));
vector<Role> roles;
IniRoles(roles);
play(roles, 1000);
return 0;
}

//初始化角色
void IniRoles(vector<Role> &roles)
{
vector<Weapon> WeaponBase;
Role Tp;//temp people
int WeaponNum = ReadWeaInfo(WeaponBase);
int wn; //要加的武器数
int wno; //要加入的武器的编号(weaponBase中的下标)
for(int i = 0; i < M; ++i)//添加角色
{
Tp.setBaseInfo(NameLib[rand()%16]+NameLib[rand()%16],randBetween(10, 100));
Tp.setLocation(randBetween(0,1000),randBetween(0, 1000));
wn = randBetween(1,N);
for(int j = 0; j < wn; ++j)
{
wno = randBetween(0,WeaponNum);
Tp.vAddWeapon(WeaponBase[wno]);
}
Tp.changeWeapon(randBetween(0,wn));
roles.push_back(Tp);
}
}

//初始化武器库
int ReadWeaInfo(vector<Weapon> &WeaponBase)
{
ifstream fin("weapon.txt");
string wn;
int wf;
double wr;
if(!fin)
{
cerr << "open error!" << endl;
exit(1);
}
while(fin >> wn >> wf >> wr)
WeaponBase.push_back(Weapon(wn,wf,wr));//此处老师调用的函数,而我直接构建的新对象
fin.close();
return WeaponBase.size();
}

//产生一定大于等于low,小于high范围内的随机数
int randBetween(int low, int high)
{
return low+rand()%(high-low);
}

void play(vector<Role> &roles, int n)
{
int i;
int rno,rno2;
int action;//行动0-攻击,1-移动,2-换武器,3-吃东西
int newx,newy,newWeapon,eatd;
ofstream fout("log.txt");
if(!fout)
{
cout << "open error!" << endl;
exit(1);
}

cout << "开始前......" << endl;
for(auto &temp : roles)
temp.show();
cout << "开始游戏,请到日志文件中看过程...." << endl << endl;
for(i = 0; i < n; ++i)
{
rno = randBetween(0,M);
fout << "第" << i << "轮: " << roles[rno].getName();
action = randBetween(0,4);
switch(action)
{
case 0: //攻击
rno2 = randBetween(0,M);
fout << "攻击" << roles[rno2].getName();
roles[rno].attack(roles[rno2]);
break;
case 1: //移动
newx = randBetween(0,1000);
newy = randBetween(0, 1000);
fout << "移动到(" << newx << "," << newy << ")";
roles[rno].moveTo(newx,newy);
break;
case 2: //换武器
newWeapon = randBetween(0,roles[rno].getWeaponNum());
roles[rno].changeWeapon(newWeapon);
fout << "将武器换为: " << roles[rno].getCurWeapon();
break;
case 3: //吃
eatd = randBetween(0,100);
roles[rno].eat(eatd);
fout << "吃了: " << eatd;
break;
}
fout << "." << endl;
}
fout << endl;
fout.close();
cout << "游戏结束后...." << endl;
for(auto &temp : roles)
temp.show();
}

运行结果:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: