魔兽世界小游戏(基于C++)

前言

这个项目的需求还是挺复杂的,作为检验c++学习成果还是挺不错的,做完这个项目,感觉自己的水平又更上一层楼了,哈哈

下面是整个项目的需求,和我写的代码原码,全部代码(不包括测试代码)大概在1000行左右。

源码、需求说明、校验数据可以在我的百度云下载
链接:https://pan.baidu.com/s/1FPYWndPqM_s1zvMmoKnKiA
提取码:p6im

整个架构、流程完全独立构思完成的,没有参考任何其它人的代码,虽然里面还有一点点小BUG,但是目前来说影响不大,如果哪位大牛能看出来问题在哪,有偿求教!

项目需求

魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部。两个司令部之间是依次排列的若干城市,城市从西向东依次编号为1,2,3 … N ( N <= 20)。红魔军的司令部算作编号为0的城市,蓝魔军的司令部算作编号为N+1的城市。司令部有生命元,用于制造武士。

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

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

武士在刚降生的时候有一个初始的生命值,生命值在战斗中会发生变化,如果生命值减少到0(生命值变为负数时应当做变为0处理),则武士死亡(消失)。

武士可以拥有武器。武器有三种,sword, bomb,和arrow,编号分别为0,1,2。

sword的攻击力是使用者当前攻击力的20%(去尾取整)。

bomb的攻击力是使用者当前攻击力的40%(去尾取整),但是也会导致使用者受到攻击,对使用者的攻击力是对敌人取整后的攻击力的1/2(去尾取整)。Bomb一旦使用就没了。

arrow的攻击力是使用者当前攻击力的30%(去尾取整)。一个arrow用两次就没了。

武士降生后就朝对方司令部走,在经过的城市如果遇到敌人(同一时刻每个城市最多只可能有1个蓝武士和一个红武士),就会发生战斗。战斗的规则是:

在奇数编号城市,红武士先发起攻击

在偶数编号城市,蓝武士先发起攻击

战斗开始前,双方先对自己的武器排好使用顺序,然后再一件一件地按顺序使用。编号小的武器,排在前面。若有多支arrow,用过的排在前面。排好序后,攻击者按此排序依次对敌人一件一件地使用武器。如果一种武器有多件,那就都要用上。每使用一件武器,被攻击者生命值要减去武器攻击力。如果任何一方生命值减为0或小于0即为死去。有一方死去,则战斗结束。

双方轮流使用武器,甲用过一件,就轮到乙用。某一方把自己所有的武器都用过一轮后,就从头开始再用一轮。如果某一方没有武器了,那就挨打直到死去或敌人武器用完。武器排序只在战斗前进行,战斗中不会重新排序。

如果双方武器都用完且都还活着,则战斗以平局结束。如果双方都死了,也算平局。

有可能由于武士自身攻击力太低,而导致武器攻击力为0。攻击力为0的武器也要使用。如果战斗中双方的生命值和武器的状态都不再发生变化,则战斗结束,算平局。

战斗的胜方获得对方手里的武器。武士手里武器总数不超过10件。缴获武器时,按照武器种类编号从小到大缴获。如果有多件arrow,优先缴获没用过的。

如果战斗开始前双方都没有武器,则战斗视为平局。如果先攻击方没有武器,则由后攻击方攻击。

不同的武士有不同的特点。

编号为n的dragon降生时即获得编号为n%3 的武器。dragon在战斗结束后,如果还没有战死,就会欢呼。

编号为n的ninjia降生时即获得编号为n%3 和(n+1)%3的武器。ninja 使用bomb不会让自己受伤。

编号为n的iceman降生时即获得编号为n%3 的武器。iceman每前进一步,生命值减少10%(减少的量要去尾取整)。

编号为n的lion降生时即获得编号为n%3 的武器。lion 有“忠诚度”这个属性,其初始值等于它降生之后其司令部剩余生命元的数目。每前进一步忠诚度就降低K。忠诚度降至0或0以下,则该lion逃离战场,永远消失。但是已经到达敌人司令部的lion不会逃跑。lion在己方司令部可能逃跑。

wolf降生时没有武器,但是在战斗开始前会抢到敌人编号最小的那种武器。如果敌人有多件这样的武器,则全部抢来。Wolf手里武器也不能超过10件。如果敌人arrow太多没法都抢来,那就先抢没用过的。如果敌人也是wolf,则不抢武器。

以下是不同时间会发生的不同事件:

在每个整点,即每个小时的第0分, 双方的司令部中各有一个武士降生。

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

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

制造武士需要生命元。

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

如果司令部中的生命元不足以制造某本该造的武士,那就从此停止制造武士。

在每个小时的第5分,该逃跑的lion就在这一时刻逃跑了。

在每个小时的第10分:所有的武士朝敌人司令部方向前进一步。即从己方司令部走到相邻城市,或从一个城市走到下一个城市。或从和敌军司令部相邻的城市到达敌军司令部。

在每个小时的第35分:在有wolf及其敌人的城市,wolf要抢夺对方的武器。

在每个小时的第40分:在有两个武士的城市,会发生战斗。

在每个小时的第50分,司令部报告它拥有的生命元数量。

在每个小时的第55分,每个武士报告其拥有的武器情况。

武士到达对方司令部后就算完成任务了,从此就呆在那里无所事事。

任何一方的司令部里若是出现了敌人,则认为该司令部已被敌人占领。

任何一方的司令部被敌人占领,则战争结束。战争结束之后就不会发生任何事情了。

给定一个时间,要求你将从0点0分开始到此时间为止的所有事件按顺序输出。事件及其对应的输出样例如下:

  1. 武士降生

输出样例:000:00 blue dragon 1 born

表示在0点0分,编号为1的蓝魔dragon武士降生

如果造出的是lion,那么还要多输出一行,例:

000:00 blue lion 1 born

Its loyalty is 24

表示该lion降生时的忠诚度是24

  1. lion逃跑

输出样例:000:05 blue lion 1 ran away

表示在0点5分,编号为1的蓝魔lion武士逃走

  1. 武士前进到某一城市

输出样例:

000:10 red iceman 1 marched to city 1 with 20 elements and force 30

表示在0点10分,红魔1号武士iceman前进到1号城市,此时他生命值为20,攻击力为30

对于iceman,输出的生命值应该是变化后的数值

  1. wolf抢敌人的武器

000:35 blue wolf 2 took 3 bomb from red dragon 2 in city 4

表示在0点35分,4号城市中,红魔1号武士wolf 抢走蓝魔2号武士dragon 3个bomb。为简单起见,武器不写复数形式

  1. 报告战斗情况

战斗只有3种可能的输出结果:

000:40 red iceman 1 killed blue lion 12 in city 2 remaining 20 elements

表示在0点40分,1号城市中,红魔1号武士iceman 杀死蓝魔12号武士lion后,剩下生命值20

000:40 both red iceman 1 and blue lion 12 died in city 2

注意,把红武士写前面

000:40 both red iceman 1 and blue lion 12 were alive in city 2

注意,把红武士写前面

  1. 武士欢呼

输出样例:003:40 blue dragon 2 yelled in city 4

  1. 武士抵达敌军司令部

输出样例:001:10 red iceman 1 reached blue headquarter with 20 elements and force 30

(此时他生命值为20,攻击力为30)对于iceman,输出的生命值和攻击力应该是变化后的数值

  1. 司令部被占领

输出样例:003:10 blue headquarter was taken

9)司令部报告生命元数量

000:50 100 elements in red headquarter

000:50 120 elements in blue headquarter

表示在0点50分,红方司令部有100个生命元,蓝方有120个

10)武士报告情况

000:55 blue wolf 2 has 2 sword 3 bomb 0 arrow and 7 elements

为简单起见,武器都不写复数形式。elements一律写复数,哪怕只有1个

交代武器情况时,次序依次是:sword,bomb, arrow。

输出事件时:

首先按时间顺序输出;

同一时间发生的事件,按发生地点从西向东依次输出. 武士前进的事件, 算是发生在目的地。

在一次战斗中有可能发生上面的 5 至 6 号事件。这些事件都算同时发生,其时间就是战斗开始时间。一次战斗中的这些事件,序号小的应该先输出。

两个武士同时抵达同一城市,则先输出红武士的前进事件,后输出蓝武士的。

对于同一城市,同一时间发生的事情,先输出红方的,后输出蓝方的。

