编程语言
首页 > 编程语言> > C++ 多态练习,实现一个回合制小游戏

C++ 多态练习,实现一个回合制小游戏

作者:互联网

//头文件
#ifndef WUQI_H #define WUQI_H
#include <iostream> #include <string> using namespace std; class wuqi { public: virtual int Get_wuqi_gongji() = 0;//获取攻击力 virtual string Get_wuqi_name() = 0;//获取武器名称 virtual int Get_wuqi_baoji() = 0;//获取武器暴击率 virtual int Get_wuqi_xixue() = 0;//获取武器吸血率 virtual bool is_chufa(int num)=0;//触发条件 protected: string wuqiname; //武器名称 int gongji;//武器攻击 int baoji; //武器暴击率 int xixue; //武器吸血率 }; #endif

//创建一个狼牙棒类 

#ifndef LANGYABANG_H
#define LANGYABANG_H
#include "wuqi.h"
#include <iostream>
#include <string>
using namespace std;


class  langyabang:public wuqi
{
public:
    langyabang();
    ~langyabang();
    virtual int Get_wuqi_gongji();//获取攻击力
    virtual string Get_wuqi_name() ;//获取武器名称
    virtual int Get_wuqi_baoji();//获取武器暴击率
    virtual int Get_wuqi_xixue();//获取武器吸血率
    virtual bool is_chufa(int num) ;//触发条件

};


#endif

 

//创建一个棍子类

#ifndef GUNZI_H
#define GUNZI_H
#include "wuqi.h"
#include <iostream>
#include <string>
using namespace std;


class  gunzi :public wuqi
{
public:
    gunzi();
    ~gunzi();
    virtual int Get_wuqi_gongji();//获取攻击力
    virtual string Get_wuqi_name();//获取武器名称
    virtual int Get_wuqi_baoji();//获取武器暴击率
    virtual int Get_wuqi_xixue();//获取武器吸血率
    virtual bool is_chufa(int num);//触发条件

};


#endif

//创建一个英雄基类

#ifndef YINGXIONG_H
#define YINGXIONG_H
#include "langyabang.h"
#include <iostream>
#include <string>
using namespace std;


class  yingxiong
{
public:
    virtual int Get_yingxiong_gongji() = 0;//获取英雄攻击力
    virtual string Get_yingxiong_name() = 0;//获取英雄名称
    virtual int Get_yingxiong_baoji() = 0;//获取英雄暴击率
    virtual int Get_yingxiong_xixue() = 0;//获取英雄吸血率
    virtual int Get_yingxiong_xixuezhi(int num) = 0;//获取英雄吸血值 
    virtual int Get_yingxiong_def() = 0;//获取英雄防御力
    virtual int Get_yingxiong_hp() = 0;//获取英雄血量
    virtual bool is_chufa(int num) = 0;//触发条件
    virtual void Set_yingxiong_wuqi(wuqi* other) = 0; //英雄装备武器
    virtual wuqi* Get_yingxiong_wuqi() = 0;//获取英雄武器
    virtual string Getwuqi_name() = 0;//获取武器名称
protected:
    string yingxiongname; //英雄名称
    int Hp;//英雄血量
    int gongji;//英雄攻击
    int baoji; //英雄暴击率
    int def; //英雄防御
    wuqi* m_wuqi;//装备的武器
};


#endif

 

//创建一个英雄类

#ifndef JIANSHENG_H
#define JIANSHENG_H
#include "yingxiong.h"
#include <iostream>
#include <string>
using namespace std;


class  jiansheng:public yingxiong
{
public:
    jiansheng();
    ~jiansheng();
    virtual int Get_yingxiong_gongji() ;//获取攻击力
    virtual int Get_yingxiong_hp();//获取英雄血量
    virtual string Get_yingxiong_name() ;//获取武器名称
    virtual int Get_yingxiong_baoji() ;//获取武器暴击率
    virtual int Get_yingxiong_xixue() ;//获取武器吸血率
    virtual int Get_yingxiong_xixuezhi(int num);//获取英雄吸血值
    virtual int Get_yingxiong_def() ;//获取英雄防御力
    virtual bool is_chufa(int num) ;//触发条件
    virtual void Set_yingxiong_wuqi(wuqi* other) ; //装备武器

    virtual wuqi* Get_yingxiong_wuqi() ;//获取英雄武器
    virtual string Getwuqi_name() ;//获取武器名称
};


