多态类简单格斗游戏

这里写图片描述

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

class CCreature         //抽象类,基类
{
public:
    virtual void Attack(CCreature *p) = 0;
    virtual void Defend(CCreature *p) = 0;
    virtual void Hurted(int nPower) = 0;
    virtual int GetLifeValue() = 0;     //获取当前生命值
    virtual ~CCreature(){}
};

//***************************
//以下代码为Clark英雄角色的类定义

class Clark :public CCreature
{
private:
    int nPower;
    int nLifeValue;
public:
    Clark();
    void Attack(CCreature *p);
    void Defend(CCreature *p);
    void Hurted(int nPower);
    int GetLifeValue();
};
//*****************************
//Ralf英雄角色类定义
class Ralf :public CCreature
{
private:
    int nPower;
    int nLifeValue;
public:
    Ralf();
    void Attack(CCreature *p);
    void Defend(CCreature *p);
    void Hurted(int nPower);
    int GetLifeValue();
};
//Heidern英雄角色的类定义
class Heidern :public CCreature
{
private:
    int nPower;
    int nLifeValue;
public:
    Heidern();
    void Attack(CCreature *p);
    void Defend(CCreature *p);
    void Hurted(int nPower);
    int GetLifeValue();
};
//以下代码为Clark英雄角色的成员函数的实现
Clark::Clark()
{
    cout << "正在初始化英雄,请输入出师生命值,单次攻击带给对方的伤害值" << endl;
    cin >> nLifeValue >> nPower;
    while (nPower<=0||nLifeValue<=0)
    {
        if (nPower <= 0)
        {
            cout << "攻击伤害必须为正整数,请重新输入!";
            cin >> nPower;
        }
        if (nLifeValue <= 0)
        {
            cout << "生命值必须为正整数,请重新输入!";
            cin >> nLifeValue;
        }

    }

    cout << "初始化完成,准备战斗" << endl << endl;

}

void Clark::Attack(CCreature *p)
{
    cout << "Clark技能 召唤死灵" << endl;
    p->Hurted(nPower);
    p->Defend(this);

}

void Clark::Defend(CCreature *p)
{
    if (nLifeValue > 0)
    {
        cout << "Clark反击技能 圆盾" << endl;
        p->Hurted(nPower / 2);

    }
    else
        ;

}
void Clark::Hurted(int nPower)
{
    nLifeValue -= nPower;
    if (nLifeValue <= 0)
    {
        cout << "当前英雄死亡" << endl;
        nLifeValue = 0;

    }
    else
        cout << "受到伤害 生命值为" << nLifeValue << endl;

}
int Clark::GetLifeValue()
{
    return nLifeValue;
}
//以下代码为Ralf英雄角色类函数的实现



Ralf::Ralf()
{
    cout << "正在初始化英雄,请输入出师生命值,单次攻击带给对方的伤害值" << endl;
    cin >> nLifeValue >> nPower;
    while (nPower <= 0 || nLifeValue <= 0)
    {
        if (nPower <= 0)
        {
            cout << "攻击伤害必须为正整数,请重新输入!";
            cin >> nPower;
        }
        if (nLifeValue <= 0)
        {
            cout << "生命值必须为正整数,请重新输入!";
            cin >> nLifeValue;
        }

    }

    cout << "初始化完成,准备战斗" << endl << endl;

}

void Ralf::Attack(CCreature *p)
{
    cout << "Ralf技能 旋风双刀" << endl;
    p->Hurted(nPower);
    p->Defend(this);

}

void Ralf::Defend(CCreature *p)
{
    if (nLifeValue > 0)
    {
        cout << "Ralf反击技能 天使庇护" << endl;
        p->Hurted(nPower / 2);

    }
    else
        ;

}
void Ralf::Hurted(int nPower)
{
    nLifeValue -= nPower;
    if (nLifeValue <= 0)
    {
        cout << "当前英雄死亡" << endl;
        nLifeValue = 0;

    }
    else
        cout << "受到伤害 生命值为" << nLifeValue << endl;

}
int Ralf::GetLifeValue()
{
    return nLifeValue;
}


//以下为Heidern英雄角色函数的实现

Heidern::Heidern()
{
    cout << "正在初始化英雄,请输入出师生命值,单次攻击带给对方的伤害值" << endl;
    cin >> nLifeValue >> nPower;
    while (nPower <= 0 || nLifeValue <= 0)
    {
        if (nPower <= 0)
        {
            cout << "攻击伤害必须为正整数,请重新输入!";
            cin >> nPower;
        }
        if (nLifeValue <= 0)
        {
            cout << "生命值必须为正整数,请重新输入!";
            cin >> nLifeValue;
        }

    }

    cout << "初始化完成,准备战斗" << endl << endl;

}

void Heidern::Attack(CCreature *p)
{
    cout << "Heidern技能 魔法球" << endl;
    p->Hurted(nPower);
    p->Defend(this);

}