显然,8号事件发生之前的一瞬间一定发生了7号事件。输出时,这两件事算同一时间发生,但是应先输出7号事件

虽然任何一方的司令部被占领之后,就不会有任何事情发生了。但和司令部被占领同时发生的事件,全都要输出。

输入
第一行是t,代表测试数据组数

每组样例共三行。

第一行,4个整数 M,N,K, T。其含义为:
每个司令部一开始都有M个生命元( 1 <= M <= 100000)
两个司令部之间一共有N个城市( 1 <= N <= 20 )
lion每前进一步,忠诚度就降低K。(0<=K<=100)
要求输出从0时0分开始,到时间T为止(包括T) 的所有事件。T以分钟为单位,0 <= T <= 6000

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

第三行:五个整数,依次是 dragon 、ninja、iceman、lion、wolf 的攻击力。它们都大于0小于等于200
输出
对每组数据,先输出一行:

Case n:

如对第一组数据就输出 Case 1:

然后按恰当的顺序和格式输出到时间T为止发生的所有事件。每个事件都以事件发生的时间开头,时间格式是“时: 分”,“时”有三位,“分”有两位。
样例输入
1
20 1 10 400
20 20 30 10 20
5 5 5 5 5
样例输出
Case 1:
000:00 blue lion 1 born
Its loyalty is 10
000:10 blue lion 1 marched to city 1 with 10 elements and force 5
000:50 20 elements in red headquarter
000:50 10 elements in blue headquarter
000:55 blue lion 1 has 0 sword 1 bomb 0 arrow and 10 elements
001:05 blue lion 1 ran away
001:50 20 elements in red headquarter
001:50 10 elements in blue headquarter
002:50 20 elements in red headquarter
002:50 10 elements in blue headquarter
003:50 20 elements in red headquarter
003:50 10 elements in blue headquarter
004:50 20 elements in red headquarter
004:50 10 elements in blue headquarter
005:50 20 elements in red headquarter
005:50 10 elements in blue headquarter
提示
请注意浮点数精度误差问题。OJ上的编译器编译出来的可执行程序,在这方面和你电脑上执行的程序很可能会不一致。5 * 0.3 的结果,有的机器上可能是 15.00000001,去尾取整得到15,有的机器上可能是14.9999999,去尾取整后就变成14。因此,本题不要写 5 * 0.3,要写 5 * 3 / 10。

源码

main.cpp

#include <iostream>
#include "game.h"


using namespace std;
	
int main(void){
	gameInfo_t gInfo;
	cin>>gInfo.testCase;
	cin>>gInfo.initElements>>gInfo.totalCities>>gInfo.decLoyalty>>gInfo.totalTime;
	cin>>gInfo.warrior[0].element>>gInfo.warrior[1].element>>gInfo.warrior[2].element>>gInfo.warrior[3].element>>gInfo.warrior[4].element;
	cin>>gInfo.warrior[0].force>>gInfo.warrior[1].force>>gInfo.warrior[2].force>>gInfo.warrior[3].force>>gInfo.warrior[4].force;
	gInfo.warrior[0].name = "dragon";
	gInfo.warrior[1].name = "ninja";
	gInfo.warrior[2].name = "iceman";
	gInfo.warrior[3].name = "lion";
	gInfo.warrior[4].name = "wolf";

	cout<<"Case "<<gInfo.testCase<<":"<<endl;
	Game game(gInfo.initElements, gInfo.totalCities, gInfo.decLoyalty, gInfo.totalTime, gInfo.warrior);
	game.begin();
	return 0;
}

game.cpp

#include <iomanip>
#include "game.h"

#define DEBUG 0

using namespace std;

int Game::curTime = 0;