#endif

 

//创建一个怪物基类

#ifndef GUAIWU_H
#define GUAIWU_H

#include <iostream>
#include <string>
using namespace std;


class  guaiwu
{
public:
    virtual string Get_guaiwu_name() = 0; //获取怪物名称
    virtual int Get_guaiwu_gongji() = 0;//获取怪物攻击
    virtual int Get_guaiwu_def() = 0;//获取怪物防御力
    virtual int Get_guaiwu_hp() = 0;//获取怪物血量
protected:
    string guaiwuname; //怪物名称
    int gongji;//怪物攻击
    int def;   //怪物防御力
    int Hp;//怪物血量
};


#endif

//创建一个怪物

#ifndef BANSHOUREN_H
#define BANSHOUREN_H

#include <iostream>
#include <string>
#include "guaiwu.h"
using namespace std;


class  banshouren:public guaiwu
{
public:
    banshouren();
    ~banshouren();
    virtual string  Get_guaiwu_name() ; //获取怪物名称
    virtual int Get_guaiwu_gongji() ;//获取怪物攻击
    virtual int Get_guaiwu_def() ;//获取怪物防御力
    virtual int Get_guaiwu_hp() ;//获取怪物血量

};


#endif

 

//创建一个管理员类

#ifndef MANAGER_H
#define MANAGER_H
#include <time.h>
#include "banshouren.h"
#include "langyabang.h"
#include "gunzi.h"
#include "jiansheng.h"
#include <windows.h>
#define DELETEOBJ(p) {if(p!=NULL)delete p;p==NULL; }
using namespace std;



class manager
{

public:
    manager();
    ~manager();
    void  initGame(); //初始化游戏
    void yingxiongask();//英雄攻击怪物
    void guaiwuask();//怪物攻击英雄
    int Get_yingxiong_hp_now(); //获取当前英雄生命值
    int Get_guaiwu_hp_now();//获取当前怪物生命值
    int showgame();//开始游戏回合
    void restgame();//游戏重开
    int showmassager();//游戏结束提示
    int round = 1;//游戏回合
private:
    wuqi* m_langyabang;     //武器类
    wuqi* m_gunzi;          //武器类
    yingxiong* m_yingxiong; //英雄类
    guaiwu* m_guaiwu;      //怪物类
    int yingxiong_hp_now; //英雄当前剩余生命值
    int guaiwu_hp_now;    //怪物当前剩余生命值
    
};




#endif

 

 

//狼牙棒cpp文件

#include "langyabang.h"

//构造
langyabang::langyabang()
{
    this->wuqiname = "狼牙棒";
    this->gongji = 100;
    this->baoji = 20;
    this->xixue = 15;

}
//析构
langyabang::~langyabang()
{

}
int langyabang::Get_wuqi_gongji()//获取攻击力
{
    return this->gongji;
}
string langyabang::Get_wuqi_name()//获取武器名称
{
    if (this == NULL)
        return "双手";
    return this->wuqiname;
}
int langyabang::Get_wuqi_baoji()//获取武器暴击率
{
    return this->baoji;
}
int langyabang::Get_wuqi_xixue()//获取武器吸血率
{
    return this->xixue;
}
bool langyabang::is_chufa(int num)//触发条件
{
    if (rand() % 100 < num)
        return true;
    return false;

}

 

//棍子cpp文件

 

#include "gunzi.h"

//构造
gunzi::gunzi()
{
    this->wuqiname = "棍子";
    this->gongji = 20;
    this->baoji = 0;
    this->xixue = 0;

}
//析构
gunzi::~gunzi()
{

}
int gunzi::Get_wuqi_gongji()//获取攻击力
{
    return this->gongji;
}
string gunzi::Get_wuqi_name()//获取武器名称
{
    if (this == NULL)
        return "双手";
    return this->wuqiname;
}
int gunzi::Get_wuqi_baoji()//获取武器暴击率
{
    return this->baoji;
}
int gunzi::Get_wuqi_xixue()//获取武器吸血率
{
    return this->xixue;
}
bool gunzi::is_chufa(int num)//触发条件
{
    if (rand() % 100 < num)
        return true;
    return false;

}

//英雄cpp

#include "jiansheng.h"


