打怪兽问题

题目:一个游戏中有很多怪物(Monster),怪物之间可能要发生战斗(fight),每场战斗都是一个怪物与另一怪物之间的一对一战斗。每个怪物都有自己
的速度(Speed)、生命值(hitpoint)、攻击力值(damage)和防御力值(defense);战斗时,两个怪物依次攻击对方,即怪物a首先攻击怪物b, 然后轮到怪物b
攻击怪物a,之后,怪物a再次攻击怪物b,…,直到一方生命值为0;战斗时,由速度快的一方首先发起攻击;若速度一样,比较生命值,由高者首先攻击;

若生命值也相等,比较攻击力,由高者首先攻击;若攻击力还相等,比较防御力,由高者首先攻击;若四项都相等,则选择任一方首先攻击;

怪物A攻击怪物B时,会给怪物B造成伤害,使得怪物B的生命值降低,降低值为:2*A的攻击力-B的防御力,最小为1。请根据你对上述描述的理解,

定义并实现怪物类Monster,成员的设计可以任意,但要求该类至少有一个成员函数fight,用来描述与另外一个怪物进行战斗的过程。

不必考虑怪物的生命值减少至0后如何处理。

//main.cpp

#include <iostream>
#include"Monster.h"
#include"Myfunc.h" 
using namespace std;
int main()
{
	Welcome();
	for(;;)
	{
		system("cls");
	    int M1[4],M2[4];
	    cout<<"\t\t\t请输入怪兽A的相关信息 :\n\n\n\n";
	    cout<<"\n\nspeed   :\t";   cin>>M1[0];
	    cout<<"\n\nhitpoint:\t";   cin>>M1[1];
	    cout<<"\n\ndamage  :\t";   cin>>M1[2];
	    cout<<"\n\ndefence :\t";   cin>>M1[3];
	    Monster a(M1);
	    Waiting(); 
	    cout<<"\t\t\t请输入怪兽B的相关信息 :\n\n\n\n";
	    cout<<"\n\nspeed   :\t";   cin>>M2[0];
	    cout<<"\n\nhitpoint:\t";   cin>>M2[1];
	    cout<<"\n\ndamage  :\t";   cin>>M2[2];
	    cout<<"\n\ndefence :\t";   cin>>M2[3];
	    Monster b(M2);
        Waiting();
        Homepage(M1,M2);
        Defeat(a,b);
        cout<<"\t\t\t是否重新开始?(y/n)\n\n\n\n";
        char r;
        cin>>r;
        Correct(r);
        if(r=='Y'||r=='y')
           continue;
        else
           break;
	}
    Quit();
    return 0;
}

//Monster.h

#ifndef MONSTER_H
#define MONSTER_H


class Monster
{
    public:
        Monster(int (&M)[4]);
        bool AttackEarlierTo(Monster & another);
        int Attack(Monster & another);
        bool Fight(Monster & another);
    private:
        int speed;
        int hitpoint;
        int damage;
        int defence;
};

#endif // MONSTER_H

//Monster.cpp

#include<iostream>
#include "Monster.h"
using namespace std;
Monster::Monster(int (&M)[4])
{
    speed=M[0];
    hitpoint=M[1];
    damage=M[2];
    defence=M[3];
}

bool Monster::AttackEarlierTo(Monster & another)
{
    if(speed!=another.speed)
        return (speed>another.speed? true:false); 
    if(hitpoint!=another.hitpoint)
        return (hitpoint>another.hitpoint? true:false);
    if(damage!=another.damage)
        return (damage>another.damage? true:false);
    if(defence!=another.defence)
        return (defence>another.defence? true:false);
}

int Monster::Attack(Monster & another)
{
    int harm;
    harm=2*damage-another.defence;
    if(harm<1)
        harm=1;
    another.hitpoint-=harm;
    if(another.hitpoint<0)
        another.hitpoint=0;
    return another.hitpoint;
}

bool Monster::Fight(Monster & another)
{
    if(AttackEarlierTo(another))
        if(Attack(another)==0)
            return true;
    while(1)
    {
        if(another.Attack(*this)==0)
            return false;
        if(Attack(another)==0)
            return true;
    }
             
}

//Myfunc.h

#ifndef MYFUNC_H
#define MYFUNY_H

#include<iostream>
#include<windows.h>
#include<conio.h>
using namespace std;


void Welcome()
{
        system("color 4f");
        cout<<"\n\n";
        cout<<"  \t┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
        cout<<"  \t┃**********************************************************┃\n";
        cout<<"  \t┃***┏━━━━━━━━━━━━━━━━━━━━━━━━┓***┃\n";
        cout<<"  \t┃***┃************************************************┃***┃\n";
        cout<<"  \t┃***┃***                                         ****┃***┃\n";
        cout<<"  \t┃***┃***                                         ****┃***┃\n";
        cout<<"  \t┃***┃***           欢迎试玩打怪兽游戏            ****┃***┃\n";
        cout<<"  \t┃***┃***                                         ****┃***┃\n";
        cout<<"  \t┃***┃***                                         ****┃***┃\n";
        cout<<"  \t┃***┃***                        Super pan 制作   ****┃***┃\n";
        cout<<"  \t┃***┃***                                         ****┃***┃\n";
        cout<<"  \t┃***┃***                                         ****┃***┃\n";
        cout<<"  \t┃***┃************************************************┃***┃\n";
        cout<<"  \t┃***┗━━━━━━━━━━━━━━━━━━━━━━━━┛***┃\n";
        cout<<"  \t┃**********************************************************┃\n";
        cout<<"  \t┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n";
        getch(); 
}


