这里有两个类,武士类和阵营类,建议从main函数开始阅读。
#include <iostream>
#include <cstring>
using namespace std;
const int WARRIOR_NUM = 5;
class Cheadquarter;
class CWarrior{
private:
Cheadquarter * pheadquarter;//武士所属阵营的指针
int kindNo;//武士编号的种类 0 dragon 1 ninja 2 iceman 3 lion 4 wolf
int nNo;//所属阵营的第几个武士
public:
static const char *names[WARRIOR_NUM];
static int InitialLifeValue[WARRIOR_NUM];
CWarrior(Cheadquarter *p, int kindNo_, int nNo_): pheadquarter(p), kindNo(kindNo_), nNo(nNo_){
} ;
void PrintResult(int nTime);
};
const char *name[WARRIOR_NUM] = {"dragon", "ninja", "iceman", "lion", "wolf"};
int CWarrior::InitialLifeValue[WARRIOR_NUM];
class Cheadquarter{
private:
int totalLifeValue;//总生命元
bool bStopped;//是否能继续制造武士
int totalWarriorNum;//该阵营的武士总数
int color;//该阵营的颜色
int curMakingSeqIdx;// 当前要制造的武士是制造序列中的第几个
int warriorNum[WARRIOR_NUM];//存放每种武士的数量
CWarrior *pWarriors[1000];//数组存放该阵营的所有武士
public:
friend class CWarrior;
static int makingSeq[2][WARRIOR_NUM];//武士的制作顺序序列
void Init(int color_, int lv)
{
totalLifeValue = lv;
bStopped = false;
totalWarriorNum = 0;
color = color_;
curMakingSeqIdx = 0;
for (int i = 0; i < WARRIOR_NUM; i ++) warriorNum[i] = 0;
}
~Cheadquarter()
{
for (int i = 0; i < totalWarriorNum; i ++){
delete pWarriors[i];
}
}
int Produce(int nTime){
if (bStopped) return 0;
int i;//找到该阵营能制造的武士
for (i = 1; CWarrior::InitialLifeValue[makingSeq[color][curMakingSeqIdx]] > totalLifeValue && i <= WARRIOR_NUM; i ++)
curMakingSeqIdx = (curMakingSeqIdx + 1) % WARRIOR_NUM;
int kindNo = makingSeq[color][curMakingSeqIdx];//局部变量,可能的 将要制造的武士的编号(CWarrior::InitialLifeValue[]中的编号)
if (i ==6)
{
bStopped = true;
if (color == 0) printf("%03d red headquarter stops making warriors\n", nTime);
if (color == 1) printf("%03d blue headquarter stops making warriors\n", nTime);
return 0;
}
totalLifeValue -= CWarrior::InitialLifeValue[kindNo];
curMakingSeqIdx = (curMakingSeqIdx + 1) % WARRIOR_NUM;
pWarriors[totalWarriorNum] = new CWarrior(this, kindNo, totalWarriorNum + 1);
warriorNum[kindNo] ++;
pWarriors[totalWarriorNum]->PrintResult(nTime);
totalWarriorNum ++;
return 1;
}
};
int Cheadquarter::makingSeq[2][WARRIOR_NUM] = {{2, 3, 4, 1, 0}, {3, 0, 1, 2, 4}};
void CWarrior::PrintResult(int nTime)
{
char szColor[20];
int color = pheadquarter->color;
if (color == 0) strcpy(szColor, "red");
else strcpy(szColor, "blue");
//000 red iceman 1 born with strength 5,1 iceman in red headquarter
printf("%03d %s %s %d born with strength %d,%d %s in %s headquarter\n",
nTime, szColor, name[kindNo], nNo, InitialLifeValue[kindNo], pheadquarter->warriorNum[kindNo], name[kindNo],szColor);
}
int main()
{
int t, m;
Cheadquarter RedHead, BlueHead;
int nCaseNo = 1;
cin >> t;
while (t --)
{
cout << "Case:" << nCaseNo ++ << endl;
cin >> m;
for (int i = 0; i < WARRIOR_NUM; i ++)
cin >> CWarrior::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;
}
参考里面的官方简要版本代码:
[C++编程练习 013:魔兽世界之一:备战]
题目来源:
中国大学MOOC_程序设计与算法(三)C++面向对象程序设计_郭炜