jiansheng::jiansheng()//构造
{
    this->yingxiongname = "剑圣";
    this->Hp = 800;
    this->gongji = 150;
    this->def = 50;
    this->baoji = 20;
    this->m_wuqi = NULL;
}
jiansheng::~jiansheng() //析构
{

}
 string jiansheng::Getwuqi_name()//获取武器名称
{
    if (this->Get_yingxiong_wuqi() == NULL)
        return "空手";
    return this->Get_yingxiong_wuqi()->Get_wuqi_name();

}
int jiansheng::Get_yingxiong_gongji()//获取攻击力
{
    if (this->m_wuqi == NULL)
        return this->gongji;
    else
        return this->gongji + this->m_wuqi->Get_wuqi_gongji();
}
string jiansheng::Get_yingxiong_name()//获取英雄名称
{
    return this->yingxiongname;
}

int jiansheng::Get_yingxiong_hp()//获取英雄血量
{
    return this->Hp;
}
int jiansheng::Get_yingxiong_baoji()//获取英雄暴击率
{
    if (this->m_wuqi == NULL)
        return this->baoji;
    else
        return this->baoji + this->m_wuqi->Get_wuqi_baoji();
}

int jiansheng::Get_yingxiong_xixuezhi(int num)//获取英雄吸血值
{
    if (this->m_wuqi == NULL)
        return 0;
    else
    return  num /100* this->Get_yingxiong_xixue();

}
int jiansheng::Get_yingxiong_xixue()//获取英雄吸血率
{
    if (this->m_wuqi == NULL)
        return 0;
    else
        return this->m_wuqi->Get_wuqi_xixue();
}
int jiansheng::Get_yingxiong_def()//获取英雄防御力
{
    return this->def;
}

bool jiansheng::is_chufa(int num)//触发条件
{
    if (rand() % 100 < num)
        return true;
        return false;

}

void jiansheng::Set_yingxiong_wuqi(wuqi* other) //装备武器
{
    this->m_wuqi=other;
}
wuqi* jiansheng::Get_yingxiong_wuqi()//获取英雄武器
{
    if (this->m_wuqi == NULL)
        return NULL;
    return this->m_wuqi;
}

//怪物cpp文件

#include "banshouren.h"
banshouren::banshouren()
{
    this->guaiwuname = "半兽人";
    this->gongji = 200;
    this->def = 50;
    this->Hp = 2000;
}
banshouren::~banshouren()
{

}
string banshouren::Get_guaiwu_name() //获取怪物名称
{
    return this->guaiwuname;
}
int banshouren::Get_guaiwu_gongji()//获取怪物攻击
{
    return this->gongji;
}
int banshouren::Get_guaiwu_def()//获取怪物防御力
{
    return this->def;
}
int banshouren::Get_guaiwu_hp()//获取怪物血量
{
    return this->Hp;
}

 

//管理者cpp文件

#include "manager.h"

manager::manager()
{
    srand((unsigned)time(NULL));
    this->m_guaiwu = new banshouren;
    this->m_langyabang = new langyabang;
    this->m_gunzi = new gunzi;
    this->m_yingxiong = new jiansheng;
    this->guaiwu_hp_now = m_guaiwu->Get_guaiwu_hp();
    this->yingxiong_hp_now = m_yingxiong->Get_yingxiong_hp();
}

int manager::Get_yingxiong_hp_now()
{
    return this->yingxiong_hp_now;
}
int manager::Get_guaiwu_hp_now()
{
    return this->guaiwu_hp_now;
}


