菜鸟在编码(文本战斗系统)方面存在问题

Noob has problems in coding (text combat system)

本文关键字:系统 方面 存在 问题 编码 文本 菜鸟      更新时间:2023-10-16

我最近开始尝试用C++编程。我正在为我的文字冒险创建一个战斗系统,但我遇到了一些问题。例如,我希望战斗在你或对手死亡时结束。目前,只有当两个人都死了,它才会结束。另一个是,如果你的防御大于攻击,你可以通过攻击来治愈。你能在这里帮助菜鸟吗?:D这是代码:

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iso646.h>
int main()
{
int att, def, hp, eatt, edef, ehp, pinput, einput;
cout<<"Your damgae"<<endl;
cin>>att;
cout<<"Your defence"<<endl;
cin>>def;
cout<<"Your hp"<<endl;
cin>>hp;
cout<<"Enemy attack"<<endl;
cin>>eatt;
cout<<"Enemy defence"<<endl;
cin>>edef;
cout<<"Enemy hp"<<endl;
cin>>ehp;
do {
    srand (time(NULL));
    einput=rand()%2+1;
    cout<<"1) attack 2) defend"<<endl;
    cin>>pinput;
    if (pinput==1) {
                   if (einput==1) {
                                  cout<<"enemy defends"<<endl;
                                  att=att-edef;
                                  ehp=ehp-att;
                                  cout<<"Enemy hp now is:"<<ehp<<endl;
                                  att=att+edef;
                                  }
                   if (einput==2) {
                                  cout<<"enemy doesn't defend but attacks you too"<<endl;
                                  hp=hp-eatt;
                                  ehp=ehp-att;
                                  cout<<"Your hp is:"<<hp<<endl;
                                  cout<<"Enemy hp now is:"<<ehp<<endl;
                                  }
                   }
    if (pinput==2) {
                   if (einput==1) cout<<"enemy stands in defence aswell"<<endl;
                   if (einput==2) {
                                  cout<<"enemy attacks you"<<endl;
                                  eatt=eatt-def;
                                  hp=hp-eatt;
                                  eatt=eatt+def;
                                  cout<<"Your hp is:"<<hp<<endl;
                                  }
                                  }

                   }
                   while (ehp>0||hp>0);
                   if (ehp<1) cout<<"You win"<<endl;
                   if (hp<1) cout<<"You die"<<endl;

 system("PAUSE");
return 0;
}

提前感谢所有的帮助,并为我犯的任何愚蠢错误感到抱歉。

第一个问题很容易解决,使用&&||,你想在你的生命值>0,敌人命中点>0的情况下继续前进。

while (ehp>0 && hp>0);