一个RPG游戏,逻辑错误很多,希望高手帮忙找找

Akmvp 2008-11-07 11:58:49
写了几天的RPG代码
本人很菜
测试后发现打怪那里死亡后再次打怪才那么重复获得经验金钱,是哪里错了,我找不到啊

#include <iostream>
#include <cstring>
#include <ctime>
using namespace std;
string str1="大生命药水",str2="生命药水",str3="大魔法药水",str4="魔法药水";
class Bag
{
public:
Bag();
void set_bag(string str[]);
void add_bag(string str);
bool used_bag(string str);
void display_bag();
private:
string article[20];
};

Bag::Bag()
{
for(int i=0;i<20;i++)
article[i]="0";
}

void Bag::set_bag(string str[])
{
for(int i=0;i<sizeof(str)/sizeof(string);i++)
article[i]=str[i];
}

void Bag::add_bag(string str)
{
for(int i=0;i<20;i++)
{
if(article[i]=="0")
{
article[i]=str;
break;
}
}
}

bool Bag::used_bag(string str)
{
for(int i=0;i<20;i++)
{
if(article[i]==str)
{
str="0";
return 1;
}
else
return 0;
}
}

void Bag::display_bag()
{
int count=0;
for(int i=0;i<20;i++)
{
if(article[i]!="0")
{
cout<<article[i]<<" ";
count++;
if(count%3==0)
cout<<endl;
}
}
}


class Role
{
public:
Role(Bag ba,string na,string ra,string mag[10],double le,double h,double ma,double at,double def,double ex,double mon);
void fight(Role *other);
void hurt(Role *other);
void display_property();
void display_magic();
bool attack_on(Role *other);
void level_up();
Bag bag;
string name,racial,magic[10];
double level,hp,mana,attack,defense,exper,money;
};

Role::Role(Bag ba,string na,string ra,string mag[10],double le,double h,double ma,double at,double def,double ex,double mon)
{
bag=ba;
level=le;
name=na;
racial=ra;
for(int i=0;i<sizeof(mag)/sizeof(string);i++)
magic[i]=mag[i];
hp=h;
mana=ma;
attack=at;
defense=def;
exper=ex;
money=mon;
}

void Role::fight(Role *other)
{
int a,b,c,d,m1,m2;
static int times=0;
do
{
cout<<"输入编号: 1.普通攻击 2.使用魔法 3.使用必杀技 4.使用药水 5.逃跑 6.离开"<<endl;
cin>>a;
switch(a)
{
case 1:
if(other->hp>0)
{
hurt(other);
times++;
}
else
return;
break;
case 2:
cout<<"输入要使用魔法的编号:";
display_magic();
cout<<"6.返回"<<endl;
cin>>b;
if(b==1||b==2||b==3||b==4||b==5)
times++;
switch(b)
{
case 1:
if(mana>=100)
{
attack*=1.2;
defense*=1.2;
cout<<"攻击力与防御力提升20%"<<endl;
hurt(other);
attack*=5.0/6.0;
defense*=5.0/6.0;
mana-=100;
}
else
cout<<"mana不够100,无法使用技能"<<endl;
break;
case 2:
if(mana>=200)
{
other->hp*=0.7;
cout<<"消耗对手30%生命"<<endl;
mana-=200;
}
else
cout<<"mana不够200,无法使用技能"<<endl;
break;
case 3:
if(mana>=100)
{
attack+=attack/2;
cout<<"攻击提高50%"<<endl;
hurt(other);
attack*=2.0/3.0;
mana-=100;
}
else
cout<<"mana不够100,无法使用技能"<<endl;
break;
case 4:
if(mana>=160)
{
defense+=10000;
cout<<"无敌状态"<<endl;
hurt(other);
defense-=10000;
mana-=160;
}
else
cout<<"mana不够160,无法使用技能"<<endl;
break;
case 5:
if(mana>=80)
{
m1=rand()%2;
if(m1)
defense+=10000;
cout<<"产生1个幻象,迷惑对手,对手有50%概率打中幻象"<<endl;
hurt(other);
if(m1)
defense-=10000;
}
else
cout<<"mana不够80,无法使用技能"<<endl;
break;
case 6:
break;
}
break;
case 3:
if(times>=8)
{
cout<<"使用必杀技<剑刃风暴>"<<endl;
other->hp*=0.4;
times-=7;
}
else
cout<<"怒气到达一定值时才能使用"<<endl;
break;
case 4:
cout<<"输入编号:1.大生命药水(hp+1000) 2.生命药水(hp+500)"<<endl
<<"3.大魔法药水(mana+400) 4.魔法药水(mana+200)"<<endl;
cin>>d;
if(d==1)
{
if(bag.used_bag(str1))
{
cout<<"hp增加1000"<<endl;
hurt(other);
}
else
cout<<"没有此药水"<<endl;
}
if(d==2)
{
if(bag.used_bag(str2))
{
cout<<"hp增加500"<<endl;
hurt(other);
}
else
cout<<"没有此药水"<<endl;
}
if(d==3)
{
if(bag.used_bag(str3))
{
cout<<"mana增加400"<<endl;
hurt(other);
}
else
cout<<"没有此药水"<<endl;
}
if(d==4)
{
if(bag.used_bag(str4))
{
cout<<"mana增加200"<<endl;
hurt(other);
}
else
cout<<"没有此药水"<<endl;
}
break;
case 5:
m2=rand()%3;
if(m2)
{
cout<<"逃跑成功"<<endl;
return;
}
else
{
cout<<"逃跑失败"<<endl;
hurt(other);
}
break;
case 6:
if(other->hp>0)
cout<<"战斗还没有结束,不能离开,但你可以选择逃跑"<<endl;
else
{
cout<<"已经离开战斗"<<endl;
return;
}
break;
}
}while (a!=7);
}