void Heidern::Defend(CCreature *p)
{
    if (nLifeValue > 0)
    {
        cout << "Heidern反击技能 神圣光环" << endl;
        p->Hurted(nPower / 2);

    }
    else
        ;

}
void Heidern::Hurted(int nPower)
{
    nLifeValue -= nPower;
    if (nLifeValue <= 0)
    {
        cout << "当前英雄死亡" << endl;
        nLifeValue = 0;

    }
    else
        cout << "受到伤害 生命值为" << nLifeValue << endl;

}
int Heidern::GetLifeValue()
{
    return nLifeValue;
}

//全局函数,战斗过程

int startgame()
{
    int n = 1, m = 1;
    CCreature *play1=0, *play2=0;           //分别指向第一玩家和第二玩家

    while (n)                                       //  开始创建英雄角色,第1战斗角色
    {
        cout << endl
            << "********** 开始创建英雄角色  **********" << endl
            << "********** 请选择第1战斗角色 **********" << endl
            << "**********     1 Clark       **********" << endl
            << "**********     2 Ralf        **********" << endl
            << "**********     3 Heidern     **********" << endl
            << "**********     0 退出游戏    **********" << endl;
        cin >> n;
        if (n >= 0 && n < 4)
            break;

    }
    //创建第一战斗角色

    switch (n)
    {
    case 0:
        return 1;
    case 1:
        play1 = new Clark;
        break;
    case 2:
        play1 = new Ralf;
        break;
    case 3:
        play1 = new Heidern;
        break;
    }

    while (m)                                       //  开始创建英雄角色,第2战斗角色
    {
        cout << endl
            << "********** 开始创建英雄角色  **********" << endl
            << "********** 请选择第2战斗角色 **********" << endl
            << "**********     1 Clark       **********" << endl
            << "**********     2 Ralf        **********" << endl
            << "**********     3 Heidern     **********" << endl
            << "**********     0 退出游戏    **********" << endl;
        cin >> m;
        if (m >= 0 && m < 4)
            break;

    }
    switch (m)
    {
    case 0:
        return 1;
    case 1:
        play2 = new Clark;
        break;
    case 2:
        play2 = new Ralf;
        break;
    case 3:
        play2 = new Heidern;
        break;
    }

    //游戏开始
    cout << "**********      游戏开始     **********" << endl;

    int fight = 1;      //值
    int fightnum = 1;   //次数


    while (1)
    {
        cout << endl
            << "***** 现在开始第" << fightnum << "轮战斗***** " << endl
            << "**********  请选择主动攻击方 **********" << endl
            << "**********  1  第1战斗角色   **********" << endl
            << "**********  2  第2战斗角色   **********" << endl
            << "**********  0   退出游戏     **********" << endl;

        cin >> fight;
        switch (fight)
        {
        case 1:
            cout << "玩家1开始攻击" << endl;
            play1->Attack(play2);
            break;
        case 2:
            cout << "玩家2开始攻击" << endl;
            play2->Attack(play1);
            break;
        case 0:
            cout << "异常结束游戏,没有输赢" << endl;
            return 1;
        }

        cout << "第" << fightnum << "轮战斗结束" << endl
            << "玩家1生命值为" << play1->GetLifeValue() << endl
            << "玩家2生命值为" << play2->GetLifeValue() << endl;
        fightnum++;

        if (play1->GetLifeValue() <= 0 || play2->GetLifeValue() <= 0)
            break; //跳出循环判定
    }

    //delete play1;
    //delete play2;

    cout << endl
        << "***************************************" << endl
        << "本局游戏结束,共进行" << fightnum << "次攻击" << endl
        << "本局游戏的胜利方是:";

    if (play1->GetLifeValue() == 0)
        cout << "游戏玩家2" << endl << endl;
    else if (play2->GetLifeValue() == 0)
        cout << "游戏玩家1" << endl << endl;
    return 1;

    delete play1;
    delete play2;

}

//全局函数开始游戏

int start()
{
    int n = 1;
    while (n)
    {
        cout << "******  欢迎进入游戏,请选择操作  ******" << endl
            << "********** 0 开始选择英雄角色 *********" << endl
            << "********** 1 退出游戏         *********" << endl;

        cin >> n;
        switch (n)
        {
        case 0:
            break;
        case 1:
            cout << "退出游戏,欢迎再来" << endl;
            return 1;
        default:
            break;
        }
    }

    //循环游戏设置
    n = 1;
    while (n)
    {
        n = startgame();
        cout << endl
            << "**********     请选择操作     *********" << endl
            << "**********     0 重新开始游戏 *********" << endl
            << "**********     1 退出游戏     *********" << endl;

        cin >> n;
        switch (n)
        {
        case 0:break;
        case 1:
            cout << "退出游戏,欢迎再来" << endl;
            return 1;
        default:
            break;
        }
    }
}
//主函数

int main()
{
    start();
    cout << "程序结束" << endl;
    return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值