c++自学笔记003-用选择,循环语句编写的模拟关羽和吕布战斗的控制台程序

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    //使用循环模拟游戏角色对战过程
    //游戏角色双方生命值初始为1000,角色攻击力为100,角色防御为50(即可以减少防御/100)的伤害。
    //角色攻击分为普通攻击,蓄力一击,致命一击,分别造成100%,150%,250%的伤害。概率分别为60%,30%,10%
    //角色每次攻击造成的实际伤害均有波动,范围在80%~120%之间。
    //双方角色互相攻击一次为一个回合,回合内每次出招先后顺序随机判定
    //在遭受对方先手攻击时,有30%几率闪避此次攻击。
    srand(unsigned(time(NULL)));
    int i=1;//回合数
    double r11,r21,r31,r41;  //r11 攻击先后顺序,小于0.5角色A先手。r21攻击浮动范围,r31闪避概率判定,大于0.3则判定为有效。否则,被闪躲。r42决定普通攻击还是致命一击
    double r12,r22,r32,r42;   //同上

    double hp1 = 1000.0, hp2 = 1000.0;//AB角色生命值
    double attack1 = 100.0, attack2 = 100.0;    //角色攻击力
    double a1,a2;   //角色每次攻击造成的实际伤害值

    while (hp1>0 && hp2>0)
    {
        cout << endl << endl << endl << endl;
        cout << "回合:" << i <<endl;
        i++;
        r11 = rand()/(RAND_MAX + 1.0);
        r21 = rand()/(RAND_MAX + 1.0);
        r31 = rand()/(RAND_MAX + 1.0);
        r41 = rand()/(RAND_MAX + 1.0);

        r12 = rand()/(RAND_MAX + 1.0);
        r22 = rand()/(RAND_MAX + 1.0);
        r32 = rand()/(RAND_MAX + 1.0);
        r42 = rand()/(RAND_MAX + 1.0);

        if(r11>r22) //判断谁先手,是则关羽先手
        {
            while(1) //计算关羽攻击是否命中
            {
                a1 = 100*(0.8+(r21*0.4))/2;   //A实际攻击伤害
                //cout << a1 << endl;

                if(r31>=0.3)//判断攻击是否命中,为真则命中。
                    goto loop1;
                else
                {
                    a1=0;
                    hp2 -=a1;
                    cout << "关羽的攻击被闪避了" << endl;
                     cout << endl;
                    break;
                }

            }

            while(1)//判断关羽伤害
            {
loop1:
                if(r41<=0.6)
                {
                    a1 = a1*1;
                    hp2 = hp2 - a1;
                    cout << "关羽发动普通攻击,对吕布造成了" << a1 << "点伤害。" << endl; cout << endl;
                    break;
                }
                else if(r41>0.6&&r41<=0.9)
                {
                    a1 = a1*2.5;
                    hp2 = hp2 - a1;
                    cout << "关羽发动蓄力一击,对吕布造成了" << a1 << "点伤害!" << endl; cout << endl;
                    break;
                }
                else if(r41>0.9)

                    a1 = a1*6;
                hp2 = hp2 - a1;
                cout << "关羽发动致命一击,对吕布造成了" << a1 << "点伤害。" << endl; cout << endl;

                break;
            }
            if(hp1 <=0 || hp2 <=0) break;
                        cout << "关羽:"<< hp1 << "," << "吕布:" << hp2 << endl; cout << endl;
            while(1) //计算吕布攻击效果是否命中
            {


                //cout << r11 << "," << r21<< ","  << r31<< ","  << r41 <<endl;
                //cout << r12 << "," << r22 << "," << r32<< ","  << r42 <<endl;

                a2 = 100*(0.8+(r22*0.4))/2;   //B实际攻击伤害
                //cout << a1 <<","<< a2 << endl;

                if(r32>=0.3)//判断攻击是否命中,为真则命中。
                    goto loop2;
                else
                {
                    a2=0;
                    hp1 -=a2;
                    cout << "吕布的攻击被闪避了" << endl; cout << endl;
                    break;
                }

            }

            while(1)//判断吕布伤害
            {
loop2:
                if(r42<=0.6)
                {
                    a2 = a2*1;
                    hp1 = hp1 - a2;
                    cout << "吕布发动普通攻击,对关羽造成了" << a2 << "点伤害。" << endl;
                    break;
                }
                else if(r42>0.6&&r42<=0.9)
                {
                    a2 = a2*2.5;
                    hp1 = hp1 - a2;
                    cout << "吕布发动蓄力一击,对关羽造成了" << a2 << "点伤害!" << endl;
                    break;
                }
                else if(r42>0.9)
                    a2 = a2*6;
                hp1 = hp1 - a2;
                cout << "吕布发动致命一击,对关羽造成了" << a2 << "点伤害。" << endl;
                break;
            }
            cout << endl; cout << endl;
            if(hp1 <=0 || hp2 <=0) break;
                        cout << "关羽:"<< hp1 << "," << "吕布:" << hp2 << endl; cout << endl;
        }
        else
        {
            while(1) //计算吕布攻击效果是否命中
            {


                //cout << r11 << "," << r21<< ","  << r31<< ","  << r41 <<endl;
                //cout << r12 << "," << r22 << "," << r32<< ","  << r42 <<endl;

                a2 = 100*(0.8+(r22*0.4))/2;   //B实际攻击伤害
                //cout << a1 <<","<< a2 << endl;

                if(r32>=0.3)//判断攻击是否命中,为真则命中。
                    goto loop3;
                else
                {
                    a2=0;
                    hp1 -=a2;
                    cout << "吕布的攻击被闪避了" << endl;
                    break;
                }

            }

            while(1)//判断吕布伤害
            {
loop3:
                if(r42<=0.6)
                {
                    a2 = a2*1;
                    hp1 = hp1 - a2;
                    cout << "吕布发动普通攻击,对关羽造成了" << a2 << "点伤害。" << endl; cout << endl;
                    break;
                }
                else if(r42>0.6&&r42<=0.9)
                {
                    a2 = a2*2.5;
                    hp1 = hp1 - a2;
                    cout << "吕布发动蓄力一击,对关羽造成了" << a2 << "点伤害!" << endl; cout << endl;
                    break;
                }
                else if(r42>0.9)

                    a2 = a2*6;
                hp1 = hp1 - a2;
                cout << "吕布发动致命一击,对关羽造成了" << a2 << "点伤害。" << endl; cout << endl;
                break;
            }
            if(hp1 <=0 || hp2 <=0) break;
                        cout << "关羽:"<< hp1 << "," << "吕布:" << hp2 << endl; cout << endl;
            while(1) //计算关羽攻击是否命中
            {
                a1 = 100*(0.8+(r21*0.4))/2;   //A实际攻击伤害
                //cout << a1 << endl;

                if(r31>=0.3)//判断攻击是否命中,为真则命中。
                    goto loop4;
                else
                {
                    a1=0;
                    hp2 -=a1;
                    cout << "关羽的攻击被闪避了" << endl; cout << endl;
                    break;
                }

            }

            while(1)//判断关羽伤害
            {
loop4:
                if(r41<=0.6)
                {
                    a1 = a1*1;
                    hp2 = hp2 - a1;
                    cout << "关羽发动普通攻击,对吕布造成了" << a1 << "点伤害。" << endl;
                    break;
                }
                else if(r41>0.6&&r41<=0.9)
                {
                    a1 = a1*2.5;
                    hp2 = hp2 - a1;
                    cout << "关羽发动蓄力一击,对吕布造成了" << a1 << "点伤害!" << endl;
                    break;
                }
                else if(r41>0.9)

                    a1 = a1*6;
                hp2 = hp2 - a1;
                cout << "关羽发动致命一击,对吕布造成了" << a1 << "点伤害。" << endl;
                break;

            }
            cout << endl; cout << endl;
            if(hp1 <=0 || hp2 <=0) break;
            cout << "关羽:"<< hp1 << "," << "吕布:" << hp2 << endl; cout << endl;
        }


    }
    if(hp1>0) cout << "关羽获胜!剩余" << hp1 << "生命值:" << endl;
    else cout << "吕布获胜!剩余" << hp2 << "生命值:" << endl; cout << endl;
    return 0;
 }

细节上面还有很多不足,先就这样吧,花了我半天时间。。。贴给未来的我看看。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值