void Role::hurt(Role *other)
{
cout<<name<<"hp="<<hp<<endl;
cout<<other->name<<"hp="<<other->hp<<endl;
hp-=((rand()%5+5)/100.0+1)*(other->attack-defense);
if(hp<=0)
{
cout<<name<<"已经死亡,游戏结束"<<endl;
system("pause");
exit(EXIT_SUCCESS);
}
if(attack_on(other))
{
cout<<other->name<<"已经死亡,战斗胜利"<<endl;
cout<<"获得"<<other->money<<"金钱"<<endl;
cout<<"获得"<<other->exper<<"经验"<<endl;
money+=other->money;
exper+=other->exper;
level_up();
}
}

void Role::display_property()
{
cout<<"等级:"<<level<<" 名字:"<<name<<" 种族:"<<racial<<" 生命:"<<hp<<" 魔法:"<<mana<<endl
<<"攻击:"<<attack<<" 防御:"<<defense<<" 经验:"<<exper<<" 金钱:"<<money<<endl;
}

void Role::display_magic()
{
for(int i=0;i<10;i++)
if(magic[i]!="0")
cout<<magic[i];
}

bool Role::attack_on(Role *other)
{
other->hp-=((rand()%10+5)/100.0+1)*(attack-other->defense);
//cout<<name<<"hp="<<hp<<endl;
if(other->hp<=0)
return 1;
else
return 0;
}

void Role::level_up()
{
static double max_exper=500;
if(exper>=max_exper)
{
level++;
hp=hp*1.1+300;
mana*=1.15;
attack*=1.05;
defense*=1.1;
exper-=max_exper;
max_exper*=1.5;
cout<<"人物升级"<<endl<<"现在的属性为"<<endl;
display_property();
}
}

...全文
290 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
pusshi 2008-11-08
  • 打赏
  • 举报
回复
建义.
1.把怪物的数据建立个文件,如:分3个文件,文件名相当于地图名,每个文件里放N个怪物数据,
由角色选译进入地图也就是文件,可以随机,也可以顺序地提取怪物数据和角色战斗.

2.把角色技能提取出来做为一个类,

3.增加装备武器类和可以物怪帮定一起,打死怪物随机掉武器或装备.

星羽 2008-11-07
  • 打赏
  • 举报
回复
逻辑错误。。。。。。- -

up一下吧
dfkjsdhfks 2008-11-07
  • 打赏
  • 举报
回复
太牛了,看着就觉得晕.
镜机 2008-11-07
  • 打赏
  • 举报
回复
帮顶,up!
Akmvp 2008-11-07
  • 打赏
  • 举报
回复
还有这里,贴不了这么多

