测试还有很多bug,时间关系,先提交了,以后再改。 加了位置类,并且可以佩戴多个武器了。至于多人对决有思路,但是过于麻烦,而且感觉没有什么意义。
#include<iostream>
#include<string>
#include<cstdlib>
#include<cmath>
using namespace std;
骰子,用于随机触发武器能力
int rollDice()
{
int sum=1+rand()%6;
return sum;
}
class Point //Point类声明
{
public: //外部接口
Point(int xxx=0, int yyy=0):x(xxx),y(yyy){}
int getXX()
{
return x;
}
int getYY()
{
return y;
}
double distance(const Point &p)
{
return int(sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)));
}
void moveTo()
{
x++;
y++;
}
private:
int x, y; //座标
};
Point guaishouweizhi(6,6);
//武器类
class Wuqi
{
private:
string mingzi;
int gongjili;
int huilan;
int gongjijuli;
public:
Wuqi(string ming="0",int gongji=0,int huil=0,int gongjiju=0):mingzi(ming),gongjili(gongji),huilan(huil),gongjijuli(gongjiju){} //构造函数
void setwuqi() //设定武器属性
{
cin>>mingzi>>gongjili>>huilan>>gongjijuli;
}
void get_mingzi() //输出
{
cout<<mingzi;
}
void get_gongjili() //输出
{
cout<<gongjili;
}
void get_huilan() //输出
{
cout<<huilan;
}
void get_gongjijuli() //输出
{
cout<<gongjijuli;
}
void get_quanbu()
{
cout<<"你当前的武器状态:"<<endl;
cout<<mingzi<<" "<<gongjili<<" "<<huilan<<" "<<gongjijuli<<endl;
}
int get__gongjili() //得到
{
return gongjili;
}
int get__huilan() //得到
{
return huilan;
}
int getgongjijuli()
{
return gongjijuli;
}
};
人物类
class Role
{
private:
Wuqi wuqi[10]; ///最多配备10个武器
string name;
int blood;
int magic;
int jibawuqi;
Wuqi dangqianwuqi;
Point weizhi;
static int count; //静态 显示回合数
public:
bool life;
void weizhibiandong()
{
weizhi.moveTo();
}
void changeWeapon(int wno)//换手中的武器
{
dangqianwuqi=wuqi[wno-1];
}
Role(string na=" ",int bl=0,int ma=0):name(na),blood(bl),magic(ma){}
bool isAlived()
{
return life;
}
void setRole(string na,int bl,int ma)
{
cout<<"您成功建立的角色"<<endl;
name=na;
blood=bl;
magic=ma;
life=true;
}
/设定武器属性并展示
void sheding()
{
int i=0;
cout<<"请输入你要佩戴几把武器(最多10把)"<<endl;
cin>>jibawuqi;
for(i=0;i<jibawuqi;i++)
{
cout<<"请输入你所持有的武器,攻击力和特殊能力(回蓝)以及攻击距离 特殊能力在攻击时有几率触发"<<endl;
wuqi[i].setwuqi();
cout<<"您已经装备武器:"<<endl;
cout<<"武器名字:";
wuqi[i].get_mingzi();
cout<<endl;
cout<<"武器攻击力:";
wuqi[i].get_gongjili();
cout<<endl;
cout<<"武器回蓝:";
wuqi[i].get_huilan();
cout<<"武器攻击距离:";
wuqi[i].get_gongjijuli();
cout<<endl;
}
}
///展示任务状态
void show()
{
if(life)
cout<<name<<" have "<<blood<<" blood"<<magic<<" magic"<<endl;
else
{
cout<<"你已经死了,不能有任何动作"<<endl;
}
}
///攻击
void attack()
{
int uy;
cout<<"请选择武器"<<endl;
cin>>uy;
changeWeapon(uy);
dangqianwuqi.get_quanbu();
count=count+1;
if(weizhi.distance(guaishouweizhi)>=dangqianwuqi.getgongjijuli())
{
cout<<"你成功攻击了对方,生命值+";
dangqianwuqi.get_gongjili();
int sum=rollDice();
if(sum<4)
{
cout<<" 武器特殊能力触发! 魔法值+";
dangqianwuqi.get_huilan();
magic=magic+dangqianwuqi.get__huilan();
}
cout<<endl;
blood=blood+dangqianwuqi.get__gongjili();
}
else
{
cout<<"攻击距离不够啊,攻击失败 ~~~~(>_<)~~~~ "<<endl;
}
}
魔法攻击
void magicattact()
{
int uy;
cout<<"请选择武器"<<endl;
cin>>uy;
changeWeapon(uy);
dangqianwuqi.get_quanbu();
count=count+1;
if(weizhi.distance(guaishouweizhi)>=dangqianwuqi.getgongjijuli())
{
if(magic>0)
{
cout<<"你使用了魔法攻击,对方生命值-";
dangqianwuqi.get_gongjili();
cout<<"您的生命值+";
dangqianwuqi.get_gongjili();
int sum=rollDice();
if(sum<4)
{
cout<<" 武器特殊能力触发! 魔法值+";
dangqianwuqi.get_huilan();
magic=magic+dangqianwuqi.get__huilan();
}
cout<<endl;
blood=blood+dangqianwuqi.get__gongjili();
magic=magic-1;
}
else
{
cout<<"魔法释放失败了,魔力不足!!!!"<<endl;
}
}
else
{
cout<<"攻击距离不够啊,攻击失败 ~~~~(>_<)~~~~ "<<endl;
}
}
/吃面包
void eat(int n)
{
count=count+1;
cout<<"你吃了"<<n<<"个面包,生命值回复"<<n<<"点"<<endl;
blood=blood+n;
}
~Role()
{
cout<<"游戏退出"<<endl;
}
/牺牲
void xisheng()
{
cout<<name<<"光荣牺牲了"<<endl;
}
void beAttack()
{
cout<<"你被攻击了,生命值-1"<<endl;
blood--;
if(blood<=0)
{
life=false;
xisheng();
}
}
static int showCount()
{
return count;
}
};
int Role::count=0;
int main()
{
int seed;
cout<<"请任意输入一个数字,开启游戏"<<endl;
cin>>seed;
srand(seed);
Role mary;
cout<<"输入人物名称 血量和魔力值"<<endl;
string nam;
int xue,magi;
cin>>nam>>xue>>magi;
mary.setRole(nam,xue,magi);
mary.sheding();
mary.show();
cout<<"1为吃面包"<<endl;
cout<<"2为物理攻击"<<endl;
cout<<"3为魔法攻击"<<endl;
cout<<"4为向怪兽靠近"<<endl;
cout<<"5为退出"<<endl;
while(mary.life)
{
int i;
int q=0;
cin>>i;
switch(i)
{
case 1: cout<<"请输入面包能量:"<<endl;cin>>q;mary.eat(q);mary.beAttack();break;
case 2: mary.attack();mary.beAttack();break;
case 3:mary.magicattact();mary.beAttack();break;
case 4:mary.weizhibiandong();break;
case 5:q=1;break;
default: cout<<"输入错误"<<endl;
}
mary.show();
if(q==1)
break;
}
cout<<"你一共经历了"<<Role::showCount()<<"回合"<<endl;
}