魔兽世界:备战

//主要是参考老师的代码

#include 
#include
#include
#include
using namespace std;

#define WARRIOR_NUM 5

class CHeadquarter;
class CWarrior
{
private:
    CHeadquarter* pHeadquarter;
    int kindNo;
    int no;

public:
    static string names[WARRIOR_NUM];
    //static char* names[WARRIOR_NUM];
    static int initialLifeValue[WARRIOR_NUM];
    CWarrior(CHeadquarter* pHeadquarter_,int kindNo_,int no_);
    void printResult(int time);
};
class CHeadquarter
{
private:
    int color;
    int totalLifeValue;
    int totalWarriorNum;
    bool isStop;

    int curMakingSeqIdx;
    int warriorNum[WARRIOR_NUM];
    CWarrior* pWarriors[1000];
public:
    friend class CWarrior;
    static int makingSeq[2][WARRIOR_NUM];
    void init(int color_,int totalLifeValue_);
    ~CHeadquarter();
    int produceWarrior(int time);
    void getColor(char *headquarterColor);
};

CWarrior::CWarrior(CHeadquarter* pHeadquarter_,int kindNo_,int no_):pHeadquarter(pHeadquarter_),kindNo(kindNo_),no(no_){}
void CWarrior::printResult(int time)
{
    char color[20];
    pHeadquarter->getColor(color);
    printf("%03d %s %s %d born with strength %d,%d %s in %s headquarter\n",time,color,names[kindNo].c_str(),no,initialLifeValue[kindNo],
		pHeadquarter->warriorNum[kindNo],names[kindNo].c_str(),color);
}
void CHeadquarter::init(int color_,int totalLifeValue_)
{
    color = color_;
    totalLifeValue = totalLifeValue_;
    totalWarriorNum = 0;
    isStop = false;

    curMakingSeqIdx = 0;
    for(int i = 0;i<WARRIOR_NUM;i++)
    {
        warriorNum[i] = 0;
    }
}
CHeadquarter::~CHeadquarter()
{
    for(int i = 0;i<totalWarriorNum;i++)
    {
        delete pWarriors[i];
    }
}

int CHeadquarter::produceWarrior(int time)
{
    if(isStop)
        return 0;
    int searchTimes = 0;
    while(CWarrior::initialLifeValue[makingSeq[color][curMakingSeqIdx]] > totalLifeValue && searchTimes<WARRIOR_NUM)
    {
        curMakingSeqIdx = (curMakingSeqIdx + 1)%WARRIOR_NUM;
        searchTimes++;
    }

    int kindNo = makingSeq[color][curMakingSeqIdx];//*****
    if(CWarrior::initialLifeValue[kindNo]>totalLifeValue)
    {
        isStop = true;
        if(color == 0)
			printf("%03d red headquarter stops making warriors\n",time);
		else
			printf("%03d blue headquarter stops making warriors\n",time);

			return 0;
    }

    curMakingSeqIdx = (curMakingSeqIdx + 1)%WARRIOR_NUM;
    totalLifeValue -= CWarrior::initialLifeValue[kindNo];
    pWarriors[totalWarriorNum] = new CWarrior(this,kindNo,totalWarriorNum+1);
    warriorNum[kindNo]++;
    pWarriors[totalWarriorNum]->printResult(time);
    totalWarriorNum++;

    return 1;
}

void CHeadquarter::getColor(char* headquarterColor)
{
    if(color == 0)
        strcpy(headquarterColor,"red");
    else
        strcpy(headquarterColor,"blue");
}

string CWarrior::names[WARRIOR_NUM] = {"dragon","ninja","iceman","lion","wolf"};
//char* CWarrior::names[WARRIOR_NUM] = {"dragon","ninja","iceman","lion","wolf"};
int CWarrior::initialLifeValue[WARRIOR_NUM];
int CHeadquarter::makingSeq[2][WARRIOR_NUM] = { {2,3,4,1,0} , {3,0,1,2,4} };

int main()
{
    freopen("data.txt","r",stdin);
    int test;
    int caseNo = 1;
    cin>>test;

    while(test--)
    {
        CHeadquarter redHead,blueHead;
        int M;
        int time = 0;

        cin>>M;
        printf("Case:%d\n",caseNo++);

        for(int i = 0;i<WARRIOR_NUM;i++)
        {
            cin>>CWarrior::initialLifeValue[i];
        }

        redHead.init(0,M);
        blueHead.init(1,M);
        int temp1,temp2;
        while(true)
        {
            temp1 = redHead.produceWarrior(time);
            temp2 = blueHead.produceWarrior(time);
            if(temp1 == 0 && temp2 == 0)
                break;
            time++;
        }
    }
    return 0;
}

注:
考察知识点
(类和对象)
难点
类怎样设计
类之间是什么关系
和类相关的全局变量使用static变量
如何循环生产战士,在什么地方编写生产战士的代码

你可能感兴趣的:(#,C/C++语法问题,#,C++项目:魔兽世界)