1.
#include <iostream>
using namespace std;
int monster=10000;
class Hero
{
string name;
int hp;
int attack;
public:
Hero():name("张三"),hp(100),attack(10){}
Hero(string name,int hp,int attack):name(name),hp(hp),attack(attack){}
virtual int Atk()
{
monster-=attack;
hp-=10;
if(hp<=0)
{
return 0;
}
cout << "-------------" << name << "hp=" << hp << "-------------" << endl;
return hp;
}
};
class Master:public Hero
{
int ap_ack;
public:
Master(string name,int hp,int attack,int ap_ack):Hero(name,hp,attack),ap_ack(ap_ack){}
int Atk()
{
int res=Hero::Atk();
if(res<=0)
{
return -1;
}
monster-=ap_ack;
return 0;
}
};
class Shooter:public Hero
{
int ad_ack;
public:
Shooter(string name,int hp,int attack,int ad_ack):Hero(name,hp,attack),ad_ack(ad_ack){}
int Atk()
{
int res=Hero::Atk();
if(res<=0)
{
return -1;
}
monster-=ad_ack;
return 0;
}
};
int main()
{
int time=1;
Master m1("甄姬",150,50,300);
Shooter s1("鲁班",120,50,400);
while(monster>0)
{
Hero* p1=&m1;
if(p1->Atk()==0)
{
cout << "-------------monster=" << monster << "-------------" << endl;
}
Hero* p2=&s1;
if(p2->Atk()==0)
{
cout << "-------------monster=" << monster << "-------------" << endl;
}
cout << " " << endl;
time++;
}
cout << "-----------怪兽在第" << time << "次被消灭-----------" << endl;
return 0;
}
2.
#include <iostream>
using namespace std;
int monster=10000;
class Hero
{
string name;
int hp;
int attack;
public:
Hero():name("张三"),hp(100),attack(10){}
Hero(string name,int hp,int attack):name(name),hp(hp),attack(attack){}
virtual int Atk()
{
monster-=attack;
hp-=10;
if(hp<=0)
{
return 0;
}
cout << "-------------" << name << "hp=" << hp << "-------------" << endl;
return hp;
}
};
class Master:public Hero
{
int ap_ack;
public:
Master(string name,int hp,int attack,int ap_ack):Hero(name,hp,attack),ap_ack(ap_ack){}
int Atk()
{
int res=Hero::Atk();
if(res<=0)
{
return -1;
}
monster-=ap_ack;
return 0;
}
};
class Shooter:public Hero
{
int ad_ack;
public:
Shooter(string name,int hp,int attack,int ad_ack):Hero(name,hp,attack),ad_ack(ad_ack){}
int Atk()
{
int res=Hero::Atk();
if(res<=0)
{
return -1;
}
monster-=ad_ack;
return 0;
}
};
int main()
{
int time=1;
Master m1("甄姬",150,50,300);
Shooter s1("鲁班",120,50,400);
while(monster>0)
{
Hero* p1=&m1;
if(p1->Atk()==0)
{
cout << "-------------monster=" << monster << "-------------" << endl;
}
Hero* p2=&s1;
if(p2->Atk()==0)
{
cout << "-------------monster=" << monster << "-------------" << endl;
}
cout << " " << endl;
time++;
}
cout << "-----------怪兽在第" << time << "次被消灭-----------" << endl;
return 0;
}