欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

魔兽世界:装备

程序员文章站 2024-03-17 19:42:46
...

//在上次编写的基础上自己编写

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<string>
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;
}

注:
考察知识点:
继承和派生
难点:
添加一个CWeapon类
派生类如何编写,如何初始化(学会利用基类的构造函数
public继承,将派生类赋值给父类指针
使用switch语句时,不能再case中定义变量,否则出现
error: jump to case label编译错误

上一篇: 取下界的二分查找

下一篇: