[土狗之路]Coursera 北京大学C++ 魔兽世界:备战

这道题目中间因为学校正课耽误了很久,结果时间到了 coursera 把我的课都给锁了,哎,申请了助学金,慢慢弄吧,之前心态不好,确实错在了细节,现在debug以及代码重构了,重新把代码po上来,然后开始准备下一道题,武器了。


编程题#4:魔兽世界之一:备战

来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

注意: 总时间限制: 1000ms 内存限制: 65536kB

描述

魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部。两个司令部之间是依次排列的若干城市。

红司令部,City 1,City 2,……,City n,蓝司令部

两军的司令部都会制造武士。武士一共有 dragon 、ninja、iceman、lion、wolf 五种。每种武士都有编号、生命值、攻击力这三种属性。

双方的武士编号都是从1开始计算。红方制造出来的第n个武士,编号就是n。同样,蓝方制造出来的第n个武士,编号也是n。

武士在刚降生的时候有一个生命值。

在每个整点,双方的司令部中各有一个武士降生。

红方司令部按照iceman、lion、wolf、ninja、dragon的顺序循环制造武士。

蓝方司令部按照lion、dragon、ninja、iceman、wolf的顺序循环制造武士。

制造武士需要生命元。

制造一个初始生命值为m的武士,司令部中的生命元就要减少m个。

如果司令部中的生命元不足以制造某个按顺序应该制造的武士,那么司令部就试图制造下一个。如果所有武士都不能制造了,则司令部停止制造武士。

给定一个时间,和双方司令部的初始生命元数目,要求你将从0点0分开始到双方司令部停止制造武士为止的所有事件按顺序输出。

一共有两种事件,其对应的输出样例如下:

1) 武士降生

输出样例: 004 blue lion 5 born with strength 5,2 lion in red headquarter

表示在4点整,编号为5的蓝魔lion武士降生,它降生时生命值为5,降生后蓝魔司令部里共有2个lion武士。(为简单起见,不考虑单词的复数形式)注意,每制造出一个新的武士,都要输出此时司令部里共有多少个该种武士。

2) 司令部停止制造武士

输出样例: 010 red headquarter stops making warriors

表示在10点整,红方司令部停止制造武士

输出事件时:

首先按时间顺序输出;

同一时间发生的事件,先输出红司令部的,再输出蓝司令部的。

输入

第一行是一个整数,代表测试数据组数。

每组测试数据共两行。

第一行:一个整数M。其含义为, 每个司令部一开始都有M个生命元( 1 <= M <= 10000)。

第二行:五个整数,依次是 dragon 、ninja、iceman、lion、wolf 的初始生命值。它们都大于0小于等于10000。

输出

对每组测试数据,要求输出从0时0分开始,到双方司令部都停止制造武士为止的所有事件。

对每组测试数据,首先输出"Case:n" n是测试数据的编号,从1开始 。

接下来按恰当的顺序和格式输出所有事件。每个事件都以事件发生的时间开头,时间以小时为单位,有三位。

样例输入

 
  
1
2
3
1
20
3 4 5 6 7

样例输出

Case:1
000 red iceman 1 born with strength 5,1 iceman in red headquarter
000 blue lion 1 born with strength 6,1 lion in blue headquarter
001 red lion 2 born with strength 6,1 lion in red headquarter
001 blue dragon 2 born with strength 3,1 dragon in blue headquarter
002 red wolf 3 born with strength 7,1 wolf in red headquarter
002 blue ninja 3 born with strength 4,1 ninja in blue headquarter
003 red headquarter stops making warriors
003 blue iceman 4 born with strength 5,1 iceman in blue headquarter
004 blue headquarter stops making warriors