Game::Game(int initElements, int totalCities, int decLoyalty, int totalTime, warriorInfo_t *warrior){
	this->initElements 	= initElements;
	this->totalCities  	= totalCities;
	this->decLoyalty   	= decLoyalty;
	this->totalTime   	= totalTime;
	this->warrior 		= warrior;
}
void Game::printGameTime(void){
	cout<<setw(3)<<setfill('0')<<curTime/60<<":"<<setw(2)<<setfill('0')<<curTime%60<<" ";
}
bool Game::checkGameTime(void){
	//totalTime = 500;			//测试用,限制游戏时间 
	if(curTime >= totalTime)	//到达时间时,退出游戏
		return true;
	else
		return false;
}
void Game::begin(void){
	Warrior *tmp, *tmpRed, *tmpBlue;
	string redName, blueName;
	int ret=0, retRed=0, retBlue=0;
	bool redWin = false;
	bool blueWin = false;
	
	//创建所有城市
	NormalCity city[totalCities];
	for(int i=0;i<totalCities; i++){
		city[i].setCityNum(i+1);
		//cout<<"create normal city, "<<"Number="<<city[i].number<<endl;
	} 

	//创建红蓝基地
	Headquarter *red = new Headquarter(0, "red", initElements, decLoyalty);
	Headquarter *blue = new Headquarter(totalCities+1, "blue", initElements, decLoyalty);
	//red->showCity();
	//blue->showCity();
	
	//游戏正式开始
	while(true){
		//第一阶段,双方基地制造武士
		if(0 == curTime%60){
			redName = red->getWarriorName();		//先获取要制造的武士的名字 
			blueName = blue->getWarriorName();
			//cout<<"redname="<<redName<<",bluename="<<blueName<<endl;
			for(int i=0; i<WARRIOR_KIND; i++){		//按照指定顺序制造武士 
				if(0 == warrior[i].name.compare(redName)){
					red->makeWarrior(warrior[i].element, warrior[i].force, totalCities);
				}
			}
			for(int i=0; i<WARRIOR_KIND; i++){
				if(0 == warrior[i].name.compare(blueName)){
					blue->makeWarrior(warrior[i].element, warrior[i].force, totalCities);
				}	
			} 
			if(checkGameTime())	break;
		}
		
		//第二阶段,5min判断狮子是否逃跑
		curTime += 5;
		if(5 == curTime%60){
			//遍历所有的城市,找出狮子对象 
			for(int i=0; i<totalCities; i++){
				if(city[i].redWarrior){
					tmp = city[i].redWarrior;
					//如果武士是狮子,判断忠诚度,是否会逃跑
					if(0 == tmp->name.compare("lion")){
						Lion *lion = dynamic_cast<Lion *>(tmp);
						if(lion->loyalty <= 0){
							lion->escape();
							red->eraseWarrior(tmp);
							city[i].redWarrior = NULL;
						}	
					} 
				}	
				if(city[i].blueWarrior){
					tmp = city[i].blueWarrior;
					//如果武士是狮子,判断忠诚度,是否会逃跑
					if(0 == tmp->name.compare("lion")){
						Lion *lion = dynamic_cast<Lion *>(tmp);
						if(lion->loyalty <= 0){
							lion->escape();
							blue->eraseWarrior(tmp);
							city[i].blueWarrior = NULL;
						}
					}
				}
			}
			if(checkGameTime())	break; 
		}
		
		//第三阶段,10min武士前进
		curTime += 5;
		if(10 == curTime%60){
			//所以武士完成前进后,再按城市顺序进行汇报
			//如果已经在敌方基地,则无需前进,返回-1
			//如果前进后,到达敌方基地,则返回0,到达常规城市,返回1  
			for(vector<Warrior *>::const_iterator  i=red->warriorVec.begin(); i!=red->warriorVec.end(); ++i){
				retRed = (*i)->march(city);
				if(0 == retRed){
					tmpRed = (*i);	//该武士占领了敌方基地 
					redWin = true;
				}	
			}
			for(vector<Warrior *>::const_iterator  i=blue->warriorVec.begin(); i!=blue->warriorVec.end(); ++i){
				retBlue = (*i)->march(city);
				if(0 == retBlue){
					tmpBlue = (*i);	//该武士占领了敌方基地
					blueWin = true; 	
				}
			}
			//检查红方基地是否被占领 
			if(blueWin){
				//001:10 red iceman 1 reached blue headquarter with 20 elements and force 30
				//003:10 blue headquarter was taken
				printGameTime();
				cout<<"blue "<<tmpBlue->name<<" "<<tmpBlue->number<<" reached red headquarter with "<<tmpBlue->elements<<" elements and force "<<tmpBlue->force<<endl;
				printGameTime();
				cout<<"red headquarter was taken"<<endl;
			}
			//打印普通城市前进信息 
			for(int i=0; i<totalCities; i++){
				if(city[i].redWarrior){
					tmp = city[i].redWarrior;
					Game::printGameTime(); 
					cout<<tmp->team<<" "<<tmp->name<<" "<<tmp->number<<" marched to city "<<city[i].number<<" with "<<tmp->elements<<" elements and force "<<tmp->force<<endl;	
				}
				if(city[i].blueWarrior){
					tmp = city[i].blueWarrior;
					Game::printGameTime(); 
					cout<<tmp->team<<" "<<tmp->name<<" "<<tmp->number<<" marched to city "<<city[i].number<<" with "<<tmp->elements<<" elements and force "<<tmp->force<<endl;
				}
			}
			//检查蓝方基地是否被占领
			if(redWin){
				//001:10 red iceman 1 reached blue headquarter with 20 elements and force 30
				//003:10 blue headquarter was taken
				printGameTime();
				cout<<"red "<<tmpRed->name<<" "<<tmpRed->number<<" reached blue headquarter with "<<tmpRed->elements<<" elements and force "<<tmpRed->force<<endl;
				printGameTime();
				cout<<"blue headquarter was taken"<<endl;
			}
			 
			if(redWin || blueWin)			//有一方到达敌方基地,则游戏结束 
				break;
			if(checkGameTime())	break; 
		}
		
		//第四阶段,35min狼抢夺武器 
		curTime += 25;
		if(35 == curTime%60){
			for(int i=0; i<totalCities; i++){
				//同一城市有两个武士 
				if(city[i].redWarrior && city[i].blueWarrior){
					tmp = city[i].redWarrior; 
					if(0 == tmp->name.compare("wolf")){
						Wolf *wolf = dynamic_cast<Wolf *>(tmp);
						wolf->rob(city[i].blueWarrior);
					}
					tmp = city[i].blueWarrior; 
					if(0 == tmp->name.compare("wolf")){
						Wolf *wolf = dynamic_cast<Wolf *>(tmp);
						wolf->rob(city[i].redWarrior);
					}
				}		
			}
			if(checkGameTime())	break; 
		}

		//第五阶段,40min发生战斗 
		curTime += 5;
		if(40 == curTime%60){
			for(int i=0; i<totalCities; i++){
				tmpRed = city[i].redWarrior;
				tmpBlue = city[i].blueWarrior;

				//同一城市有两个武士则开战,一定要先对武器排序 
				if(tmpRed && tmpBlue){
					ret = city[i].fight(tmpRed, tmpBlue);
					
					if(ret == -2){		//都死了 
						//000:40 both red iceman 1 and blue lion 12 died in city 2
						Game::printGameTime(); 
						cout<<"both red "<<tmpRed->name<<" "<<tmpRed->number<<" and blue "<<tmpBlue->name<<" "<<tmpBlue->number<<" died in city "<<city[i].number<<endl;
					}
					else if(ret == -1){	//蓝方活着 
						//008:40 red iceman 6 killed blue ninja 3 in city 4 remaining 85 elements
						Game::printGameTime(); 
						cout<<"blue "<<tmpBlue->name<<" "<<tmpBlue->number<<" killed red "<<tmpRed->name<<" "<<tmpRed->number<<" in city "<<city[i].number<<" remaining "<<tmpBlue->elements<<" elements"<<endl;
						tmpBlue->getEnemyWeapon(tmpRed);
					}
					else if(ret == 1){	//红方活着 
						Game::printGameTime(); 
						cout<<"red "<<tmpRed->name<<" "<<tmpRed->number<<" killed blue "<<tmpBlue->name<<" "<<tmpBlue->number<<" in city "<<city[i].number<<" remaining "<<tmpRed->elements<<" elements"<<endl;
						tmpRed->getEnemyWeapon(tmpBlue);
					}
					else if(ret == 0){	//都活着 
						//008:40 both red lion 7 and blue dragon 2 were alive in city 3
						Game::printGameTime(); 
						cout<<"both red "<<tmpRed->name<<" "<<tmpRed->number<<" and blue "<<tmpBlue->name<<" "<<tmpBlue->number<<" were alive in city "<<city[i].number<<endl;
					}
					//活着的龙,欢呼 
					if(tmpRed->alive() && (0 == tmpRed->name.compare("dragon"))){
						Dragon *dragon = dynamic_cast<Dragon *>(tmpRed);
						dragon->yelled(); 
					}
					if(tmpBlue->alive() && (0 == tmpBlue->name.compare("dragon"))){
						Dragon *dragon = dynamic_cast<Dragon *>(tmpBlue);
						dragon->yelled(); 
					}
					//删除死去的武士对象
					if(!tmpRed->alive()) {
						red->eraseWarrior(tmpRed);
						city[i].redWarrior = NULL;
					}	
					if(!tmpBlue->alive()){
						blue->eraseWarrior(tmpBlue);
						city[i].blueWarrior = NULL; 
					}
				}
			}
			if(checkGameTime())	break; 
		}
		//第六阶段,50min司令部报告剩余生命元 
		curTime += 10;
		if(50 == curTime%60){
			red->report();
			blue->report();
			if(checkGameTime())	break; 
		}
		
		//第七阶段,55min武士报告拥有武器情况 
		curTime += 5;
		if(55 == curTime%60){
			for(int i=0; i<totalCities; i++){
				tmpRed = city[i].redWarrior;
				tmpBlue = city[i].blueWarrior;
				if(tmpRed)	tmpRed->showInfo();
				if(tmpBlue)	tmpBlue->showInfo();
			}
			if(checkGameTime())	break; 
		}
		
		//第八阶段,60min 
		curTime += 5;
	} 
}


#if DEBUG
int main(){
	// case 1
	gameInfo_t gInfo = {
		.testCase = 1,
		.initElements = 6000,
		.totalCities = 10,
		.decLoyalty = 10,
		.totalTime = 4000,
		.warrior ={
			{"dragon",	120,	50},
			{"ninja",	20,		50},
			{"iceman",	130,	50},
			{"lion",	10,		50},
			{"wolf",	20,		150}
		}
	};
	cout<<"Case "<<gInfo.testCase<<":"<<endl;
	Game game(gInfo.initElements, gInfo.totalCities, gInfo.decLoyalty, gInfo.totalTime, gInfo.warrior);
	game.begin();	
}

#endif

game.h

#ifndef __GAME__H
#define __GAME__H

#include <iostream>
#include <map>
#include "city.h" 

#define WARRIOR_KIND	5 
     
typedef struct WARRIOR_INFO{
	string name;
	int element;
	int force;
}warriorInfo_t;

typedef struct GAME_INFO{
	int testCase;
	int initElements;
	int totalCities;
	int decLoyalty;
	int totalTime;
	warriorInfo_t warrior[WARRIOR_KIND];
}gameInfo_t;

class Game{
public:
	int initElements;			//基地初始生命元
	int decLoyalty;				//狮子每次递减的忠诚度 
	int totalCities;			//城市总数量(不算红蓝军基地)
	int totalTime;				//总的游戏时长,到时间后游戏结束 
	static int curTime;			//当前游戏时间
	warriorInfo_t *warrior;		//保存武士的初始化信息
public:
	Game(int initElements, int totalCities, int decLoyalty, int totalTime, warriorInfo_t *warrior); 	//创建游戏对象,游戏初始化 
	void begin(void);			//游戏正式开始 
	static void printGameTime(void);		//打印当前游戏时间
	bool checkGameTime(void); 				//检查游戏时间,确认是否应该结束游戏 
};


#endif

city.cpp

#include <iomanip>
#include "city.h"
#include "game.h"

#define DEBUG 0 

using namespace std;

City::City(){
}
City::City(int number){
	if(number < 0)	this->number = 0;
	else			this->number = number;
}
void City::setCityNum(int number){
	if(number < 0)	this->number = 0;
	else			this->number = number;
}