manager::~manager()
{
    DELETEOBJ(m_langyabang);
    DELETEOBJ(m_gunzi);
    DELETEOBJ(m_yingxiong);
    DELETEOBJ(m_guaiwu);
}
void  manager::initGame()
{
    int num;
    cout << "请为英雄装备一把武器,输入1为狼牙棒,输入2为棍子,输入其他则不装备任何武器" << endl;
    cin >> num;
    switch (num)
    {
    case 1:
        m_yingxiong->Set_yingxiong_wuqi(m_langyabang);
        cout << "英雄装备了:" << m_yingxiong->Getwuqi_name() << " 就上了!" << endl;
        break;
    case 2:
        m_yingxiong->Set_yingxiong_wuqi(m_gunzi);
        cout << "英雄装备了:" << m_yingxiong->Getwuqi_name() << " 就上了!" << endl;
        break;
    default:
        cout << "英雄空手就上了!" << endl;
        break;
    }
   
}
void manager::yingxiongask()
{
    int shanghai;
    //英雄攻击怪物
    if (this->m_yingxiong->is_chufa( this->m_yingxiong->Get_yingxiong_baoji() ) == true)//判断是否触发暴击
    {
        shanghai = (this->m_yingxiong->Get_yingxiong_gongji() - this->m_guaiwu->Get_guaiwu_def()) * 2;  //(攻击力-防御力)*2
        cout << "英雄使用" << m_yingxiong->Getwuqi_name() << "对怪物造成了一次暴击伤害:" << shanghai << endl;
    }
    else
    {
        shanghai = this->m_yingxiong->Get_yingxiong_gongji() - this->m_guaiwu->Get_guaiwu_def(); //英雄攻击力 - 防御力
        cout << "英雄使用" << m_yingxiong->Getwuqi_name() << "对怪物造成了一次伤害:" << shanghai << endl;
    }
    this->guaiwu_hp_now -= shanghai;
    //英雄吸血
    if (yingxiong_hp_now + this->m_yingxiong->Get_yingxiong_xixuezhi(shanghai) >= m_yingxiong->Get_yingxiong_hp()) //判断吸血后是否大于总血量,若大于总血量则回满血量并不会溢出, 否则当前血量+吸血量为当前英雄剩余血量
    {
        yingxiong_hp_now = this->m_yingxiong->Get_yingxiong_hp();
        cout << "英雄吸了一口血:" << this->m_yingxiong->Get_yingxiong_xixuezhi(shanghai) << endl;
    }
    else
    {
        yingxiong_hp_now += this->m_yingxiong->Get_yingxiong_xixuezhi(shanghai);  
        cout << "英雄吸了一口血:" << this->m_yingxiong->Get_yingxiong_xixuezhi(shanghai) << endl;
    }

}
void manager::guaiwuask()
{
    int shanghai; //定义怪物造成的伤害
    shanghai = this->m_guaiwu->Get_guaiwu_gongji() - this->m_yingxiong->Get_yingxiong_def();
    cout << "怪物对英雄造成了一次伤害:" << shanghai << endl;
    this->yingxiong_hp_now -= shanghai;
}
void manager::restgame()//游戏重开
{
    system("cls");
    guaiwu_hp_now = m_guaiwu->Get_guaiwu_hp();
    yingxiong_hp_now = m_yingxiong->Get_yingxiong_hp();
    this->m_yingxiong->Set_yingxiong_wuqi(NULL);
    round = 1;
    initGame();
}

int manager::showmassager()
{
    int reslut = MessageBox(NULL, TEXT("游戏结束,是否选择重开?"), TEXT("确认"), MB_ICONINFORMATION | MB_YESNO);
    switch (reslut)
    {
    case IDYES:
    {
        restgame();
        return 100; //
    }

    case IDNO:
        return 101;
    }

}

int  manager::showgame()//开始游戏回合
{
   
    while (1)
    {
        Sleep(1000);
        system("cls");
        printf("--------------------------第%d回合----------------------------\n", round);
            yingxiongask();
            guaiwuask();
        if (Get_guaiwu_hp_now() <= 0)
        {
            cout << "怪物死亡~!" << endl;
            guaiwu_hp_now = 0; //生命值只能为0 不能为负数
            break;
        }
         if (Get_yingxiong_hp_now() <= 0)
        {
            cout << "英雄死亡~!" << endl;
            yingxiong_hp_now = 0;//生命值只能为0 不能为负数

            break;

        }
        cout << "当前英雄血量:" << Get_yingxiong_hp_now() << endl;
        cout << "当前怪物血量:" << Get_guaiwu_hp_now() << endl;

        
        round++;

        }

    return 0;
}

 

//主函数

 

#include "manager.h"

int main()
{
    manager temp;
    temp.initGame(); //初始化游戏
    temp.showgame(); //开始游戏
    while (1)  //循环判断是否需要重开游戏,代码100为重开否则直接退出
    {   
        if (temp.showmassager() == 100)
        {
            temp.showgame();
        }
        else 
            return 0;
    }

    return 0;

}

 

 

最终实现效果如下

 

 

标签:回合制,return,yingxiong,Get,int,多态,C++,wuqi,virtual
来源: https://www.cnblogs.com/shenji/p/12821496.html