打怪兽问题

题目:一个游戏中有很多怪物(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 
#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
#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
#include
#include
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:           "<>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

打怪兽问题_第1张图片



打怪兽问题_第2张图片



打怪兽问题_第3张图片



打怪兽问题_第4张图片



打怪兽问题_第5张图片



你可能感兴趣的:(C++)