NormalCity::NormalCity(void){
	redWarrior = NULL;
	blueWarrior = NULL;
}
int NormalCity::fight(Warrior *red, Warrior *blue){
	int ret1=0,ret2=0;
	Warrior *first,*last;	 
	if(number % 2){
		first = red;							//奇数城市,红方先出手 
		last = blue;
	}else{
		first = blue;
		last = red;
	}
	first->weaponSort();
	last->weaponSort();

	//当某一方只剩武器 剑 时,且它的剑攻击力为0,attack函数也返回0,退出循环 
	while(true){
		ret1 = first->attack(last);
		if(!first->alive() || !last->alive())	//出现死人的情况,则退出循环  	
			break;
		if(ret1 && ret2)						//双方都没有武器,则返回0
			return 0;
		ret2 = last->attack(first);
		if(!first->alive() || !last->alive())	//出现死人的情况,则退出循环 
			break;
		if(ret1 && ret2)						//双方都没有武器,则返回0
			return 0;
	}
	if(red->alive() && blue->alive())			//都或者,返回0 
		return 0;
	if(red->alive())	{						//红方活着返回1 
		//cout<<"red alive"<<endl;
		return 1;	
	}
	else if(blue->alive())	{					//蓝方活着返回-1 
		//cout<<"blue alive"<<endl;
		return -1;	
	}
	else										//都死了返回 -2 
		return -2;
}


Headquarter::Headquarter(int number, string team, int elements, int loyaltyDec):City(number){
	this->team = team;
	this->elements = elements;
	this->warriorNum = 0;
	this->setWarriorOrder(team);
	this->loyaltyDec = loyaltyDec;
	this->warrior = NULL;
}
void Headquarter::setWarriorOrder(string team){
	if(0 == team.compare("blue")){
		warriorOrder[0] = "lion";
		warriorOrder[1] = "dragon";
		warriorOrder[2] = "ninja";
		warriorOrder[3] = "iceman";
		warriorOrder[4] = "wolf";
	}else if(0 == team.compare("red")){
		warriorOrder[0] = "iceman";
		warriorOrder[1] = "lion";
		warriorOrder[2] = "wolf";
		warriorOrder[3] = "ninja";
		warriorOrder[4] = "dragon";
	}
}
string Headquarter::getWarriorName(void){
	return warriorOrder[warriorNum%5];
} 
int Headquarter::decElements(int elements){
	if(this->elements >= elements){
		this->elements -= elements;
		return 0;
	}
	else
		return -1;
}
Warrior * Headquarter::makeWarrior(int elements, int force, int totalCities){
	bool direction;
	if(this->elements < elements)							//基地生命元过低时,无法生产武士 
		return NULL;

	if(0 == team.compare("red"))	direction = true;	 
	else							direction = false;

	if(0 == warriorOrder[warriorNum%5].compare("dragon")){
		warrior = new Dragon(team, ++warriorNum, elements, force, number, totalCities, direction);
	}	
	else if(0 == warriorOrder[warriorNum%5].compare("ninja")){
		warrior = new Ninja(team, ++warriorNum, elements, force, number, totalCities, direction);
	}	
	else if(0 == warriorOrder[warriorNum%5].compare("iceman")){
		warrior = new Iceman(team, ++warriorNum, elements, force, number, totalCities, direction);
	}
	else if(0 == warriorOrder[warriorNum%5].compare("lion")){
		warrior = new Lion(team, ++warriorNum, elements, force, number, totalCities, direction, this->elements-elements, loyaltyDec);
		Lion *lion = dynamic_cast<Lion *>(warrior);
		if(lion->loyalty <= 0){		//如果刚好创建狮子后,基地血量为0,则狮子忠诚度也为0,狮子逃跑 
			lion->escape();
			delete warrior;
			warrior = NULL;
		}	
	}
	else if(0 == warriorOrder[warriorNum%5].compare("wolf")){
		warrior = new Wolf(team, ++warriorNum, elements, force, number, totalCities, direction);
	}
	decElements(elements);				//制造武士后,基地生命元降低 
	if(warrior)
		warriorVec.push_back(warrior);	//如果狮子没有逃跑,则加入该阵营的武士容器 
	return warrior;	
}
int Headquarter::eraseWarrior(Warrior *warrior){
	for(vector<Warrior *>::iterator  i=warriorVec.begin(); i!=warriorVec.end(); ++i){
		if((*i) == warrior){
			warriorVec.erase(i);
			//delete warrior;			//目前加上去会有BUG,暂时还不清楚什么原因 
			return 0;
		}
	}
	return -1;
}
void Headquarter::report(void){
	//008:50 5520 elements in red headquarter 
	Game::printGameTime(); 
	cout<<elements<<" elements in "<<team<<" headquarter"<<endl;
} 
void Headquarter::showWarrior(void){
	for(vector<Warrior *>::const_iterator i=warriorVec.begin(); i!=warriorVec.end(); ++i){
		cout<<"team="<<(*i)->team<<",name="<<(*i)->name<<",number="<<(*i)->number<<",elements="\
		<<(*i)->elements<<",force="<<(*i)->force<<",city="<<(*i)->city<<",totalCities="<<(*i)->totalCities<<",direction="<<(*i)->direction<<endl;
	
		if(0 == (*i)->name.compare("lion")){
			Lion *lion = dynamic_cast<Lion *>(*i);
			cout<<" Loyalty="<<lion->loyalty<<" loyaltyDec="<<loyaltyDec;
		}
		cout<<endl;
	}
}	
void Headquarter::showCity(){
	cout<<team<<", cityNumber="<<number<<", baseName="<<team<<", elements="<<elements<<", warriorTotalNum="<<warriorNum<<endl;
} 

#if DEBUG
int main(){
	//测试1 创建普通城市
//	int MAX = 10;
//	NormalCity city[MAX];
//	for(int i=0;i<MAX; i++){
//		city[i].setCityNum(i+1);
//		cout<<"create normal city, "<<"Number="<<city[i].number<<endl;
//	} 

	//测试2 创建红蓝基地
//	Headquarter *red = new Headquarter(0, "red", 1000, 100);
//	Headquarter *blue = new Headquarter(11, "blue", 2000, 70);
//	red->showCity();
//	blue->showCity();
//
//	//测试3 生产武士
//	Warrior *w1 = red->makeWarrior(100, 20);
//	Warrior *w2 = red->makeWarrior(200, 50);
//	Warrior *w3 = red->makeWarrior(150, 32);
//	
////	red->makeWarrior(147, 21);
////	red->makeWarrior(23, 501);
////	red->makeWarrior(44, 10);
////	red->makeWarrior(30, 88);	
//
//	red->showWarrior();
//	cout<<"after"<<endl;
////	//red->showCity();
//	int ret = red->eraseWarrior(w1);
////
//	if(ret == 0){
//		cout<<w1->team<<" "<<w1->name<<" "<<w1->number <<" elements="<<w1->elements<<" force="<<w1->force;
//		delete w1;
//	}
////		
//	
//	red->showWarrior();
	
	
	//测试4 两个武士战斗
//	int ret=0;
//	city[0].redWarrior = red->makeWarrior(100, 20);
//	city[0].blueWarrior = blue->makeWarrior(200, 50);
//	
//	city[0].redWarrior->showWeaponInfo();
//	city[0].redWarrior->showTestInfo();
//	city[0].blueWarrior->showWeaponInfo();
//	city[0].blueWarrior->showTestInfo();
//
//	ret = city[0].fight(city[0].redWarrior, city[0].blueWarrior);
//	if(ret == -2)
//		cout<<"both died"<<endl;
//	else if(ret == -1)
//		cout<<"blue died"<<endl;
//	else if(ret == 1)
//		cout<<"red died"<<endl;
//	else if(ret == 0)
//		cout<<"both alive"<<endl;
}
#endif

city.h

#ifndef __CITY__H
#define __CITY__H

#include <iostream>
#include <vector>
#include "warrior.h"
using namespace std;

//基类:城市 
class City{
public:
	int number;								//每个城市都有编号
public:
	City();
	City(int number);
	void setCityNum(int number);		 
};

//司令部 
class Headquarter : public City{
public:
	string team;							//阵营名字
	int elements;							//基地剩余生命元
	int warriorNum; 						//武士最大编号
	int initLoyalty;						//狮子的初始化忠诚度 
	int loyaltyDec;							//狮子的忠诚度递减数 
	string warriorOrder[5];					//制造顺序  
	vector<Warrior *> warriorVec;			//用以存储的武士对象的指针
	Warrior *warrior;
public:
	Headquarter(int number, string team, int elements, int loyaltyDec);
	void setWarriorOrder(string team);		//设置阵容制造武士的顺序
	string getWarriorName(void);			//获取当前需要制造的武士名字 
	int decElements(int elements);			//剩余生命源数量减少
	Warrior * makeWarrior(int elements, int force, int totalCities);	//制造武士
	int eraseWarrior(Warrior *warrior);		//删除死去的武士 
	void report(void);						//司令部报告剩余生命元 
	void showWarrior(void);					//显示目前阵营所有活着的武士,仅供测试用
	void showCity(void);					//显示城市信息 ,仅供测试用
};

