Design and write code for automatic fighting games.

Keywords: C++

1) Role class CRole is the base class:

Constructor, destructor;

Member variables: avatar, HP (blood volume), ATK (attack), DEF (defense), Lv (rank), EXP (experience value);

Member function: Weapon attack, jump.

2) Hero CHero inherits from CRole class

Constructor, destructor;

The new skills of heroes, kicking (member function), hugging (member function), cause injury to each other specific values set by the students themselves;

3) Enemy class CEnemy inherits from CRole class

Constructor, destructor;

The new skills splitting (member function), tandem legs (member function) to cause injury to the other party are set by the trainees themselves;

4) Combat type CBattle

Constructors, destructors, and other member functions

The initial HP (blood) value of both sides is 100. After the start of the battle, heroes and enemies attack each other in turn. Blood loss = Attacker's skill damage + ATK (Attacker's Attack) - DEF (Defense's Defense). When one side's blood is 0, the battle ends, the player's experience value increases, and when the experience level reaches a certain level, the player's level increases.

#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;

class CRole//Role Class: Base Class
{
    public:
        CRole();
        CRole(char *name,int ATK,int DEF,int Lv,int EXP);//Constructor
        ~CRole(){};
    public:
        char *name;
        int hp;
        int ATK;
        int DEF;
        int Lv;
        int EXP;
    public:
        void WpAttack();//Weapon Attack
        void jump();//jump
};
CRole::CRole(char *name,int ATK,int DEF,int Lv,int EXP)
{
    this->name = name;
    this->hp = 100;
    this->ATK = ATK;
    this->DEF = DEF;
    this->Lv = Lv;
    this->EXP = EXP;
}

class CHero: public CRole//Heroes
{
    public :
        CHero(){};
        CHero(char *name,int ATK,int DEF,int Lv,int EXP):CRole(name,ATK,DEF,Lv,EXP){};//Constructor
        ~CHero(){};
    public:
        void Kicking();//Kicking skills
        void Wrestling();//Wrestling skills
    public:
        int k_ATK;//Kicking Attack
        int w_ATK;//Attacking power of wrestling skills
};

void CHero::Kicking()//Initialize kicking skills attack when using kicking skills
{
    k_ATK = 30;
}

void CHero::Wrestling()//Initialize wrestling skills attack when using wrestling skills
{
    w_ATK = 20;
}

class CEnemy:public CRole//Enemy class
{
    public :
        CEnemy(){};
        CEnemy(char *name,int ATK,int DEF,int Lv,int EXP):CRole(name,ATK,DEF,Lv,EXP){};//Constructor
        ~CEnemy(){};
    public:
        void zp();//Split palm skills
        void lht();//Serial Kicking Skills
    public:
        int z_ATK;//Split skill attack
        int l_ATK;//String Kick Attack
};

void CEnemy::zp()//Initialize the attack power of the split skill when using the split skill
{
    z_ATK = 20;
}

void CEnemy::lht()//Initialize the attack power of the chain kick skill when using the chain kick skill
{
    l_ATK = 30;
}

class CBattle//Combat Class
{
    public :
        CBattle();//Constructor
        ~CBattle(){};
    public:
        void attacMode(CHero R1,CEnemy R2);//Battle function
    public:
        int i;
        int temp;
};

CBattle::CBattle()
{
    i = 1;
    temp = 0;
}

void CBattle::attacMode(CHero R1,CEnemy R2)
{
    srand((unsigned)time(0));//Generate random seeds
    while(R1.hp>0&&R2.hp>0)//Continue fighting when both hero and enemy HP are greater than 0
    {
        temp = rand()%3;//Generate 0-2 random numbers
        if(i%2!=0)//Heroes attack when i is odd
        {
            if(temp==0)//When the random number generates 0, the hero attacks normally
            {
                int harm = R1.ATK-R2.DEF;
                R2.hp -= harm;
                cout<<R1.name<<"Attack Common,Yes"<<R2.name<<"Causes"<<harm<<"Point injury\n";
            }
            if(temp==1)//Heroes use kicking skills when random numbers generate 1
            {
                R1.Kicking();
                int harm = R1.ATK+R1.k_ATK-R2.DEF;
                R2.hp -= harm;
                cout<<R1.name<<"Use kicking"<<"Attack,Yes"<<R2.name<<"Causes"<<harm<<"Point injury\n";
            }
            if(temp==2)//Heroes use wrestling skills when a random number produces 2
            {
                R1.Wrestling();
                int harm = R1.ATK+R1.w_ATK-R2.DEF;
                R2.hp -= harm;
                cout<<R1.name<<"Use wrestling"<<"Attack,Yes"<<R2.name<<"Causes"<<harm<<"Point injury\n";
            }
        }
        if(i%2==0)//The enemy attacks when i is even
        {
            if(temp==0)//When the random number generates 0, the hero attacks normally
            {
                int harm = R2.ATK-R1.DEF;
                R1.hp -= harm;
                cout<<R2.name<<"Attack Common,Yes"<<R1.name<<"Causes"<<harm<<"Point injury\n";
            }
            if(temp==1)//When the random number produces 1, the enemy uses split palms
            {
                R2.zp();
                int harm = R2.ATK+R2.z_ATK-R1.DEF;
                R1.hp -= harm;
                cout<<R2.name<<"Use split palms"<<"Attack,Yes"<<R1.name<<"Causes"<<harm<<"Point injury\n";
            }
            if(temp==2)//When a random number generates 2, the enemy uses the tandem kick skill
            {
                R2.lht();
                int harm = R2.ATK+R2.l_ATK-R1.DEF;
                R1.hp -= harm;
                cout<<R2.name<<"Use tandem kicking"<<"Attack,Yes"<<R1.name<<"Causes"<<harm<<"Point injury\n";
            }
        }
        i++;
    }
    i = 0;
    if(R1.hp<=0)
    {
        cout<<R1.name<<"death\n";
        R2.EXP +=100;
        if(R2.EXP>=100)
        {
            R2.Lv++;
            cout<<R2.name<<"Experience gained"<<100<<" upgrade,The current rating is:"<<R2.Lv<<endl;
        }
        cout<<R2.name<<"Win victory";
    }
    if(R2.hp<=0)
    {
        cout<<R2.name<<"death\n";
        R1.EXP +=100;
        if(R1.EXP>=100)
        {
            R1.Lv++;
            cout<<R1.name<<"Experience gained"<<100<<" upgrade,The current rating is:"<<R1.Lv<<endl;
        }
        cout<<R1.name<<"Win victory";
    }
}

int main()
{
    CHero yx("Hero",5,2,1,0);
    CEnemy dr("Enemy",4,2,1,0);
    CBattle battle;
    battle.attacMode(yx,dr);
}

Code implementation diagram:

Posted by schme16 on Fri, 24 Jan 2020 08:20:46 -0800