void Homepage(int(&M1)[4],int(&M2)[4])
{
	   cout<<"\n\n";
       cout<<"\t┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
	   cout<<"\t┃************************************************************┃\n";
       cout<<"\t┃*                             *                            *┃\n";
       cout<<"\t┃*       Monster A             *         Monster B          *┃\n";
       cout<<"\t┃*                             *                            *┃\n";
       cout<<"\t┃************************************************************┃\n";
       cout<<"\t┃*                             *                            *┃\n";
       cout<<"\t┃*  speed:           "<<M1[0]<<"         *  speed:           "<<M2[0]<<"        *┃\n";
       cout<<"\t┃*                             *                            *┃\n";
       cout<<"\t┃************************************************************┃\n";
       cout<<"\t┃*                             *                            *┃\n";
       cout<<"\t┃*  hitpoint:        "<<M1[1]<<"         *  hitpoint:        "<<M2[1]<<"        *┃\n";
       cout<<"\t┃*                             *                            *┃\n";
       cout<<"\t┃************************************************************┃\n";
       cout<<"\t┃*                             *                            *┃\n";
       cout<<"\t┃*  damage:          "<<M1[2]<<"         *  damage:          "<<M2[2]<<"        *┃\n";
       cout<<"\t┃*                             *                            *┃\n";
       cout<<"\t┃************************************************************┃\n";
       cout<<"\t┃*                             *                            *┃\n";
       cout<<"\t┃*  defence:         "<<M1[3]<<"         *  defence:         "<<M2[3]<<"        *┃\n";
       cout<<"\t┃*                             *                            *┃\n";
       cout<<"\t┃************************************************************┃\n";
       cout<<"\t┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n";
       getch();
}




void Defeat(Monster a,Monster b)
{
    system("cls");
    cout<<"\n\n";
    cout<<"\t┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
    cout<<"\t┃************************************************************┃\n";
    cout<<"\t┃*                                                          *┃\n";
    cout<<"\t┃*            A          VS           B                     *┃\n";
    cout<<"\t┃*                                                          *┃\n";
    cout<<"\t┃************************************************************┃\n";
    cout<<"\t┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
    cout<<"\n\n\n\n\n";
    cout<<"\t┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
    cout<<"\t┃************************************************************┃\n";
    cout<<"\t┃*                                                          *┃\n";
    if(a.Fight(b))
    {
    	cout<<"\t┃*                  A    Win    !                           *┃\n";
    }
    else
    {
    	cout<<"\t┃*                  B    Win    !                           *┃\n";
    }
    cout<<"\t┃*                                                          *┃\n";
    cout<<"\t┃************************************************************┃\n";
    cout<<"\t┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n\n";
	getch();
	system("cls");
}

bool Correct(char & r)
{
     while(r!='y'&&r!='Y'&&r!='n'&&r!='N')  
     {
           cout<<"\n\n输入错误,请重新输入   :   ";
           cin>>r;
     }
     return true;
}

void Waiting()
{
        system("cls");
        int i;
        printf("\n\t\t\t存储中...........");
        for(i=0;i<8;i++)
        {
                switch(i%4)
                {
                case 1:printf("\b\\");break;
                case 2:printf("\b-");break;
                case 3:printf("\b/");break;
                case 0:printf("\b|");break;
                }
                Sleep(100);
        }
        system("cls");
}


void Quit()
{
        system("cls");
        cout<<"\n\n";
        cout<<"\t┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
        cout<<"\t┃************************************************************┃\n";
        cout<<"\t┃*                                                          *┃\n";
        cout<<"\t┃*                   谢谢使用本程序                         *┃\n";
        cout<<"\t┃*                                                          *┃\n";
        cout<<"\t┃************************************************************┃\n";
        cout<<"\t┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n\n";
}





#endif














  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
嗨!我可以帮你解答这个问题。根据描述,mybing每次释放技能可以对一个怪物造成 $a$ 伤害,对其他每个怪物造成 $b$ 伤害。 假设我们按照以下策略来消灭怪物: 1. mybing首先使用技能攻击血量最高的怪物,造成 $a$ 伤害。 2. 接下来,mybing使用技能攻击其他每个怪物,每个怪物都会受到 $b$ 伤害。 在这种策略下,我们可以计算每次技能释放后所有怪物的剩余血量: - 对于第一次释放技能后,血量最高的怪物的血量减少了 $a$,剩余血量为 $h_i - a$。 - 对于其他每个怪物,血量减少了 $b$,剩余血量为 $h_i - b$。 如果我们令 $x$ 表示 mybing 需要释放的技能次数,那么第一次释放技能后,剩余血量最高的怪物的血量应该满足: $h_i - a \leq 0$ 解得: $h_i \leq a$ 换句话说,mybing 需要找到血量最高的怪物,并确保其血量小于等于 $a$。 对于其他每个怪物,剩余血量应该满足: $h_i - b \leq 0$ 解得: $h_i \leq b$ 换句话说,mybing 需要确保每个怪物的血量都小于等于 $b$。 综上所述,mybing 需要找到所有怪物中血量最高的一个,并确保其血量小于等于 $a$,同时确保所有怪物的血量都小于等于 $b$。 因此,mybing 最少需要释放的技能次数为 $x = \lceil \frac{h_{\text{max}}}{a} \rceil$,其中 $h_{\text{max}}$ 表示所有怪物中血量最高的值。 希望对你有帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值