//普通城市类 
class NormalCity : public City{
public:
	Warrior *redWarrior;
	Warrior *blueWarrior;
public:
	NormalCity();
	int fight(Warrior *red, Warrior *blue);
};




#endif

warrior.cpp

#include <iomanip>
#include "warrior.h" 
#include "game.h"
#define DEBUG 0

using namespace std;

//武器进行排序时的算法 
bool mySort(const WEAPON_PAIR& lhs, const WEAPON_PAIR& rhs)
{
	if(lhs.first != rhs.first)				//编号不相同的武器,根据编号排序 
    	return lhs.first < rhs.first;
    else									//编号相同的武器,根据剩余使用次数排序	
    	return lhs.second->times < rhs.second->times;
}
 
Warrior::Warrior(){
	this->name 		= "default";
	this->team  	= "default";
	this->number 	= 0;
	this->elements 	= 0;
	this->force 	= 0;
	this->city 		= 0;
	this->totalCities= 0; 
	this->direction	= false;
	Game::printGameTime();
	printBornInfo();
}
	
Warrior::Warrior(string name, string team, int number, int elements, int force, int city, int totalCities, bool direction){
	if(name.empty())	this->name 		= "default";
	else				this->name 		= name;

	if(team.empty())	this->team 		= "default";
	else				this->team 		= team;
	
	if(number < 0)		this->number 	= 0;
	else				this->number 	= number;
	
	if(elements < 0)	this->elements 	= 0;
	else				this->elements 	= elements;
	
	if(force < 0)		this->force 	= 0;
	else				this->force 	= force;

	if(city < 0)		this->city 		= 0;
	else				this->city 		= city;
	
	if(totalCities < 0)	this->totalCities	= 0;
	else				this->totalCities	= totalCities;
	
	this->direction = direction;
	Game::printGameTime(); 
	printBornInfo();
}
/*
一般情况下,武士消亡时,武器会被敌人缴获
但是当敌人武器数量等于10时,武器并不会被全部缴获,因此需要手动删除 
*/
Warrior::~Warrior(){				//武士消亡时,武器库也要清空 
	//cout<<"~Warrior call"<<endl;
	for(WEAPON_VEC::iterator i=weaponVec.begin(); i!=weaponVec.end(); ++i){
		delete ((*i).second);		//删除武器库的武器对象 
	} 
	weaponVec.clear(); 				//清空容器
}
int Warrior::decElements(int force){
	if(force <= 0)
		return -2; 			//攻击为负,或者攻击为0,返回-2
	if(elements > 0){
		elements -= force;	//正常攻击,减血,返回0 
		return 0;
	}
	return -1;				//血量已经不大于0,返回-1 
}
bool Warrior::alive(void){
	if(elements > 0)
		return true;
	else
		return false;
}
int Warrior::march(NormalCity* iCity){
	int enemyCity = 0; 
	if(0 == team.compare("red")){
		enemyCity = totalCities+1;
	}else if(0 == team.compare("blue")){
		enemyCity = 0;
	}else{
		return -2;			//未知错误 
	}
	if(city == enemyCity)	//如果当前已经在敌方基地,则无需前进,返回-1
		return -1;
	//前进一个城市 
	if(direction)	city += 1;		
	else			city -= 1;
	//cout<<team<<" "<<name<<", number="<<number<<" march city = "<<city<<", enemyCity = "<<enemyCity<<endl;
	setCity(iCity); 
	//到达敌方基地,返回0, 到达常规城市,返回1 
	if(city == enemyCity)		return 0;
	else						return 1;					
}
void Warrior::setCity(NormalCity* iCity){		//武士前进后,需要将之前和当前的城市对象中的武士指针重新赋值 
	if(totalCities < 0 || NULL == iCity)		//参数不对直接退出 
		return;
	if(0 == team.compare("red")){
		if((city - 1) > 0){
			iCity[city - 2].redWarrior = NULL;	//武士前进后,上一个城市不为基地,则赋值为NULL
		}else{
			//cout<<"no null "<<team<<" "<<name<<" number="<<number<<" city="<<city<<endl;
		}
		if(city != (totalCities+1)){				//武士前进后,所在城市不是敌方基地 				
			iCity[city - 1].redWarrior = this;	//将武士的指针赋值给城市对象 
		}else{
			//cout<<"no this "<<team<<" "<<name<<" number="<<number<<" city="<<city<<endl;
		}
	}else if(0 == team.compare("blue")){
		if((city + 1) < (totalCities+1)){
			iCity[city].blueWarrior = NULL;		//武士前进后,上一个城市不为基地,则赋值为NULL
		}
		if(city != 0){							//武士前进后,所在城市不是敌方基地 
			iCity[city-1].blueWarrior = this;	//将武士的指针赋值给城市对象
		}
	}
}
int Warrior::attack(Warrior * enemy){
	if((NULL == enemy) || (enemy == this))
		return -1;										//传入错误参数,直接返回-1 
	if(weaponVec.empty())
		return 1;										//没有武器时,直接返回1
	if(checkWeaponNoForce())							//武器为剑,且没有伤害时,返回 
		return 1;
	int number = (*iweapon).second->number;				//获取当前武器的编号 
	int force  = (*iweapon).second->force;				//获取当前武器的攻击力
	int times  = (*iweapon).second->useWeapon();		//使用武器,获取剩余可使用次数 
	enemy->decElements(force);							//敌人血量降低 
	//cout<<team<<" "<<name<<" attack "<<enemy->team<<" "<<enemy->name<<" with weapon number "<<number<<", enemy elements dec "<<force<<", now elements is "<<elements<<" enemy elements is "<<enemy->elements<<endl;
	if(1 == number){ 									//使用炸弹时 
		this->decElements(force/2);						//自己受到伤害的一半
		//cout<<team<<" "<<name<<"get hurt form bomb, elements dec "<<force/2<<endl; 
	} 
	if(times > 0)										//如果武器还能用 
		iweapon++;										//则指向下一个武器 
	else{
		delete (*iweapon).second;						//删除武器对象
		weaponVec.erase(iweapon);						//从容器中移除 
	} 
	if(iweapon == weaponVec.end())
		iweapon = weaponVec.begin();					//武器用完一遍又回到开头
	return 0;
}
void Warrior::weaponSort(void){
	sort(weaponVec.begin(), weaponVec.end(), mySort);
	iweapon = weaponVec.begin();						//指针指向武器库的开头 
}
void Warrior::getEnemyWeapon(Warrior * enemy){
	if((NULL == enemy) || (enemy == this))
		return;											//传入错误参数,直接返回 	
	if(enemy->weaponVec.empty())						//如果敌人没有武器,则直接返回 
		return;
	for(WEAPON_VEC::iterator i=enemy->weaponVec.begin(); i!=enemy->weaponVec.end(); ){
		if(weaponVec.size() < 10){						//武器少于10个,才缴获 
			if(0 == (*i).first){						//攻击力重新计算 
				(*i).second->force = force*2/10;
			}else if(1 == (*i).first){
				(*i).second->force = force*4/10;
			}else if(2 == (*i).first){
				(*i).second->force = force*3/10;
			}
			weaponVec.push_back(*i);					//放入武器容器中 
			enemy->weaponVec.erase(i);					//移除原先武士的对应武器
		}else
			break;										//武器达到10个,直接退出 
	}
	weaponSort();										//缴获武器后,进行排序
}
bool Warrior::checkWeaponNoForce(void){
	for(WEAPON_VEC::const_iterator i=weaponVec.begin(); i!=weaponVec.end(); ++i){
		if((*i).second->force > 0){			//武器有攻击力,则武器正常使用	
			return false;
		} 				 	
		if((*i).first != 0){				//攻击力为0的炸弹和弓箭,也要使用
			return false;
		}		
	}
	return true;
}
void Warrior::showInfo(void){
	int swordNum = 0;
	int bombNum = 0;
	int arrowNum = 0;
	for(WEAPON_VEC::const_iterator i=weaponVec.begin(); i!=weaponVec.end(); ++i){
		if(0 == (*i).first)
			swordNum++;
		else if(1 == (*i).first)
			bombNum++;
		else if(2 == (*i).first)
			arrowNum++;
	}
	Game::printGameTime(); 
	cout<<team<<" "<<name<<" "<<number <<" has "<<swordNum<<" sword "<<bombNum<<" bomb "<<arrowNum<<" arrow and "<<elements<<" elements"<<endl;
} 
void Warrior::showWeaponInfo(void){
	cout<<team<<" "<<name<<" "<<number<<" has "<<endl;
	for(WEAPON_VEC::const_iterator i=weaponVec.begin(); i!=weaponVec.end(); ++i){
		cout<<"number="<<(*i).second->number<<", force="<<(*i).second->force<<",times="<<(*i).second->times<<endl;
	}
	if(weaponVec.empty())
		cout<<"nothing"<<endl;
}

