魔兽世界之二武器

#include <iostream>
#include <iomanip>
#include <string>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

int dHP, iHP, lHP, nHP, wHP;	// 全局魔兽初始生命

const int MAXN = 1000 + 10;
class Headquater;
class Iceman;
class Warrior;
class Ninja;
class Wolf;
class Lion;
class Dragon;
class Weapon;

/* class: 游戏钟表 */
class Clock
{
public:
	Clock(){ hour = 0; }
	void tick()
	{
		hour++;
	}
	void printHour()
	{
		cout << setfill('0') << setw(3) << hour;
	}
	void reset()
	{
		hour = 0;
	}
private:
	int hour;
}CLOCK;							// 全局游戏时钟

/* class:司令部 */

class Headquater
{
public:
	Headquater(string color_, int M)
	{
		color = color_;
		elements = M;
		nWarrior = nNinja = nIceman = nWolf = nLion = nDragon = 0;
		stop_producing = false;
		produce_peek = 0;			 
	}
	int elements;
	string color;
	bool stop_producing;
	void produceWarrior();
	Iceman* iceman[MAXN];
	Ninja* ninja[MAXN];
	Wolf* wolf[MAXN];
	Lion* lion[MAXN];
	Dragon* dragon[MAXN];
private:
	int nNinja;
	int nIceman;
	int nWolf;
	int nLion;
	int nDragon;
	bool producePeek(int peek);				// 生产武士帧为peek的武士
	bool produceIceman();					// 生产一个Iceman,成功返回true,下同
	bool produceNinja();					// 对五个生产函数进行一点说明,首先,这五个函数完全可以由一个
	bool produceDragon();					// 接受参数的函数代替,在此没有节省这点代码量,主要是因为:
	bool produceLion();						// 1.这个游戏到后期怪兽的异质性很强,共性很弱,函数总会拆离
	bool produceWolf();						// 2.在一定情况下可方便地为其他函数提供好的接口,这一点在游戏初期不明显
	int produce_peek;						// 记录当前应该生产武士的“指针”(武士帧),构造函数中初始为0
	int nWarrior;
};
/* 武器类定义开始 */
class Weapon
{
public:
	Weapon(string type_):type(type_){};
	Weapon();
	static Weapon* produceWeapon(int weaponId)
	{
		switch(weaponId)
		{
		case 0:return (new Weapon("sword")); break;
		case 1:return (new Weapon("bomb")); break;
		case 2:return (new Weapon("arrow")); break;
		default: break;
		}
	};
	string type;
};
/* 武器类定义结束 */

/* 武士类定义开始 */
/*
* 以 Warrior 类为基类
* 派生5种武士的类型
* 在此仍然在基地处维护5个不同兵种的数组
* 日后可以考虑仅维护一个warrior,而用虚函数,多态的方式使程序更加“舒畅”
* 关于储存形式,现在为定长顺序数组,以后可考虑用 c++ STL 的容器
*/
class Warrior
{
public:
	Warrior(int id_, int hp_, string type_, Headquater* headquater_):
	  id(id_), hp(hp_), type(type_), headquater(headquater_){};
	string type;
	int id;
	int hp;
	Headquater* headquater;
};

class Iceman: public Warrior
{
public:
	Iceman(int id_, int hp_, Headquater* headquater_):Warrior(id_, hp_, "iceman", headquater_)
	{
		weapon = Weapon::produceWeapon(id % 3);
	};
	Weapon* weapon;
};

class Dragon: public Warrior
{
public:
	Dragon(int id_, int hp_, Headquater* headquater_):Warrior(id_, hp_, "dragon", headquater_)
	  {
		  morale = (double)headquater->elements / dHP;
		  weapon = Weapon::produceWeapon(id % 3);
	  }
	double morale;
	Weapon* weapon;
};

class Ninja: public Warrior
{
public:
	Ninja(int id_, int hp_, Headquater* headquater_):Warrior(id_, hp_, "ninja", headquater_)
	{
		lWeapon = Weapon::produceWeapon(id % 3);
		rWeapon = Weapon::produceWeapon((id + 1) % 3);
	};
	Weapon* lWeapon;
	Weapon* rWeapon;
};

class Wolf: public Warrior
{
public:
	Wolf(int id_, int hp_, Headquater* headquater_):Warrior(id_, hp_, "wolf", headquater_){};
};

class Lion: public Warrior
{
public:
	Lion(int id_, int hp_, Headquater* headquater_):Warrior(id_, hp_, "lion", headquater_)
	{
		loyalty = headquater->elements;
	};
	int loyalty;
};
/* 武士类定义结束 */