int main()
{
srand(time(NULL));
string hero_magic[10]={"1.天神下凡(100 mana)","2.死亡凋零(200 mana)","3.致命一击(100 mana)","4.无敌(160 mana)","5.分身(80 mana)","0","0","0","0","0"},
monster_magic[10]={"0"},
str1="大生命药水",str2="生命药水",str3="大魔法药水",str4="魔法药水";
Bag b1,b2;
Role hero(b1,"尤迪安","暗夜精灵族",hero_magic,1,2000,500,380,200,0,5000);
int a,b,c,d;
do
{
cout<<"输入编号:1.打怪练级 2.到商店买药 3.查看属性 4.使用药水 5.离开游戏"<<endl;
cin>>a;
if(a==1)
{
b=rand()%3;
if(b==0)
{
Role *monster=new Role(b2,"穴居恶魔","不死族",monster_magic,1,1200,200,500,120,300,1000);
cout<<"找到怪物"<<endl;
monster->display_property();
hero.fight(monster);
hero.hurt(monster);
delete monster;
}
if(b==1)
{
Role *monster=new Role(b2,"破法者","人族",monster_magic,1,1500,500,350,180,250,800);
cout<<"找到怪物"<<endl;
monster->display_property();
hero.fight(monster);
hero.hurt(monster);
delete monster;
}
if(b==2)
{
Role *monster=new Role(b2,"牛头人","兽族",monster_magic,1,2500,150,400,220,450,1300);
cout<<"找到怪物"<<endl;
monster->display_property();
hero.fight(monster);
hero.hurt(monster);
delete monster;
}
}
if(a==2)
{
do
{
cout<<"输入编号:1.大生命药水(hp+1000) $1500 2.生命药水(hp+500) $700"<<endl
<<"3.大魔法药水(mana+400) $1000 4.魔法药水(mana+200) $400 5.离开"<<endl;
cin>>c;
if(c==1)
{
if(hero.money>=1500)
{
cout<<"花费1500购买了大生命药水"<<endl;
hero.bag.add_bag(str1);
hero.money-=1500;
}
else
cout<<"金钱不足,无法购买"<<endl;
}
if(c==2)
{
if(hero.money>=700)
{
cout<<"花费700购买了生命药水"<<endl;
hero.bag.add_bag(str2);
hero.money-=700;
}
else
cout<<"金钱不足,无法购买"<<endl;
}
if(c==3)
{
if(hero.money>=1000)
{
cout<<"花费1000购买了大魔法药水"<<endl;
hero.bag.add_bag(str3);
hero.money-=1000;
}
else
cout<<"金钱不足,无法购买"<<endl;
}
if(c==4)
{
if(hero.money>=1500)
{
cout<<"花费400购买了魔法药水"<<endl;
hero.bag.add_bag(str4);
hero.money-=400;
}
else
cout<<"金钱不足,无法购买"<<endl;
}
}while(c!=5);
}
if(a==3)
hero.display_property();
if(a==4)
{
cout<<"输入编号:1.大生命药水(hp+1000) 2.生命药水(hp+500)"<<endl
<<"3.大魔法药水(mana+400) 4.魔法药水(mana+200)"<<endl;
cin>>d;
if(d==1)
{
if(hero.bag.used_bag(str1))
cout<<"hp增加1000"<<endl;
else
cout<<"没有此药水"<<endl;
}
if(d==2)
{
if(hero.bag.used_bag(str2))
cout<<"hp增加500"<<endl;
cout<<"没有此药水"<<endl;
}
if(d==3)
{
if(hero.bag.used_bag(str3))
cout<<"mana增加400"<<endl;
cout<<"没有此药水"<<endl;
}
if(d==4)
{
if(hero.bag.used_bag(str4))
cout<<"mana增加200"<<endl;
cout<<"没有此药水"<<endl;
}
}
}while(a!=5);
system("pause");
return 0;
}

Akmvp 2008-11-07
  • 打赏
  • 举报
回复
哈哈,改得不错,我也学习了不少,我调试了下
下面是你的2个小错误
我打算加多点东西,下次要到1000行的
希望你来改