void Warrior::showTestInfo(void){
	cout<<"name="<<name<<",team="<<team<<",number="<<number<<",elements="\
		<<elements<<",force="<<force<<",city="<<city<<",totalCities="<<totalCities<<",direction="<<direction<<endl;
}
void Warrior::printBornInfo(void){
	cout<<team<<" "<<name<<" "<<number<<" born"<<endl;
}



Dragon::Dragon(string team, int number, int elements, int force, int city, int totalCities, bool direction):Warrior("dragon", team, number, elements, force, city, totalCities, direction){
	//龙诞生时,获得武器 
	if(0 == number%3)
		weaponVec.push_back(make_pair(0, new Sword(force*2/10)));
	else if(1 == number%3)
		weaponVec.push_back(make_pair(1, new Bomb(force*4/10)));	
	else
		weaponVec.push_back(make_pair(2, new Arrow(force*3/10)));	
}
Dragon::~Dragon(){
	cout<<"Dragon call"<<endl; 
}
void Dragon::yelled(void){
	//red dragon 5 yelled in city 7
	Game::printGameTime(); 
	cout<<team<<" "<<name<<" "<<number<<" yelled in city "<<city<<endl;
}




Ninja::Ninja(string team, int number, int elements, int force, int city, int totalCities, bool direction):Warrior("ninja", team, number, elements, force, city, totalCities, direction){
	if(0 == number%3){
		weaponVec.push_back(make_pair(0, new Sword(force*2/10)));
		weaponVec.push_back(make_pair(1, new Bomb(force*4/10)));
	}
	else if(1 == number%3){
		weaponVec.push_back(make_pair(1, new Bomb(force*4/10)));
		weaponVec.push_back(make_pair(2, new Arrow(force*3/10)));		
	}	
	else{
		weaponVec.push_back(make_pair(2, new Arrow(force*3/10)));
		weaponVec.push_back(make_pair(0, new Sword(force*2/10)));	
	}	
}
Ninja::~Ninja(){
	cout<<"Ninja call"<<endl; 
}
int Ninja::attack(Warrior * enemy){
	if((NULL == enemy) || (enemy == this))
		return -1;										//传入错误参数,直接返回-1 
	if(weaponVec.empty())
		return 1;										//没有武器时,直接返回1
	if(checkWeaponNoForce())							//武器为剑,且没有伤害时,返回 
		return 1;

	int number = (*iweapon).second->number;				//获取当前武器的编号 
	int force  = (*iweapon).second->force;				//获取当前武器的攻击力
	int times  = (*iweapon).second->useWeapon();		//使用武器,获取剩余可使用次数 

	enemy->decElements(force);							//敌人血量降低 
	//cout<<name<<" attack "<<enemy->name<<" with weapon number "<<number<<", enemy elements dec "<<force<<endl;

	//忍者使用炸弹不受伤害 
	if(1 == number){ 									//使用炸弹时 
		//this->decElements(force/2);						//自己受到伤害的一半
		//cout<<name<<"get hurt form bomb, elements dec "<<force/2<<endl; 
		//cout<<"Ninja protected himself from being hurt by bomb"<<endl;
	} 

	if(times > 0)										//如果武器还能用 
		iweapon++;										//则指向下一个武器 
	else{
		delete (*iweapon).second;						//删除武器对象
		weaponVec.erase(iweapon);						//从容器中移除 
	} 
	
	if(iweapon == weaponVec.end())
		iweapon = weaponVec.begin();					//武器用完一遍又回到开头
	return 0;
}


Iceman::Iceman(string team, int number, int elements, int force, int city, int totalCities, bool direction):Warrior("iceman", team, number, elements, force, city, totalCities, direction){
	if(0 == number%3)
		weaponVec.push_back(make_pair(0, new Sword(force*2/10)));
	else if(1 == number%3)
		weaponVec.push_back(make_pair(1, new Bomb(force*4/10)));	
	else
		weaponVec.push_back(make_pair(2, new Arrow(force*3/10)));		
}
Iceman::~Iceman(){
	cout<<"Iceman call"<<endl; 
}
int Iceman::march(NormalCity* iCity){
	int enemyCity = 0; 
	if(0 == team.compare("red")){
		enemyCity = totalCities+1;
	}else if(0 == team.compare("blue")){
		enemyCity = 0;
	}else{
		return -2;			//未知错误 
	}
	
	if(city == enemyCity)	//如果当前已经在敌方基地,则无需前进,返回-1
		return -1;
	
	//前进一个城市 
	if(direction)	city += 1;		
	else			city -= 1;
	//cout<<team<<" "<<name<<", number="<<number<<" march city = "<<city<<", enemyCity = "<<enemyCity<<endl;
	setCity(iCity); 
	elements -= elements/10;		//每前进一个城市,血量减少1/10
	//到达敌方基地,返回0, 到达常规城市,返回1 
	if(city == enemyCity)		return 0;
	else						return 1;	
}




Lion::Lion(string team, int number, int elements, int force, int city, int totalCities, bool direction, int loyalty, int loyaltyDec):Warrior("lion", team, number, elements, force, city, totalCities, direction){
	if(0 == number%3)
		weaponVec.push_back(make_pair(0, new Sword(force*2/10)));
	else if(1 == number%3)
		weaponVec.push_back(make_pair(1, new Bomb(force*4/10)));	
	else
		weaponVec.push_back(make_pair(2, new Arrow(force*3/10)));
	this->loyalty = loyalty;		
	this->loyaltyDec = loyaltyDec;		
	printLoyalty();
}
Lion::~Lion(){
	cout<<"Lion call"<<endl; 
}
int Lion::march(NormalCity* iCity){
	int enemyCity = 0; 
	if(0 == team.compare("red")){
		enemyCity = totalCities+1;
	}else if(0 == team.compare("blue")){
		enemyCity = 0;
	}else{
		return -2;			//未知错误 
	}
	
	if(city == enemyCity)	//如果当前已经在敌方基地,则无需前进,返回-1
		return -1;
	
	//前进一个城市 
	if(direction)	city += 1;		
	else			city -= 1;
	//cout<<team<<" "<<name<<", number="<<number<<" march city = "<<city<<", enemyCity = "<<enemyCity<<endl;
	setCity(iCity); 
	loyalty -= loyaltyDec;		//每前进一个城市,忠诚度降低 
	//到达敌方基地,返回0, 到达常规城市,返回1 
	if(city == enemyCity)		return 0;
	else						return 1;	
}
void Lion::escape(void){
	//002:05 blue lion 1 ran away
	Game::printGameTime(); 
	cout<<team<<" "<<name<<" "<<number<<" ran away"<<endl; 
}
void Lion::showTestInfo(void){
	cout<<"name="<<name<<",team="<<team<<",number="<<number<<",elements="\
		<<elements<<",force="<<force<<",city="<<city<<",direction="<<direction<<",loyalty="<<loyalty<<",loyaltyDec="<<loyaltyDec<<endl;
}
void Lion::printLoyalty(void){
	cout<<"Its loyalty is "<<loyalty<<endl;
}



