今天把代码稍微简化了一下,主要改动是:

  • 武士类构造函数形参的简化,形参直接来一个Command类更加方便,但是这样做Command类就必须放在程序开头,所以就有了:
  • 类声明和成员函数的顺序有所改动,顺序是:Command类声明 -> Warrior类声明+各武士类声明 -> Command类成员函数实现 -> Warrior类+各武士类成员函数实现,个人认为这种顺序也是挺合理的

以下为代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <iostream>
#include <string>
#include <iomanip>
#include <ctime>
using namespace std;
const int WARRIOR_NUM = 5;
const int WEAPON_NUM = 2;
enum warrior{total=0, dragon, ninja, iceman, lion, wolf};
//1dragon 2ninja 3iceman 4lion 5wolf
//0sword 1bomb 2arrow
class Warrior;

class Command{
private:
string Name;
short OrderCount;//循环遍历武士顺序数组
int HP;
int WarriorNumber[WARRIOR_NUM+1];//记录总数以及每个武士的数量 ,下标为0的元素存储总数
Warrior *myWarrior[200]; //指向每一个成功生成的武士
public:
friend class Warrior; friend class Dragon; friend class Ninja;
friend class Iceman; friend class Wolf; friend class Lion;
static int time;//游戏系统时间
short Order[WARRIOR_NUM+1];//制造武士的顺序记录,即武士顺序数组
Command(string _Name, const short _Order[], const int _HP);
~Command();
bool CreateWarrior(short p[]);
void StopCreateWarrior();
};
int Command::time = -1;//静态成员变量必须进行初始化

//No.0
class Warrior{
protected:
int No;
int HP;
short WeaponHP[WEAPON_NUM+1];
string Name;
public:
friend class Command;
virtual string CreateWeapon(int No);
virtual void DeclareWarrior(Command *c, short kind);
};

//No.1
class Dragon:public Warrior{
private:
double Morale;
public:
Dragon(const int _HP, Command *c);
//这些参数从左到右依次是:武士编号,武士生命值,武士所属司令部
};

//No.2
class Ninja:public Warrior{
public:
Ninja(const int _HP, Command *c);
};

//No.3
class Iceman:public Warrior{
public:
Iceman(const int _HP, Command *c);
};

//No.4
class Lion:public Warrior{
private:
int Loyalty;
public:
Lion(const int _HP, Command *c);
};

//No.5
class Wolf:public Warrior{
public:
Wolf(const int _HP, Command *c);
};

//====================================================
Command::Command(string _Name, const short _Order[], const int _HP):Name(_Name), HP(_HP)
{
OrderCount = 1;
for(int i=0; i<=WARRIOR_NUM; i++) //这里做的工作是复制+初始化
{
Order[i] = _Order[i];
WarriorNumber[i] = 0;
}
}

Command::~Command()
{
while(WarriorNumber[total]) //内存释放很重要,别忘了
{
delete myWarrior[WarriorNumber[total]];
WarriorNumber[total]--;
}
}

bool Command::CreateWarrior(short p[])
{
if( HP<p[1] && HP<p[2] && HP<p[3] && HP<p[4] && HP<p[5] ) return 0;
while(HP < p[Order[OrderCount]])
{
OrderCount++;
if(OrderCount==WARRIOR_NUM+1) OrderCount=1;
}
short ord = Order[OrderCount];
WarriorNumber[total]++;
WarriorNumber[ord]++;
HP -= p[ord];
short No = WarriorNumber[total];
cout<<setw(3)<<setfill('0')<<time<<" ";
switch(ord)
{
case 1: myWarrior[No]=new Dragon(p[1], this); break;
case 2: myWarrior[No]=new Ninja(p[2], this); break;
case 3: myWarrior[No]=new Iceman(p[3], this); break;
case 4: myWarrior[No]=new Lion(p[4], this); break;
case 5: myWarrior[No]=new Wolf(p[5], this); break;
default: break;
}
OrderCount++;
if(OrderCount == WARRIOR_NUM+1) OrderCount=1;
return 1;
}

