魔兽世界之二最终借鉴版

#include <iostream>
#include <string>
#include <vector>
#include <cstring>
using namespace std;
const int WARRIOR_NUM = 5;//属性用_隔开,且全局已知的全用大写
const int WEAPON_KIND = 3;//常量定义时就要初始化
//一般自己写的类开头都大写
int MIN = 0;
class Headquarter;
class Warrior {
private:
    int id;//它的编号比它的数目更加的有用
    int value;
    int loyalty;
    double morale;
    vector<int> weaponidx;
    Headquarter* pheadquarter;
    string name;
    int kindid;
public:
    //
    static const string names[WARRIOR_NUM];//静态常量的申明,static作为修饰语放最前面
    static const string weapons[WEAPON_KIND];
    static int inivalues[WARRIOR_NUM];
    //静态的是所有武士都有的(名字,虽然会不同),武器;,还有这里仅仅。
    Warrior(int id_, Headquarter* p,int kindno);//自己写的函数一般字母开头都用大写。
    void Print(int ntime);
    void PrintProperty();
};

//000 red iceman 1 born with strength 5,1 iceman in red headquarter
class Headquarter {
private:
    Warrior* pwarrior[1000] = {0};
    int totalvalues;
    int warrior_num[5] = {0};
    int totalnumber;
    int color;
    int stop;
public:
    friend class Warrior;//友元后面跟着的就是声明。
    static const int warriorseq[2][WARRIOR_NUM];
    static const string colorsname[2];
    int Born(int ntime);
    ~Headquarter();
    Headquarter(int life,int color_);
};
Warrior::Warrior(int id_, Headquarter* p, int kindno) :id(id_), pheadquarter(p), name(Warrior::names[kindno]), value(Warrior::inivalues[kindno]), kindid(kindno) {
    loyalty = 0;morale = 0;//可能之后忘了,先初始化
    //p->totalnumber++;
    if (name == "dragon") { morale = (double)(p->totalvalues) / value;weaponidx.push_back(id % 3);}//不允许指针指向不完整的类类型,上面是headquater,但是下面是headquarter.要改从最上面向下改,并并检查有没有改完全。
    else if (name == "ninja") { weaponidx.push_back(id % 3);weaponidx.push_back((id + 1) % 3); }
    else if (name == "iceman") { weaponidx.push_back(id % 3); }
    else if (name == "lion") { loyalty = p->totalvalues; }
}
void Warrior::Print(int ntime) {
    const char* pcolor = Headquarter::colorsname[pheadquarter->color].c_str();
    printf("00%d %s %s %d born with strenrth %d,%d %s in %s headquarter\n", ntime, pcolor, name.c_str(), id, value, pheadquarter->warrior_num[kindid], name.c_str(),pcolor);
}
void Warrior::PrintProperty() {
    if (name == "dragon") { printf("It has a %s,and it's morale is %.2f\n", Warrior::weapons[weaponidx[0]].c_str(), morale); }//morale是浮点数,不是整数不能用%d.
    //It has a arrow,and it's morale is 3.67
    else if (name == "ninja") { printf("It has a %s and a %s\n", Warrior::weapons[weaponidx[0]].c_str(), Warrior::weapons[weaponidx[1]].c_str()); }
    else if (name == "iceman") { printf("It has a %s\n", Warrior::weapons[weaponidx[0]].c_str()); }
    else if (name == "lion") { printf("It's loyalty is %d\n", loyalty); }
}
Headquarter::Headquarter(int life, int color_):totalvalues(life),totalnumber(0),color(color_),stop(0) {}
Headquarter::~Headquarter() { delete[] pwarrior; }//这里可以删除空的???
int Headquarter::Born(int ntime) {
    if (stop)
        return 0;
    else {
        if (MIN > totalvalues) { stop = 1;
        printf("00%d %s headquarter stops making warriors\n",ntime,Headquarter::colorsname[color].c_str());
        return 0; }
        else {
            int curmkingseq = ntime;
            int kindno = 0;
            do {
                kindno = warriorseq[color][curmkingseq];
                curmkingseq = (curmkingseq + 1) % 5;
            } while (Warrior::inivalues[kindno] > totalvalues);
            totalvalues -= Warrior::inivalues[kindno];
            //cout << kindno << endl;
            pwarrior[totalnumber]= new Warrior(totalnumber+1,this,kindno);//没有与参数列表匹配的实例??what
            warrior_num[kindno]++;
            pwarrior[totalnumber]->Print(ntime);
            pwarrior[totalnumber]->PrintProperty();
            totalnumber++;
            return 1;
        }
    }
}
const string Warrior::names[WARRIOR_NUM] = { "dragon","ninja","iceman","lion","wolf" };//静态常量的定义
const string Warrior::weapons[WEAPON_KIND] = { "sword","bomb","arrow" };
int Warrior::inivalues[WARRIOR_NUM];//定义时可以不初始化
const int Headquarter::warriorseq[2][WARRIOR_NUM] = { {2, 3, 4, 1, 0}, {3, 0, 1, 2, 4} };
const string Headquarter::colorsname[2] = { "red","blue" };
//dragon 、ninja、iceman、lion、wolf
//sword, bomb,和arrow
int main()
{
    int t = 0;
    cin >> t;
    for (int i = 0;i < t;++i) {
        cout << "Case:" << i + 1<<endl;
        int life;
        cin >> life;Headquarter* rp = new Headquarter(life, 0), * bp = new Headquarter(life, 1);
        for (int j = 0;j < WARRIOR_NUM;++j) { cin >> Warrior::inivalues[j];}
        MIN = Warrior::inivalues[0];
        for (int j = 1;j < WARRIOR_NUM;++j) if (MIN > Warrior::inivalues[j]) MIN = Warrior::inivalues[j];
        int ntime = 0;
        while (1) {
            int rtemp = rp->Born(ntime);
            int btemp = bp->Born(ntime);
            if (rtemp == 0 && btemp == 0)
                break;
            else ntime++;
        }
    }
}
  1. 使用了未定义的类型Headquarter.前向声明前向声明只是为了在定义之前声明之后的作用域中使用它来声明它的对象或指针,但不能访问它的对象的成员,所以有访问Headquarter里元素的函数都写在类定义的之后。(把两个类都定义后,再写各自的函数)
  2. image.png

这是把函数使用fun()写成fun的后果。

  1. printf()里面format没有写全就会出问题

4.static int curmkingseq = 0;//这里可以做一个实验,是一个类一个还是所有类共享???------>是所有类共享,red 和 blue不能共享一个curmkingseq。

评论

暂无评论