Wolf::Wolf(string team, int number, int elements, int force, int city, int totalCities, bool direction):Warrior("wolf", team, number, elements, force, city, totalCities, direction){}
Wolf::~Wolf(){
	cout<<"Wolf call"<<endl; 
}
int Wolf::rob(Warrior * enemy){
	if((NULL == enemy) || (enemy == this))
		return -1;					//传入错误参数,直接返回-1 
	if(enemy->weaponVec.empty() || (0 == enemy->name.compare("wolf")))
		return 1;					//敌人没有武器或者敌人是狼时,直接返回1
		
	int swordNum = 0;
  	int bombNum = 0;
	int arrowNum = 0;
				
	enemy->weaponSort();			//将敌人武器分类后,从编号小的开始抢																		
	int  min = (*(enemy->weaponVec.begin())).first;		//记下敌人最小的武器编号 
	for(WEAPON_VEC::iterator i=enemy->weaponVec.begin(); i!=enemy->weaponVec.end(); ){
		if((*i).first != min)							//抢完所有最小编号的武最后就退出 
			break; 
		if(weaponVec.size() < 10){						//狼的武器少于10个,才抢 
			if(0 == (*i).first){						//攻击力重新计算 
				swordNum++;
				(*i).second->force = force*2/10;
			}else if(1 == (*i).first){
				bombNum++;
				(*i).second->force = force*4/10;
			}else if(2 == (*i).first){
				arrowNum++;
				(*i).second->force = force*3/10;
			}
			weaponVec.push_back(*i);					//放入狼的武器容器中 
			enemy->weaponVec.erase(i);					//移除原先武士的对应武器
		}	
	}
	//008:35 red wolf 3 took 1 sword from blue lion 6 in city 7
	Game::printGameTime(); 
	cout<<team<<" "<<name<<" "<<number<<" took ";
	
	if(swordNum || bombNum || arrowNum){
		if(swordNum){
			cout<<swordNum<<" sword ";
		}
		if(bombNum){
			cout<<bombNum<<" bomb ";
		}
		if(arrowNum){
			cout<<arrowNum<<" arrow ";
		}
	}
	cout<<"from "<<enemy->team<<" "<<enemy->name<<" "<<enemy->number<<" in city "<<city<<endl;
	return 0;
}
#if DEBUG
int main(){
	//测试1 : 武士基类的构造函数和所有信息打印函数 
//	Warrior *w1 = new Warrior(); 
//	Warrior *w2 = new Warrior("pig", "blue", 10, 100, 120, 3, false); 
// 
//	w1->showInfo();
//	w2->showInfo();
//	
//	w1->showWeaponInfo();
//	w2->showWeaponInfo();
//	
//	w1->showTestInfo();
//	w2->showTestInfo();
	
	//测试2 : 武士血量降低,判断武士是否活着
//	Warrior *w2 = new Warrior("pig", "blue", 10, 100, 120, 3, false); 
//	if(w2->alive())
//		cout<<"before:alive"<<endl;
//	else
//		cout<<"before:died"<<endl;
	//血量降低为0的测试 
//	w2->decElements(100);
//	if(w2->alive())
//		cout<<"after:alive"<<endl;
//	else
//		cout<<"after:died"<<endl; 
	//血量降低为负数的测试 
//	w2->decElements(200);
//	if(w2->alive())
//		cout<<"after:alive"<<endl;
//	else
//		cout<<"after:died"<<endl;  
	
	//测试3 : 武士前进到下一个城市
//	int ret=1;
//	Warrior *w2 = new Warrior("pig", "blue", 10, 100, 120, 4, false); 
//	w2->showTestInfo(); 
//	while(ret > 0){				//返回值大于0,说明之前前进到一个普通城市,可以进行下一次前进 
//		ret = w2->march(0);	
//	}
	
	//测试4 :武器排序、攻击、和缴获测试 
//	Warrior *w1 = new Warrior("redBoy", "red",  10,  500, 120, 3, true); 
//	Warrior *w2 = new Warrior("blueGirl", "blue",  8,  200, 60,  0, false);
//	 
//	//创建武器对象 
//	Weapon *pSword1  = new Sword(10);
//	Weapon *pSword2  = new Sword(20);
//	Weapon *pSword3  = new Sword(30);
//	Weapon *pBomb1   = new Bomb(40);
//	Weapon *pBomb2   = new Bomb(50);
//	Weapon *pBomb3   = new Bomb(60);
//	Weapon *pArrow1  = new Arrow(70);
//	Weapon *pArrow2  = new Arrow(80);
//	Weapon *pArrow3  = new Arrow(90);
//	Weapon *pArrow4  = new Arrow(80);
//	Weapon *pArrow5  = new Arrow(70);
//	
//	//给双方发武器
//	w1->weaponVec.push_back(make_pair(pArrow1->number, pArrow1));
//	w1->weaponVec.push_back(make_pair(pSword1->number, pSword1));
//	w1->weaponVec.push_back(make_pair(pBomb2->number, pBomb2));
//	w1->weaponVec.push_back(make_pair(pArrow2->number, pArrow2));
//	
//	w2->weaponVec.push_back(make_pair(pArrow3->number, pArrow3));
//	w2->weaponVec.push_back(make_pair(pSword2->number, pSword2));
//	w2->weaponVec.push_back(make_pair(pSword3->number, pSword3));
//	w2->weaponVec.push_back(make_pair(pBomb1->number, pBomb1));
//	w2->weaponVec.push_back(make_pair(pBomb3->number, pBomb3));
//	w2->weaponVec.push_back(make_pair(pArrow4->number, pArrow4));
//	w2->weaponVec.push_back(make_pair(pArrow5->number, pArrow5));
	
	//展示武器数量 
//	w1->showInfo();
//	w2->showInfo();
//	w1->showWeaponInfo();
//	w2->showWeaponInfo();
//	
//	排序函数
//	pArrow2->useWeapon();	//使用一次,看排序是否正确 
//	pArrow4->useWeapon();	//使用一次,看排序是否正确 
//	w1->weaponSort();
//	w2->weaponSort();
//	cout<<"after sort"<<endl; 
//	w1->showWeaponInfo();
//	w2->showWeaponInfo();

	//缴获武器测试
//	cout<<"after take"<<endl; 
//	//w1->getEnemyWeapon(w2);
//	w2->getEnemyWeapon(w1);
//	w1->showWeaponInfo();
//	w2->showWeaponInfo();


	//单步攻击测试
//	w1->weaponSort();
//	w2->weaponSort();
//	w1->showWeaponInfo();
//	w2->showWeaponInfo();
//	
//	w1->showTestInfo(); 
//	w2->showTestInfo();
//	w1->attack(w2);
//	cout<<"attack time 1"<<endl;
//	w1->showTestInfo(); 
//	w2->showTestInfo(); 
//	w1->attack(w2);
//	cout<<"attack time 2"<<endl;
//	w1->showTestInfo(); 
//	w2->showTestInfo(); 
	
	//致死攻击测试
//	w1->weaponSort();
//	w2->weaponSort();
//	w1->showWeaponInfo();
//	w2->showWeaponInfo();
//	w1->showTestInfo(); 
//	w2->showTestInfo(); 
//	cout<<"before attack test"<<endl;
//	int ret1=0,ret2=0;
//	while(true){
//		ret1 = w1->attack(w2);
//		if(!w1->alive() || !w2->alive())	//出现死人的情况,则退出循环 
//			break;
//		if(ret1 && ret1)					//双方都没有武器,则退出循环 
//			break;
//			
//		ret2 = w2->attack(w1);
//		if(!w1->alive() || !w2->alive())	//出现死人的情况,则退出循环 
//			break;
//		if(ret1 && ret1)					//双方都没有武器,则退出循环 
//			break;
//	} 
//	w1->showWeaponInfo();
//	w2->showWeaponInfo();
//	w1->showTestInfo(); 
//	w2->showTestInfo(); 	



	//测试5 :子类龙的测试 
//	Dragon *w1 = new Dragon("red", 12, 500, 100, 3, true);
//	w1->showWeaponInfo();
//	w1->yelled();

	//测试6 :子类忍者的测试 
//	Dragon *w1 = new Dragon("red", 12, 500, 100, 3, true);
//	Warrior *w2 = new Ninja("blue", 12, 200, 200, 3, false);
//
//	w1->weaponSort();
//	w2->weaponSort();
//	
//	w1->showWeaponInfo();
//	w2->showWeaponInfo();
//	
//	w1->showTestInfo();
//	w2->showTestInfo();
//	
//	int ret1=0,ret2=0;
//	while(true){
//		ret2 = w2->attack(w1);
//		if(!w1->alive() || !w2->alive())	//出现死人的情况,则退出循环 
//			break;
//		if(ret1 && ret2)					//双方都没有武器,则退出循环 
//			break;
//		ret1 = w1->attack(w2);
//		if(!w1->alive() || !w2->alive())	//出现死人的情况,则退出循环 
//			break;
//		if(ret1 && ret2)					//双方都没有武器,则退出循环 
//			break;
//	} 
//	cout<<"after attack"<<endl;
//	w1->showTestInfo();
//	w2->showTestInfo();

	//测试7 :子类冰人的测试 
//	Warrior *w1 = new Iceman("red", 12, 500, 100, 7, true);
//	w1->march(10);
//	w1->march(10);
//	w1->march(10);
//	w1->march(10);
	
	//测试8 :子类狮子的测试 
//	Lion *w1 = new Lion("red", 12, 500, 100, 3, true, 100, 40);
//	for(int i=0; i<10; i++){
//		w1->march(10);
//		if(w1->loyalty <= 0)
//			w1->escape();
//	}

	//测试9 :子类狼的测试 
//	Wolf *w3 = new Wolf("red", 12, 500, 100, 7, true);
//	
//	w1->showWeaponInfo();
//	w2->showWeaponInfo();
//	w3->rob(w1);
//	w1->showWeaponInfo(); 
//	w3->rob(w2);
//	w2->showWeaponInfo(); 
}
#endif 

