cousera of pku:魔兽世界:装备

这是魔兽世界系列的第二个程序,都是用c++编写的.

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int Liceman = 0, Llion = 0, Lwolf = 0, Lninja = 0, Ldragon = 0;//记录每个战士的生命值
int t = 0;//记录时间
int flag1 = 0, flag2 = 0;//标记red和blue司令部是否生产完
char *p1 = "red";//记录red数据
char *p2 = "blue";//记录blue数据
string weapon[3] = { "sword","bomb","arrow" };
class army {
public:
const char *color;//用color指向字符串red和blue
bool have_produce;//记录这次时间是否已经生产
int M, ndragon, nninja, niceman, nlion, nwolf;//M表示司令部总的生命元,ndragon,nninja,niceman,nlion,nwolf分别记录已生产的某类战士数量
int order, row, cl;//order记录生产的战士是第几个即编号,row记录这一次要从第几类战士开始尝试着生产,用cl标定这次的生产属于哪个司令部
void produce_iceman();
void produce_lion();
void produce_wolf();
void produce_ninja();
void produce_dragon();
//用构造函数初始化army的对象
army(int x) {
M = x;
ndragon = nninja = niceman = nlion = nwolf = 0;
order = 0;
row = 1;
bool have_produce = 0;
}
//记录生产战士的过程,只有在M大于任意一个战士的生命元才执行if,否者停止生产。执行if时,要判断是哪个司令部生产和是否已经生产完了。
//default出现了一大串程序是为了保证,M与所有种类的战士的生命元都比较过了,防止前面的没比较。
//elseif 只有在同个军队和标记还没有生产完才执行,这是为了使某个军队生产完的输出只有一次。
void produce_soldier(const char *a) {
if (M >= Ldragon || M >= Lninja || M >= Liceman || M >= Llion || M >= Lwolf) {
if (*a == *p1&&flag1 == 0) {
cl = 0;
switch (row) {
case 1: produce_iceman();
case 2: produce_lion();
case 3: produce_wolf();
case 4: produce_ninja();
case 5: produce_dragon();
default:if (row >= 1) {
produce_iceman();
if (row>2)
produce_lion();
if (row>3)
produce_wolf();
if (row>4)
produce_ninja();
have_produce = 0;
}
}
}
else
{
cl = 1;
switch (row) {
case 1: produce_lion();
case 2: produce_dragon();
case 3: produce_ninja();
case 4: produce_iceman();
case 5: produce_wolf();
default: if (row >= 1) {
produce_lion();
if (row>2)
produce_dragon();
if (row>3)
produce_ninja();
if (row>4)
produce_iceman();
have_produce = 0;
}
}
}
}
else {
if ((flag1 == 0 && *a == *p1) || (flag2 == 0 && *a == *p2)) {
cout << setw(3) << setfill('0') << t << " ";
cout << a << " headquarter stops making warriors" << endl;
}


if (*a == *p1) {
flag1 = 1;
}
if (*a == *p2) {
flag2 = 1;
}
}
}


};
//执行了某类战士的生产,执行if语句时,判断M与生产的战士的生命元大小和该次生产是否生产过。
//else if执行时,判断该次生产是否生产过,和M是否比战士的生命元小。这个语句是为了记录即使该条语句没有执行,也要记录执行到了哪一句。
void army::produce_iceman() {
if (M >= Liceman&&have_produce == 0) {
string s;
M = M - Liceman;
niceman++;
order++;
row++;
s = weapon[order % 3];
if (cl == 0)
color = p1;
else if (cl == 1)
color = p2;
cout << setw(3) << setfill('0') << t << " ";
cout << color << " iceman " << order << " born with strength " << Liceman << "," << niceman << " iceman in " << color << " headquarter" << endl;
cout << "It has a " << s << endl;
have_produce = 1;
if (row > 5)
row = row - 5;
}
else if (have_produce == 0 && M < Liceman)
row++;
}
void army::produce_lion() {
int devo_value;
if (M >= Llion&&have_produce == 0) {
M = M - Llion;
nlion++;
order++;
row++;
devo_value = M;
if (cl == 0)
color = p1;
else if (cl == 1)
color = p2;
cout << setw(3) << setfill('0') << t << " ";
cout << color << " lion " << order << " born with strength " << Llion << "," << nlion << " lion in " << color << " headquarter" << endl;
cout << "It's loyalty is " << devo_value << endl;
have_produce = 1;
if (row > 5)
row = row - 5;
}
else if (have_produce == 0 && M <Llion)
row++;
}
void army::produce_wolf() {
if (M >= Lwolf&&have_produce == 0) {
M = M - Lwolf;
nwolf++;
order++;
row++;
if (cl == 0)
color = p1;
else if (cl == 1)
color = p2;
cout << setw(3) << setfill('0') << t << " ";
cout << color << " wolf " << order << " born with strength " << Lwolf << "," << nwolf << " wolf in " << color << " headquarter" << endl;
have_produce = 1;
if (row > 5)
row = row - 5;
}
else if (have_produce == 0 && M < Lwolf)
row++;
}
void army::produce_ninja() {
string s1,s2;
if (M >= Lninja&&have_produce == 0) {
M = M - Lninja;
nninja++;
order++;
row++;
s1 = weapon[order % 3];
s2 = weapon[(order + 1) % 3];
if (cl == 0)
color = p1;
else if (cl == 1)
color = p2;
cout << setw(3) << setfill('0') << t << " ";
cout << color << " ninja " << order << " born with strength " << Lninja << "," << nninja << " ninja in " << color << " headquarter" << endl;
cout << "It has a " << s1 << " and a " << s2 << endl;
have_produce = 1;
if (row > 5)
row = row - 5;
}
else if (have_produce == 0 && M < Lninja)
row++;
}
void army::produce_dragon() {
string s;
double brave;
if (M >= Ldragon&&have_produce == 0) {
M = M - Ldragon;
ndragon++;
order++;
row++;
brave = double(M) / double(Ldragon);
s = weapon[order % 3];
if (cl == 0)
color = p1;
else if (cl == 1)
color = p2;
cout << setw(3) << setfill('0') << t << " ";
cout << color << " dragon " << order << " born with strength " << Ldragon << "," << ndragon << " dragon in " << color << " headquarter" << endl;
cout << "It has a " << s << ",and it's morale is ";
cout << setprecision(2) << fixed << brave << endl;
have_produce = 1;
if (row > 5)
row = row - 5;
}
else if (have_produce == 0 && M < Ldragon)
row++;
}




int main() {
int n, M, a[5], j;
cin >> n;
for (j = 1; j <= n; j++) {
cin >> M;
army *pc1 = new army(M);
army *pc2 = new army(M);
for (int i = 0; i < 5; i++)
cin >> a[i];
Liceman = a[2];
Llion = a[3];
Lwolf = a[4];
Lninja = a[1];
Ldragon = a[0];
cout << "Case:" << j << endl;
while (true) {
pc1->produce_soldier(p1);
pc2->produce_soldier(p2);
t++;
if (flag1 == 1 && flag2 == 1)
break;
}
delete pc1;
delete pc2;
flag1 = 0;
flag2 = 0;
t = 0;
}


return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值