void Command::StopCreateWarrior()
{
cout<<setw(3)<<setfill('0')<<time<<" "<<this->Name<<" headquarter stops making warriors"<<endl;
}

//============================================
string Warrior::CreateWeapon(int _No)
{
_No = _No % 3;
switch(_No)
{
case 0: WeaponHP[0]=1; return "sword"; break;
case 1: WeaponHP[1]=1; return "bomb"; break;
case 2: WeaponHP[2]=1; return "arrow"; break;
default: break;
}
}

void Warrior::DeclareWarrior(Command *c, short kind)
{
cout<<c->Name<<" "<<this->Name<<" "<<this->No<<" born with strength "<<this->HP<<", ";
cout<<c->WarriorNumber[kind]<<" "<<this->Name<<" in "<<c->Name<<" headquarter, ";
cout<<"rest HP = "<<c->HP<<endl;
}

Dragon::Dragon(const int _HP, Command *c)
{
No=c->WarriorNumber[0]; HP=_HP; Name="dragon";
Morale = (double)c->HP/this->HP;
DeclareWarrior(c, 1);
cout<<"It has a "<<this->CreateWeapon(No)<<", and it's morale is "<<setiosflags(ios::fixed)<<setprecision(2)<<Morale<<endl;
}

Ninja::Ninja(const int _HP, Command *c)
{
No=c->WarriorNumber[0]; HP=_HP; Name="ninja";
DeclareWarrior(c, 2);
cout<<"It has a "<<this->CreateWeapon(No)<<" and a "<<this->CreateWeapon(No+1)<<endl;
}

Iceman::Iceman(const int _HP, Command *c)
{
No=c->WarriorNumber[0]; HP=_HP; Name="iceman";
DeclareWarrior(c, 3);
cout<<"It has a "<<this->CreateWeapon(No)<<endl;
}

Lion::Lion(const int _HP, Command *c)
{
No=c->WarriorNumber[0]; HP=_HP; Name="lion"; Loyalty=c->HP;
DeclareWarrior(c, 4);
cout<<"It's loyalty is "<<Loyalty<<endl;
}

Wolf::Wolf(const int _HP, Command *c)
{
No=c->WarriorNumber[0]; HP=_HP; Name="wolf";
DeclareWarrior(c, 5);
}

int main()
{
int n=1;
short redOrder[WARRIOR_NUM+1]={0, iceman, lion, wolf, ninja, dragon};
short blueOrder[WARRIOR_NUM+1]={0, lion, dragon, ninja, iceman, wolf};
short M, dm, nm, im, lm, wm;
clock_t startTime, endTime;
//cin>>n;
while(n--)
{
cin>>M;//输入司令部生命元
cin>>dm>>nm>>im>>lm>>wm;//输入每种武士的生命值
startTime = clock();
Command red("red", redOrder, M), blue("blue", blueOrder, M);
short p[WARRIOR_NUM+1] = {0, dm, nm, im, lm, wm}; //用数组存储每种武士的生命值
bool redCreate = 1, blueCreate = 1;//记录司令部是否能进行制造武士,1表示可以,0则相反
Command::time = -1;
while(redCreate || blueCreate)//开始制造武士
{
Command::time ++;//系统时间加1
//制造武士的函数既可以完成制造任务,又可以返回制造情况:返回1表示制造成功,
//而返回0则说明一个都制造不了, 同时还要使布尔变量red(blue)Create变成0
if(redCreate == 1 && red.CreateWarrior(p) == 0){redCreate = 0; red.StopCreateWarrior();}
if(blueCreate == 1 && blue.CreateWarrior(p) == 0){blueCreate = 0; blue.StopCreateWarrior();}
cout<<endl;
}
}
endTime = clock();
cout<<"time:"<<(double)(endTime - startTime) <<"ms"<<endl;
return 0;
}