warrior.h

#ifndef __WARRIOR__H
#define __WARRIOR__H

#include <iostream>
#include <algorithm>
#include <vector>
#include "weapon.h"

using namespace std;
class Warrior;
class NormalCity;

//pair对象的first为武器编号,second为武器对象指针 
typedef pair<int,Weapon*>			WEAPON_PAIR;
typedef vector<WEAPON_PAIR> 		WEAPON_VEC;

//基类:武士 
class Warrior{
public:
	string name;	//武士名称			//"dragon", "ninja", "iceman", "lion", "wolf"
	string team;	//武士所属阵营 		//red,blue 
	int number;		//编号 				//按出生顺序编号 1-N 
	int elements;	//生命值
	int force;		//攻击力 
	int city;		//所在城市编号		//武士会路过不同的城市 
	int totalCities;//总城市数量 
	bool direction;	//行进方向 			//向东为true,向西为false
	WEAPON_VEC weaponVec;				//武士拥有的武器 
	WEAPON_VEC::iterator iweapon;	   	//轮流攻击时,依次遍历,指向下一个使用的武器 
public:
	Warrior(); 
	Warrior(string name, string team, int number, int elements, int force, int city, int totalCities, bool direction);
	virtual ~Warrior();
	int decElements(int force);				//受到攻击,减少生命值,若生命值已经不大于0,则返回-1 
	bool alive(void);						//返回武士是否存活的信息 
	virtual int march(NormalCity* iCity);	//前进,到下一个城市,参数是敌方基地的城市编号,否则不知道何时到达敌方基地
	void setCity(NormalCity* iCity);		//前进后,将自己的指针保存到城市对象中 
	virtual int attack(Warrior * enemy);	//攻击敌人
	void weaponSort(void);					//发动攻击前,对武器排序
	void getEnemyWeapon(Warrior * enemy);	//缴获敌人的武器
	bool checkWeaponNoForce(void);			//检查当前武器是不是无伤害,如果剑无伤害,则可能死循环 
	void showInfo(void);					//展示武器数量和血量 
	void showWeaponInfo(void);				//显示武器的具体信息,仅供调试使用
	virtual void showTestInfo(void);		//显示武士的具体信息,仅供调试使用
//	void printGameTime(int gameTime);		//打印当前游戏时间 
	void printBornInfo(void);				//打印出生信息 
};

//派生类:龙 
class Dragon : public Warrior{
public:
	Dragon(string team, int number, int elements, int force, int city, int totalCities, bool direction);
	~Dragon();
	void yelled(void);				//战胜后欢呼  
};

//派生类:忍者 
class Ninja : public Warrior{
public:
	Ninja(string team, int number, int elements, int force, int city, int totalCities, bool direction);
	~Ninja();
	int attack(Warrior * enemy);	//使用炸弹不受伤害 
};

//派生类:冰人 
class Iceman : public Warrior{
public:
	Iceman(string team, int number, int elements, int force, int city, int totalCities, bool direction);
	~Iceman();
	int march(NormalCity* iCity);
};

//派生类:狮子 
class Lion : public Warrior{
public:
	int loyalty;			//降生时的忠诚度 
	int loyaltyDec; 		//每次前进需要减少的忠诚度 
	Lion(string team, int number, int elements, int force, int city, int totalCities, bool direction, int loyalty, int loyaltyDec);
	~Lion();
	int march(NormalCity* iCity);
	void escape(void);		//狮子逃跑 
	void showTestInfo(void);
	void printLoyalty(void);
};

//派生类:狼
class Wolf : public Warrior{
public:
	Wolf(string team, int number, int elements, int force, int city, int totalCities, bool direction);
	~Wolf();
	int rob(Warrior * enemy);	//抢夺敌人最低编号的武器 
};

#endif

weapon.cpp

#include "weapon.h"
#define DEBUG 0

using namespace std;

//基类 
Weapon::Weapon(int number, int force){
	//判断初始化数据是否合理 
	if(number < 0 || number > 2)  	this->number = 0;
	else							this->number = number;
	if(force < 0)	this->force = 0;
	else			this->force = force;
}
int Weapon::resetForce(int force){
	if(force < 0)	this->force = 0;
	else			this->force = force;
	
}
int Weapon::useWeapon(void){return times;}				//不降低可使用次数 

//剑 
Sword::Sword(int force):Weapon(0, force){times = 1;}	//剑的可使用次数初始化为1

//炸弹 
Bomb::Bomb(int force):Weapon(1, force){times = 1;}		//炸弹的可使用次数初始化为1 
int Bomb::useWeapon(void){								//使用炸弹,降低可使用次数,返回剩余可使用次数 
	if(times > 0)		times--;
	return times;
}

//弓箭 
Arrow::Arrow(int force):Weapon(2, force){times = 2;}	//弓箭的可使用次数初始化为2
int Arrow::useWeapon(void){								//使用弓箭,降低可使用次数,返回剩余可使用次数	
	if(times > 0)		times--;
	return times;
} 

#if DEBUG 
int main(){
	//test1 测试构造函数 
	Sword b(35);
	Bomb  c(48);
	Arrow d(51);
	cout<<"b number="<<b.number<<",force="<<b.force<<",times="<<b.times<<endl;
	cout<<"c number="<<c.number<<",force="<<c.force<<",times="<<c.times<<endl;
	cout<<"d number="<<d.number<<",force="<<d.force<<",times="<<d.times<<endl;
	cout<<endl;

	//test2 测试其它函数
	b.resetForce(200);
	c.resetForce(300);
	d.resetForce(400);
	cout<<"b number="<<b.number<<",force="<<b.force<<",times="<<b.times<<endl;
	cout<<"c number="<<c.number<<",force="<<c.force<<",times="<<c.times<<endl;
	cout<<"d number="<<d.number<<",force="<<d.force<<",times="<<d.times<<endl;
	cout<<endl;
	
	int bt, ct, dt;
	bt = b.useWeapon();
	ct = c.useWeapon();
	dt = d.useWeapon();
	cout<<"b bt="<<bt<<",force="<<b.force<<",times="<<b.times<<endl;
	cout<<"c ct="<<ct<<",force="<<c.force<<",times="<<c.times<<endl;
	cout<<"d dt="<<dt<<",force="<<d.force<<",times="<<d.times<<endl;
	cout<<endl;
	bt = b.useWeapon();
	ct = c.useWeapon();
	dt = d.useWeapon();
	cout<<"b bt="<<bt<<",force="<<b.force<<",times="<<b.times<<endl;
	cout<<"c ct="<<ct<<",force="<<c.force<<",times="<<c.times<<endl;
	cout<<"d dt="<<dt<<",force="<<d.force<<",times="<<d.times<<endl;
}
#endif



weapon.h

#ifndef __WEAPON__H
#define __WEAPON__H

#include <iostream>

using namespace std;

//基类:武器
class Weapon{
public:
	int force;							//攻击力
	int times;							//剩余使用次数
	int number; 						//武器的编号 
public:
	Weapon(int number, int force);
	int resetForce(int force);			//武器更换主人,重新计算攻击力
	virtual int useWeapon(void);
};

//派生类:剑
class Sword : public Weapon{
public:	
	Sword(int force);
};

//派生类:炸弹 
class Bomb : public Weapon{
public: 
	Bomb(int force);
	int useWeapon(void);			//使用武器后,返回剩余可使用次数 
};

//派生类:弓箭 
class Arrow : public Weapon{
public: 	
	Arrow(int force);
	int useWeapon(void);			//使用武器后,返回剩余可使用次数 
};

#endif

猜你喜欢

转载自blog.csdn.net/whitefish520/article/details/103621080
今日推荐