/* 司令部类实现开始 */
/* produceWarrior的实现 */
/* 
* 此函数为重要的公用接口,循环生产武士,并判断不能生产的状况
* 主要以私有变量produce_peek指向当前应该生产的武士,而由私有函数producePeek()
* 按照不同的司令部以内置的序列生产武士
*/
void Headquater::produceWarrior()
{
	bool has_produced = false;				// 标记是否生产武士
	for(int i = produce_peek; i < produce_peek + 5; i++) 	
	{										// 循环生产武士
		has_produced = producePeek(i % 5);	
		if(has_produced)				
		{
			produce_peek = i + 1;
			break;
		}
	}
	if(!has_produced)						// 断定无法生产武士,司令部停产,输出停产信息
	{
		stop_producing = true;
		CLOCK.printHour();
		cout << " " << color << " headquarter stops making warriors" << endl;
	}
}

bool Headquater::producePeek(int peek)
{
	if(color == "red")						// 在函数执行中书写武士生产顺序,以color区别不同的顺序
	{
		switch(peek)
		{
			case 0: return produceIceman();
			case 1: return produceLion();
			case 2: return produceWolf();
			case 3: return produceNinja();
			case 4: return produceDragon();
			default: return false;
		}
	}
	if(color == "blue")
	{
		switch(peek)
		{
			case 0: return produceLion();
			case 1: return produceDragon();
			case 2: return produceNinja();
			case 3: return produceIceman();
			case 4: return produceWolf();
			default: return false;
		}
	}
}

bool Headquater::produceIceman()
{
	if(elements >= iHP)
	{
		nWarrior++;
		elements -= iHP;
		iceman[nIceman++] = new Iceman(nWarrior, iHP, this);
		CLOCK.printHour();
		cout << " " << color << " iceman "  << nWarrior 
		<<  " born with strength " << iHP << "," << nIceman << " iceman in " << color << " headquarter" << endl;
		cout << "It has a " << iceman[nIceman - 1]->weapon->type << endl;
		return true;
	}
	else return false;
}
bool Headquater::produceWolf()
{
	if(elements >= wHP)
	{
		nWarrior++;
		nWolf++;
		elements -= wHP;
		CLOCK.printHour();
		cout << " " << color << " wolf "  << nWarrior 
		<<  " born with strength " << wHP << "," << nWolf << " wolf in " << color << " headquarter" << endl;
		return true;
	}
	else return false;
}
bool Headquater::produceLion()
{
	if(elements >= lHP)
	{
		nWarrior++;
		elements -= lHP;
		lion[nLion++] = new Lion(nWarrior, lHP, this);
		CLOCK.printHour();
		cout << " " << color << " lion "  << nWarrior 
		<<  " born with strength " << lHP << "," << nLion << " lion in " << color << " headquarter" << endl;
		cout << "It's loyalty is " << lion[nLion - 1]->loyalty << endl;
		return true;
	}
	else return false;
}
bool Headquater::produceNinja()
{
	if(elements >= nHP)
	{
		elements -= nHP;
		nWarrior++;
		ninja[nNinja++] = new Ninja(nWarrior, nHP, this);
		CLOCK.printHour();
		cout << " " << color << " ninja "  << nWarrior
		<<  " born with strength " << nHP << "," << nNinja << " ninja in " << color << " headquarter" << endl;
		cout << "It has a "<< ninja[nNinja - 1]->lWeapon->type 
			<< " and a " << ninja[nNinja - 1]->rWeapon->type << endl;
		return true;
	}
	else return false;
}
bool Headquater::produceDragon()
{
	if(elements >= dHP)
	{
		elements -= dHP;
		nWarrior++;
		dragon[nDragon++] = new Dragon(nWarrior, dHP, this);
		CLOCK.printHour();
		cout << " " << color << " dragon "  << nWarrior 
		<<  " born with strength " << dHP << "," << nDragon << " dragon in " << color << " headquarter" << endl;
		cout << "It has a " << dragon[nDragon - 1]->weapon->type << ",and it's morale is " << fixed << setprecision(2) << dragon[nDragon - 1]->morale << endl;
		return true;
	}
	else return false;
}
/* 司令部类实现结束 */

int main()
{
	int nTest;
	cin >> nTest;
	for(int test = 1; test <= nTest; test++)
	{
		CLOCK.reset();
		int M;
		cin >> M;
		cin >> dHP >> nHP >> iHP >> lHP >> wHP;
		Headquater red("red", M);
		Headquater blue("blue", M);
		cout << "Case:" << test << endl;
		/* 以上,初始化战场模型,数据 */

		while(true)
		{
			if(red.stop_producing && blue.stop_producing)	// 蓝军红军停产,模拟结束
			{
				break;
			}
			if(!red.stop_producing)							
			{
				red.produceWarrior();
			}
			if(!blue.stop_producing)
			{
				blue.produceWarrior();
			}
			CLOCK.tick();									// 游戏钟表跳
		}
	}
	return 0;
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值