#include 
#include    
#include    
using namespace std;
const int redOrder[5] = { 2,3,4,1,0 };
const int blueOrder[5] = { 3,0,1,2,4 };
int HPs[5];
int minHP;
class warrior//基类    
{
public:
	string color;
	int HP;
public: warrior(string color, int i) {
	this->color = color;
	HP = HPs[i];
};
public: warrior() {};//默认构造函数,没有这个后面会派生类会报错,不知道为什么!
};
class dragon :public warrior//派生类    
{
public:    dragon(string color) {
	warrior(color, 0);
};
};
class ninja :public warrior
{
public: ninja(string color) {
	warrior(color, 1);
};
};
class iceman :public warrior
{
public: iceman(string color) {
	warrior(color, 2);
};
};
class lion :public warrior
{
public: lion(string color) {
	warrior(color, 3);
};
};
class wolf :public warrior
{
public: wolf(string color) {
	warrior(color, 4);
};
};
class headOffice
{
private:
	int power;
	string color;
	int numberOfWarrior;//总兵数    
	bool carryOn;//是否继续造兵    
	int each[5];//每种兵的数量    
	int current;//当前轮换造兵的顺序    
	int select;//当前要造的兵    
public:
	headOffice(int power, string color)
	{
		this->color = color; this->power = power; carryOn = true; numberOfWarrior = 0; current = 0;
		each[0] = 0; each[1] = 0; each[2] = 0; each[3] = 0; each[4] = 0;
	};
	bool getCarryOn() {
		return carryOn;
	}
	bool create(string color) {
		cout << color;
		int HPOrder[5];
		for (int i = 0; i <= 4; i++) {
			if (color == "red")
				HPOrder[i] = HPs[redOrder[i]];
			if (color == "blue")
				HPOrder[i] = HPs[blueOrder[i]];
		};
		if (power >= minHP == false) {
			cout << " headquarter stops making warriors" << endl;
			carryOn = false;
			return false;
		}
		while (power >= minHP) {
			if (power >= HPOrder[current])
				break;
			if (current == 4)
				current = 0;
			else
				current++;
		}
		//根据不同颜色,调取不同颜色的造兵顺序
		if (color == "red")
			select = redOrder[current];
		if (color == "blue")
			select = blueOrder[current];
		string warriors[5] = { " dragon "," ninja "," iceman "," lion "," wolf " };//储存五种不同兵种的名字 
		switch (select) {//根据不同的兵种,进行制造    
		case 0:new dragon(color); each[0]++; power = power - HPs[0]; numberOfWarrior++;
			cout << warriors[select] << numberOfWarrior << " born with strength " << HPs[select] << "," << each[select] << warriors[select] << "in " << color << " headquarter" << endl;
			if (current == 4) current = 0;
			else
				current++; break;
		case 1:new ninja(color); each[1]++; power = power - HPs[1];  numberOfWarrior++;
			cout << warriors[select] << numberOfWarrior << " born with strength " << HPs[select] << "," << each[select] << warriors[select] << "in " << color << " headquarter" << endl;
			if (current == 4)
				current = 0;
			else
				current++;  break;
		case 2:new iceman(color); each[2]++; power = power - HPs[2];  numberOfWarrior++;
			cout << warriors[select] << numberOfWarrior << " born with strength " << HPs[select] << "," << each[select] << warriors[select] << "in " << color << " headquarter" << endl;
			if (current == 4)
				current = 0;
			else
				current++;  break;
		case 3:new lion(color); each[3]++; power = power - HPs[3];  numberOfWarrior++;
			cout << warriors[select] << numberOfWarrior << " born with strength " << HPs[select] << "," << each[select] << warriors[select] << "in " << color << " headquarter" << endl;
			if (current == 4)
				current = 0;
			else
				current++;  break;
		case 4:new wolf(color); each[4]++; power = power - HPs[4]; numberOfWarrior++;
			cout << warriors[select] << numberOfWarrior << " born with strength " << HPs[select] << "," << each[select] << warriors[select] << "in " << color << " headquarter" << endl;
			if (current == 4)
				current = 0;
			else
				current++;  break;
		}
		return true;
	}
};
int main() {
	int n, m;
	minHP = 0;
	cin >> n;
	for (int j = 0; j < n; j++) {
		cin >> m;
		for (int i = 0; i <= 4; i++) {
			cin >> HPs[i];
		}
		cout << "Case:" << j + 1 << endl;
		minHP = HPs[0];
		for (int i = 0; i <= 4; i++) {
			if (HPs[i] < minHP)
				minHP = HPs[i];
		}
		headOffice redHeadoffice(m, "red"), blueHeadoffice(m, "blue");
		int time = 0;//游戏时钟    
		while (redHeadoffice.getCarryOn() == true || blueHeadoffice.getCarryOn() == true) {
			if (redHeadoffice.getCarryOn() == true) {
				cout << setfill('0') << setw(3) << time << ' ';
				redHeadoffice.create("red");
			}
			if (blueHeadoffice.getCarryOn() == true) {
				cout << setfill('0') << setw(3) << time << ' ';
				blueHeadoffice.create("blue");
			}
			time++;//每一轮结束,时间+1
		}
	}
	return 0;
}






你可能感兴趣的:(C++相关)