if(a == 4)
{
cout<<"输入编号:1.大生命药水(hp+1000) 2.生命药水(hp+500)"<<endl
<<"3.大魔法药水(mana+400) 4.魔法药水(mana+200)"<<endl;
cin>>d;
if(d == 1)
{
if(hero.bag.used_bag(str1))
{
hero.hp = ( (hero.hpsx-hero.hp)>1000 ? (hero.hp+1000) : hero.hpsx );
cout<<"hp增加1000"<<endl;
}
else
cout<<"没有此药水"<<endl;
}
if(d==2)
{
if(hero.bag.used_bag(str2))
{
hero.hp = ( (hero.hpsx-hero.hp)>500 ? (hero.hp+500) : hero.hpsx );
cout<<"hp增加500"<<endl;
}
else//3个else漏了
cout<<"没有此药水"<<endl;
}
if(d==3)
{
if(hero.bag.used_bag(str3))
{
hero.mana = ( (hero.manasx-hero.mana)>400 ? (hero.mana+400) : hero.manasx );
cout<<"mana增加400"<<endl;
}
else
cout<<"没有此药水"<<endl;
}
if(d==4)
{
if(hero.bag.used_bag(str4))
{
hero.mana = ( (hero.manasx-hero.mana)>200 ? (hero.mana+200) : hero.manasx );
cout<<"mana增加200"<<endl;
}
else
cout<<"没有此药水"<<endl;
}



case 5:
if(mana >= 80)
{
m1= rand()%2;
if(m1)
{ //漏了括号
defense += 10000;

cout<<"产生1个幻象,迷惑对手,对手有50%概率打中幻象"<<endl;

hurt(other);
mana-=80;
defense-=10000;//这里不明白你上面的意思
}
}
else
cout<<"mana不够80,无法使用技能"<<endl;
break;


冰山之海 2008-11-07
  • 打赏
  • 举报
回复
佩服,用DOS下做游戏,觉得很有创意,加油`~

我没时间看,你用打断点的方式进到里面看看,应该很容易发现在那里出了逻辑错误~~


加油
pusshi 2008-11-07
  • 打赏
  • 举报
回复
我也想写游戏,但没学图形接口,等以后学了再写.....

看了你的游戏, 蛮牛了,所以帮你调试了一下,花我几小时呀,俺功力浅,没办法.

调了你的游戏,学了不少东西,呵呵,有空多写几个,哈哈.

以上我代码我调试过了,能运行了,三贴都要复制哟.
leank 2008-11-07
  • 打赏
  • 举报
回复
能运行了么?等你们搞好了我copy下来玩玩。
pusshi 2008-11-07
  • 打赏
  • 举报
回复
void Role::hurt(Role *other)
{
cout<<name<<"hp="<<hp<<endl;
cout<<other->name<<"hp="<<other->hp<<"\n开始战斗"<<endl;
double s=((rand()%5+5)/100.0+1)*( (other->attack-defense)>0 ?
(other->attack-defense) : ((other->attack/(defense/10*2))));
hp -=s;
cout<<"\n"<<name<<"血减少:"<<s<<"\n";

if(hp <= 0)
{
cout<<name<<"已经死亡,游戏结束"<<endl;
system("pause");
exit(EXIT_SUCCESS);
}

if(attack_on(other))
{
cout<<other->name<<"已经死亡,战斗胜利"<<endl;
cout<<"获得"<<other->money<<"金钱"<<endl;
cout<<"获得"<<other->exper<<"经验"<<endl;
money += other->money;
exper += other->exper;
level_up();

}

}

void Role::display_property()
{
cout<<"等级:"<<level<<" 名字:"<<name<<" 种族:"<<racial<<" 生命:"<<hp<<" 魔法:"<<mana<<endl
<<"攻击:"<<attack<<" 防御:"<<defense<<" 经验:"<<exper<<" 金钱:"<<money<<endl;
}

void Role::display_magic()
{
for(int i=0;i<10;i++)
if(magic[i]!="0")
cout<<magic[i];
}

bool Role::attack_on(Role *other)
{
double qu=((rand()%10+5)/100.0+1)*(attack-other->defense);
other->hp -= qu;
//cout<<name<<"hp="<<hp<<endl;
cout<<other->name<<"血减少:"<<qu<<"\n";
if(other->hp <= 0)
return 1;
else
return 0;
}

void Role::level_up()
{
static double max_exper=500;
if(exper >= max_exper)
{
hpsx += 1000;
manasx += 800;
level++;
hp= hpsx;
mana = manasx;
attack *= 1.05;
defense *= 1.1;
exper -= max_exper;
max_exper *= 1.5;
cout<<"人物升级"<<endl<<"现在的属性为"<<endl;
display_property();

}
}
int main()
{
srand(time(NULL));
string hero_magic[10]={"1.天神下凡(100 mana)","2.死亡凋零(200 mana)","3.致命一击(100 mana)","4.无敌(160 mana)","5.分身(80 mana)","0","0","0","0","0"},
monster_magic[10]={"0"},
str1="大生命药水",str2="生命药水",str3="大魔法药水",str4="魔法药水";
Bag b1,b2;
Role hero(b1,"尤迪安","暗夜精灵族",hero_magic,1,2000,500,380,200,0,5000);
int a,b,c,d;
do
{
cout<<"输入编号:1.打怪练级 2.到商店买药 3.查看属性 4.使用药水 5.离开游戏"<<endl;
cin>>a;
if(a==1)
{
b=rand()%3;
if(b==0)
{
Role *monster=new Role(b2,"穴居恶魔","不死族",monster_magic,1,1200,200,500,120,300,1000);
cout<<"找到怪物"<<endl;
monster->display_property();
hero.fight(monster);
// hero.hurt(monster);
delete monster;
}
if(b==1)
{
Role *monster=new Role(b2,"破法者","人族",monster_magic,1,1500,500,350,180,250,800);
cout<<"找到怪物"<<endl;
monster->display_property();
hero.fight(monster);
// hero.hurt(monster);
delete monster;
}
if(b==2)
{
Role *monster=new Role(b2,"牛头人","兽族",monster_magic,1,2500,150,400,220,450,1300);
cout<<"找到怪物"<<endl;
monster->display_property();
hero.fight(monster);
//hero.hurt(monster);
delete monster;
}
}
if(a==2)
{
do
{
cout<<"输入编号:1.大生命药水(hp+1000) $1500 2.生命药水(hp+500) $700"<<endl
<<"3.大魔法药水(mana+400) $1000 4.魔法药水(mana+200) $400 5.离开"<<endl;
cin>>c;
if(c == 1)
{
if(hero.money >= 1500)
{
cout<<"花费1500购买了大生命药水"<<endl;
hero.bag.add_bag(str1);
hero.money -= 1500;
}
else
cout<<"金钱不足,无法购买"<<endl;
}
if(c == 2)
{
if(hero.money >= 700)
{
cout<<"花费700购买了生命药水"<<endl;
hero.bag.add_bag(str2);
hero.money -= 700;
}
else
cout<<"金钱不足,无法购买"<<endl;
}
if(c == 3)
{
if(hero.money >= 1000)
{
cout<<"花费1000购买了大魔法药水"<<endl;
hero.bag.add_bag(str3);
hero.money -= 1000;
}
else
cout<<"金钱不足,无法购买"<<endl;
}
if(c == 4)
{
if(hero.money >= 1500)
{
cout<<"花费400购买了魔法药水"<<endl;
hero.bag.add_bag(str4);
hero.money -= 400;
}
else
cout<<"金钱不足,无法购买"<<endl;
}
}while(c != 5);
}
if(a == 3)
hero.display_property();
if(a == 4)
{
cout<<"输入编号:1.大生命药水(hp+1000) 2.生命药水(hp+500)"<<endl
<<"3.大魔法药水(mana+400) 4.魔法药水(mana+200)"<<endl;
cin>>d;
if(d == 1)
{
if(hero.bag.used_bag(str1))
{
hero.hp = ( (hero.hpsx-hero.hp)>1000 ? (hero.hp+1000) : hero.hpsx );
cout<<"hp增加1000"<<endl;
}
else
cout<<"没有此药水"<<endl;
}
if(d==2)
{
if(hero.bag.used_bag(str2))
{
hero.hp = ( (hero.hpsx-hero.hp)>500 ? (hero.hp+500) : hero.hpsx );
cout<<"hp增加500"<<endl;
}
cout<<"没有此药水"<<endl;
}
if(d==3)
{
if(hero.bag.used_bag(str3))
{
hero.mana = ( (hero.manasx-hero.mana)>400 ? (hero.mana+400) : hero.manasx );
cout<<"mana增加400"<<endl;
}
cout<<"没有此药水"<<endl;
}
if(d==4)
{
if(hero.bag.used_bag(str4))
{
hero.mana = ( (hero.manasx-hero.mana)>200 ? (hero.mana+200) : hero.manasx );
cout<<"mana增加200"<<endl;
}
cout<<"没有此药水"<<endl;
}
}
}while(a!=5);
system("pause");
return 0;
}
pusshi 2008-11-07
  • 打赏
  • 举报
回复
class Role
{
public:
Role(Bag ba, string na, string ra, string mag[10], double le, double h,
double ma, double at, double def, double ex, double mon);
void fight(Role *other);//战斗
void hurt(Role *other);//伤害
void display_property();//财产
void display_magic();//魔法
bool attack_on(Role *other);//攻击;
void level_up();
Bag bag;
string name, racial, magic[10]; //种族, 魔法技能
double level, hp, mana, attack, defense, exper, money;//等级,生命,魔法值,攻击力,防御力,经验,钱
int hpsx;//生命上限,加血时不能超过,相应等级的生命上限;
int manasx;//魔法上限;
};

Role::Role(Bag ba, string na, string ra, string mag[10], double le, double h,
double ma, double at, double def, double ex, double mon)
{
hpsx=2000;
manasx=1500;
bag=ba;
level=le;
name=na;
racial=ra;
for(int i=0;i<10;i++)//(sizeof(mag)/sizeof(string))这地方原来只持行一次,
{
magic[i]=mag[i];
}
hp=h;
mana=ma;
attack=at;
defense=def;
exper=ex;
money=mon;
}

void Role::fight(Role *other)
{
int a,b,c,d,m1,m2;
static int times=0;
do
{
cout<<"输入编号: 1.普通攻击 2.使用魔法 3.使用必杀技 4.使用药水 5.逃跑 6.离开fgfgfgf"<<endl;
cin>>a;
switch(a)
{
case 1:
if(other->hp>0)
{
hurt(other);
times++;
}
if(other->hp<=0)
return;
break;

case 2:
cout<<"输入要使用魔法的编号:";
display_magic();
cout<<"6.返回"<<endl;
cin>>b;
if(b==1||b==2||b==3||b==4||b==5)
times++;
switch(b)
{
case 1:
if(mana>=100)
{
attack *= 1.2;
defense *= 1.2;
cout<<"攻击力与防御力提升20%"<<endl;
hurt(other);
attack /= 1.2;//还原攻击
defense /= 1.2; //还原防御;
mana -= 100;
}
else
cout<<"mana不够100,无法使用技能"<<endl;
break;

case 2:
if(mana>=200)
{
other->hp *= 0.7;
cout<<"消耗对手30%生命"<<endl;
cout<<name<<hp<<"\n"<<other->name<<other->hp<<"\n";
mana -= 200;
}
else
cout<<"mana不够200,无法使用技能"<<endl;
break;
case 3:
if(mana >= 100)
{
attack += attack/2;
cout<<"攻击提高50%"<<endl;
hurt(other);
attack /= 3.0/2.0;
mana -= 100;
}
else
cout<<"mana不够100,无法使用技能"<<endl;
break;
case 4:
if(mana>=160)
{
defense += 10000;
cout<<"无敌状态"<<endl;
hurt(other);
defense -= 10000;
mana -= 160;
}
else
cout<<"mana不够160,无法使用技能"<<endl;
break;
case 5:
if(mana >= 80)
{
m1= rand()%2;
if(m1)
defense += 10000;

cout<<"产生1个幻象,迷惑对手,对手有50%概率打中幻象"<<endl;

hurt(other);
if(!m1)
defense *= 0.5 ;
}
else
cout<<"mana不够80,无法使用技能"<<endl;
break;
case 6:
break;
}
break;
case 3:
if(times >= 8)
{
cout<<"使用必杀技<剑刃风暴>"<<endl;
other->hp *= 0.4;
times -= 7;
}
else
cout<<"怒气到达一定值时才能使用"<<endl;
break;
case 4:
cout<<"输入编号:1.大生命药水(hp+1000) 2.生命药水(hp+500)"<<endl
<<"3.大魔法药水(mana+400) 4.魔法药水(mana+200)"<<endl;
cin>>d;
if(d==1)
{
if(bag.used_bag(str1))
{
hp = ( (hpsx-hp)>1000 ? (hp+1000) : hpsx ); //限制加血.
cout<<"hp增加1000"<<endl;

//hurt(other);//加血时不进行战斗,如果要就去掉.//
}
else
cout<<"没有此药水"<<endl;
}
if(d==2)
{
if(bag.used_bag(str2))
{
hp= ( (hpsx-hp)>500 ? (hp+500) : hpsx );
cout<<"hp增加500"<<endl;
// hurt(other);
}
else
cout<<"没有此药水"<<endl;
}
if(d==3)
{
if(bag.used_bag(str3))
{
mana = ( (manasx-mana)>400 ? (mana+400) : manasx );
cout<<"mana增加400"<<endl;
// hurt(other);
}
else
cout<<"没有此药水"<<endl;
}
if(d==4)
{
if(bag.used_bag(str4))
{
mana = ( (manasx-mana)>200 ? (mana+200) : manasx );
cout<<"mana增加200"<<endl;
// hurt(other);
}
else
cout<<"没有此药水"<<endl;
}
break;
case 5:
m2=rand()%3;
if(m2)
{
cout<<"逃跑成功"<<endl;
return;
}
else
{
cout<<"逃跑失败"<<endl;
hurt(other);
}
break;
case 6:
if(other->hp >0)
cout<<"战斗还没有结束,不能离开,怕死就逃跑吧"<<endl;
else
{
cout<<"已经离开战斗"<<endl;
return;
}
break;
}
}while (a!=7);
}
pusshi 2008-11-07
  • 打赏
  • 举报
回复
我对你的游戏有点兴趣,花了点时间,帮你改的差不多了...
有不对的地方,我都标出来了,我也是新手,呵呵...
#include <iostream>
#include <cstring>
#include <ctime>
#include <string>
using namespace std;
string str1="大生命药水",str2="生命药水",str3="大魔法药水",str4="魔法药水";
class Bag//人物装备类
{
public:
Bag();
void set_bag(string str[]);
void add_bag(string str);
bool used_bag(string str);
void display_bag();
private:
string article[20];//可以放 20个东西;
};

Bag::Bag()
{
for(int i=0;i<20;i++)
article[i]="0";
}

void Bag::set_bag(string str[])
{
for(int i=0;i<sizeof(str)/sizeof(string);i++)
article[i]=str[i];
}

void Bag::add_bag(string str)
{
for(int i=0; i<20; i++)
{
if(article[i] == "0")
{
article[i]=str;
break;
}
}
cout<<"备包已满";

}

bool Bag::used_bag(string str)
{
for(int i=0;i<20;i++)
{
if(article[i] == str)
{
// str="0";这地方可以你打错了.
article[i]="0";
return 1;
}
// else 这地方不应该返回; 因力第一个如果不是,他就返回了,显然后面没检查;
// return 0;
}
return 0;
}

void Bag::display_bag()
{
int count=0;
for(int i=0;i<20;i++)
{
if(article[i]!="0")
{
cout<<article[i]<<" ";
count++;
if(count%3==0)
cout<<endl;
}
}
}


Jarrys 2008-11-07
  • 打赏
  • 举报
回复
up
frank_ll 2008-11-07
  • 打赏
  • 举报
回复
void Bag::set_bag(string str[])
{
for(int i=0;i<sizeof(str)/sizeof(string);i++)
article[i]=str[i];
}

这个地方明显有问题吧
sizeof(str)/sizeof(string)=0
frank_ll 2008-11-07
  • 打赏
  • 举报
回复
mark
到时候在看
cyj626 2008-11-07
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 Longinc 的回复:]
帮顶
[/Quote]
up
pusshi 2008-11-07
  • 打赏
  • 举报
回复
我复制了一下,能运行,不过要加 #include<string>在VC++6.0
我也是新手,看了一下,for(int i=0;i<sizeof(mag)/sizeof(string);i++)
这段不对,sizeof(mag)/sizeof(string);mag在传进来就是一个string类型,不是string数组.
for(int i=0;i<sizeof(mag)/sizeof(string);i++)
这段代码就执行一次,改成10就行,
其它暂时还没看,晚上来研究一下,哈哈

Longinc 2008-11-07
  • 打赏
  • 举报
回复
帮顶
koo8 2008-11-07
  • 打赏
  • 举报
回复
看晕了,为何我拷贝下来编译会报错: if(article[i]=="0")//

error C2676: binary '==' : 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' does not define this operator or a conversion to a type acceptable to the predefined operator
chenchao1119 2008-11-07
  • 打赏
  • 举报
回复
貌似有点WAR3的影子
加载更多回复(8)

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