魔兽系列又出新篇章~其实就是在上周的基础上加入几个属性(武器,士气,忠诚度),有了老师的高质量底板,两个小时不到就完成了添加工作,唉不知道如果用我原来那个烂模板要多长时间才能改好。。。
先是题目展示(这题真是越来越长了,不知道终极版会有多长(⊙o⊙)…):
Case:1 000 red iceman 1 born with strength 5,1 iceman in red headquarter It has a bomb 000 blue lion 1 born with strength 6,1 lion in blue headquarter It's loyalty is 14 001 red lion 2 born with strength 6,1 lion in red headquarter It's loyalty is 9 001 blue dragon 2 born with strength 3,1 dragon in blue headquarter It has a arrow,and it's morale is 3.67 002 red wolf 3 born with strength 7,1 wolf in red headquarter 002 blue ninja 3 born with strength 4,1 ninja in blue headquarter It has a sword and a bomb 003 red headquarter stops making warriors 003 blue iceman 4 born with strength 5,1 iceman in blue headquarter It has a bomb 004 blue headquarter stops making warriors
新学到一点,如果把类型为string的变量用%s打出来会报错,需要在变量名后面加一个 .c_str() 才能正常打出。
嗯,经过两周的学习,对构造函数还有成员的调用更熟练了。同时郭老师的底稿真容易扩充功能啊,更加深刻的体会到了一个好的结构设计能为之后的修改提供多少方便!
然后对Warrior类加了几个属性,增加了打印属性的函数,工作量不是很大就轻松AC了。但!是!魔兽世界终极版已经来临了,目测代码量要翻倍
~~(>_<)~~
唉等到把终极版攻克了再来分享成功的喜悦吧~~~
下面是本次作业的代码,改的可能没啥水平,欢迎大家提出意见建议:
#include
#include
#include
using namespace std;
const int WARRIOR_NUM = 5;
const int WEAPON_NUM = 3;
/*
string Warrior::names[WARRIOR_NUM] = { "dragon","ninja","iceman","lion","wolf" };
红方司令部按照 iceman、lion、wolf、ninja、dragon 的顺序制造武士。
蓝方司令部按照 lion、dragon、ninja、iceman、wolf 的顺序制造武士。
*/
class Headquarter;
class Warrior
{
private:
Headquarter * pHeadquarter;
int kindNo; //武士的种类编号 0 dragon 1 ninja 2 iceman 3 lion 4 wolf
int weaponNum;
int weaponNo[2];
int no;
double morale; //士气
int loyalty; //忠诚度
public:
static string names[WARRIOR_NUM];
static string weapons[WEAPON_NUM];
static int initialLifeValue [WARRIOR_NUM];
Warrior( Headquarter * p,int no_,int kindNo_ );
void PrintResult(int nTime);
void PrintInfomation();
};
class Headquarter
{
private:
int totalLifeValue;
bool stopped;
int totalWarriorNum;
int color;
int curMakingSeqIdx; //当前要制造的武士是制造序列中的第几个
int warriorNum[WARRIOR_NUM]; //存放每种武士的数量
Warrior * pWarriors[1000];
public:
friend class Warrior;
static int makingSeq[2][WARRIOR_NUM]; //武士的制作顺序序列
void Init(int color_, int lv);
~Headquarter () ;
int Produce(int nTime);
string GetColor();
};
Warrior::Warrior( Headquarter * p,int no_,int kindNo_ ) {
no = no_;
kindNo = kindNo_;
pHeadquarter = p;
if( kindNo != 3 && kindNo != 4){
if(kindNo == 1) weaponNum = 2;
else weaponNum = 1;
}
else weaponNum = 0;
for( int i = 0;i < weaponNum;i++ ) weaponNo[i] = (no + i) % 3;
if( kindNo == 0 ) morale = (double)p->totalLifeValue/initialLifeValue[0];
else morale = 0;
if( kindNo == 3 ) loyalty = p->totalLifeValue;
else loyalty = 0;
}
void Warrior::PrintResult(int nTime)
{
string color = pHeadquarter->GetColor();
printf("%03d %s %s %d born with strength %d,%d %s in %s headquarter\n" ,
nTime, color.c_str(), names[kindNo].c_str(), no, initialLifeValue[kindNo],
pHeadquarter->warriorNum[kindNo],names[kindNo].c_str(),color.c_str());
}
void Warrior::PrintInfomation()
{
if(kindNo == 0) printf("It has a %s,and it's morale is %.2f\n",weapons[weaponNo[0]].c_str(),morale);
if(kindNo == 1) printf("It has a %s and a %s\n",weapons[weaponNo[0]].c_str(),weapons[weaponNo[1]].c_str());
if(kindNo == 2) printf("It has a %s\n",weapons[weaponNo[0]].c_str());
if(kindNo == 3) printf("It's loyalty is %d\n",loyalty);
}
void Headquarter::Init(int color_, int lv)
{
color = color_;
totalLifeValue = lv;
totalWarriorNum = 0;
stopped = false;
curMakingSeqIdx = 0;
for( int i = 0;i < WARRIOR_NUM;i++ )
warriorNum[i] = 0;
}
Headquarter::~Headquarter () {
for( int i = 0;i < totalWarriorNum;i++ )
delete pWarriors[i];
}
int Headquarter::Produce(int nTime)
{
if( stopped )
return 0;
int searchingTimes = 0;
while( Warrior::initialLifeValue[makingSeq[color][curMakingSeqIdx]] > totalLifeValue &&
searchingTimes < WARRIOR_NUM ) {
curMakingSeqIdx = ( curMakingSeqIdx + 1 ) % WARRIOR_NUM;
searchingTimes++;
}
int kindNo = makingSeq[color][curMakingSeqIdx];
if( Warrior::initialLifeValue[kindNo] > totalLifeValue ) {
stopped = true;
if( color == 0)
printf("%03d red headquarter stops making warriors\n",nTime);
else
printf("%03d blue headquarter stops making warriors\n",nTime);
return 0;
}
//制作士兵:
totalLifeValue -= Warrior::initialLifeValue[kindNo];
curMakingSeqIdx = ( curMakingSeqIdx + 1 ) % WARRIOR_NUM;
pWarriors[totalWarriorNum] = new Warrior( this,totalWarriorNum+1,kindNo);
warriorNum[kindNo]++;
pWarriors[totalWarriorNum]->PrintResult(nTime);
pWarriors[totalWarriorNum]->PrintInfomation();
totalWarriorNum++;
return 1;
}
string Headquarter::GetColor()
{
if( color == 0)
return "red";
else
return "blue";
}
string Warrior::names[WARRIOR_NUM] = { "dragon","ninja","iceman","lion","wolf" };
string Warrior::weapons[WEAPON_NUM] = { "sword","bomb","arrow" };
int Warrior::initialLifeValue [WARRIOR_NUM];
int Headquarter::makingSeq[2][WARRIOR_NUM] = { { 2,3,4,1,0 },{3,0,1,2,4} }; //两个司令部武士的制作顺序序列
int main()
{
int t;
int m;
Headquarter RedHead,BlueHead;
scanf("%d",&t);
int nCaseNo = 1;
while ( t-- ) {
printf("Case:%d\n",nCaseNo++);
scanf("%d",&m);
for( int i = 0;i < WARRIOR_NUM;i++ )
scanf("%d", & Warrior::initialLifeValue[i]);
RedHead.Init(0,m);
BlueHead.Init(1,m);
int nTime = 0;
while(true) {
int tmp1 = RedHead.Produce(nTime);
int tmp2 = BlueHead.Produce(nTime);
if( tmp1 == 0 && tmp2 == 0)
break;
nTime++;
}
}
return 0;
}