coursera_poj_魔兽世界终结版

时间:2021-03-28 17:13:05

五个下午的时间!!!!终于过了!!
有史以来做的最复杂的一个题
这是我迄今为止做的最复杂也最具有挑战的一个oj作业。虽然之前做过比这个规模一些作业项目,但是往往有简单的模块框架,模块之前的关系也只是有些简单的联系,代码量虽然多,但是并不复杂。然而,然而这个题不仅有着复杂的逻辑关系,而且是oj测试啊!!意味着你需要实现所有特性,而且不能有一丁点的错误!
从决定开始做这个题,到最后两个平台AC,前前后后大概花了5个下午的时间。
在读完题后(相信我,为了搞懂每一个细节,题目我读了整整一个小时),coding前,大概花了两个到三个小时,画图、构思程序应该有着什么样的结构,都应该有哪些类,类之前的关系是什么样的,类中应该有哪些元素。。。。等等
不过即使想了这么长时间,coding途中也遇到不少问题导致最后的代码封装性差、结构也不是那么美好。。
coding时很多地方抽象得并不合理,为了少量修改目前已经完成的代码(不想去动大结构)而作出妥协。结果越到后面越发现整个程序非常混乱,各个类耦合度过高,程序的健壮性十分差。常常是为了一个新功能的实现,要引起三四五六处修改,一不注意就会留下bug。最终调试bug的时候,对于前期没构思好结构的后加的代码修改起来相当麻烦。
虽然我一直在注重代码的重用性,但是到最后我还是感觉代码的重用性不高,程序的结构还能优化的更好
做完了这个作业,让我感觉我就好像是重新学习了c艹,c艹的继承和多态、虚函数等一些特性,从认识到了解终于可以做到了熟悉掌握。
我要是有精力,一定要重构一下= =。。。
从一开始就认认真真的画类图,把游戏角色类(建筑、武士、武器)和游戏逻辑类、单元测试类的各自特征和功能梳理得更有条理,把整条游戏流程通过写伪代码的方式弄个清晰明了后再开始coding。。
总之,编码前的准备是否足够充分决定了你编码时和debug时的痛苦程度啊

错误

我认为魔兽世界终结版这题有错误!!

一开始我在poj上提交通过了,然后再从coursera上提交居然WA

我结合讨论区花雨同学的留言再测试了下数据。

poj上提供的27组测试数据中,在case 16:

006:00 blue dragon 7 born

Its morale is 11.62

我的输出数据(编译器vs2015)是:

006:00 blue dragon 7 born

Its morale is 11.63

经调试,我发现真正的正确答案应该就是11.63。

有人反映是编译器的问题,我测试了gcc的输出结果,同样是11.63

注意!即使我的输出结果和poj提供的out.txt文件不同,我的代码仍然是Accept!只是在coursera上WA。

然后,我修改了我的代码,不再进行四舍五入了,改为直接截尾保留两位小数。

这时,我的输出与out.txt相同了,在poj提交仍然Accept!

在coursera上提交也通过了!!

所以,我认为,有两个问题:

1.poj的评测有问题(不然我两次不同的代码输出不同的结果都能AC啊)

2.两个平台的测试数据都有问题(按照题目描述的四舍五入应该是11.63才对)

希望助教和老师能再审查一下题目啊,毕竟愿意做这个题的人不多,花出很大心血做完后却一直不能通过得多痛心啊!

AC代码:

本代码在vs上能编译,若想提交到oj上,需要改动一些地方

 // World of Warcraft.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#include <map>
#include <queue> using namespace std;
#define _CRT_SECURE_NO_WARNINGS
int M, N, R, K, T;//基础数据
int SoldierBlood[];
int SoldierAd[];
int CurrentTime; string toString(int t)
{
char buf[];
sprintf_s(buf, "%d", t);
string b = buf;
return b;
}
string printNowTime()
{
int t = CurrentTime;
int h = t / ;
int m = t % ;
string s;
s += h / + '';
s += (h / ) % + '';
s += h % + '';
s += ':';
s += m / + '';
s += m % + '';
return s + ' ';
} class Weapon
{
protected:
int Attack;
int Belong;//存放的soldier的uid
public:
int getAttack()
{
return Attack;
}
void setBelong(int t)
{
Belong = t;
}
virtual void init() = ;
virtual int lose() = ;//返回0的时候表示失去该武器
virtual int getType() = ;//0 sword 1 bomb 2 arrow
virtual string getPrint() = ;
virtual ~Weapon() {};
};
class Sword:public Weapon
{
public:
void init();
int lose()
{
Attack *= 0.8;
return Attack;
}
int getType()
{
return ;
}
void setAttack(int t)
{
Attack = t*0.2;
}
string getPrint()
{
return "sword(" + toString(Attack) + ')';
}
};
class Arrow :public Weapon
{
private: public:
int time;
void init()
{
Attack = ;
time = ;
}
int lose()
{
time--;
return time;
}
int getType()
{
return ;
}
string getPrint()
{
return "arrow(" + toString(time) + ')';
}
};
class Bomb :public Weapon
{
public:
void init()
{
Attack = ;
}
int lose()//只能用一次,只要调用了就没了
{
return ;
}
int getType()
{
return ;
}
string getPrint()
{
return "bomb";
}
}; class Soldier
{
protected: int Attack;
int Id;
int uId;
int Camp;//0:red,1:blue; public:
int Blood;//<=0即为死亡
int BloodWin;//
int isLive;//0 die 1 live -1 runaway
int tArrow;//实现同时射箭的处理。先记录下中箭的数值,在战争开始时再结算中箭的伤害。
Arrow* hasArrow;//没有即为NULL
Bomb* hasBomb;//
int dieByArrow;
string getCampName()
{
if (Camp == ) return "red";
return "blue";
}
string getPrint()//return like this "red lion 2"
{
string s;
s += getCampName() + ' ' + getName() + ' ' +toString(Id);
return s;
}
Soldier()
{
isLive = ;
hasArrow = NULL;
hasBomb = NULL;
tArrow = ;
BloodWin = ;
dieByArrow = ;
}
void attackByArrow(int t)
{
tArrow += t;
}
void loseByArrow()
{
loseBlood(tArrow);
tArrow = ;
}
void loseBlood(int t)//降低生命值
{
Blood -= t;
if (Blood <= )
{
isLive = ;
//Todo
die();
}
}
int getBlood()
{
return Blood;
}
int getAttack()
{
return Attack;
}
void setId(int t)
{
Id = t;
}
int getId()
{
return Id;
}
void setuId(int t)
{
uId = t;
}
int getuId()
{
return uId;
}
void setCamp(int t)
{
Camp = t;
}
int getCamp()
{
return Camp;
}
virtual void init() = ;//武士初始化,推迟到子类中实现
virtual int fight() = ;//返回攻击的攻击力
virtual int fightback() = ;//返回反击的攻击力
virtual int getType() = ;//返回该武士的类型编号,0: dragon 、1:ninja、2:iceman、3:lion、4:wolf
virtual string getName() = ;//返回该武士的类型
virtual void die() = ;//死亡
virtual void winFight(Soldier* p,int t) = ;//获胜
virtual void loseFight(Soldier* p,int t) = ;//输掉
virtual void drawFight(Soldier* p,int t) = ;//平局
virtual void loseWeapon(int t) = ;
virtual void printWeapon() = ;
virtual ~Soldier() {}//虚函数析构
}; int getSwordAttack(Weapon* p)//fight时处理sword的事件,返回sword的攻击力
{
if (!p) return ;
if (p->getType() != ) return ;//not sword,return 0
int t = p->getAttack();
//p->lose();//sword 变钝 // 对打时处理吧
//if (p->getAttack() == 0)
//{
// delete p;
// p = NULL;
//}
return t;
}
void getWeapon(Soldier * t,Weapon* &p, int code);//获得武器 class Dragon :public Soldier
{
private: public:
double Morale;
Weapon* mWeapon;//NULL代表没有武器
Dragon()
{
Blood = SoldierBlood[];
Attack = SoldierAd[];
mWeapon = NULL;
}
double getMorale()
{
return Morale;
}
int getWeaponType()//-1 no weapon,0 sword
{
if (mWeapon == NULL) return -;
return mWeapon->getType();
}
void init(); void die()
{
//if (mWeapon != NULL) delete mWeapon;
isLive = ;
Blood = ;
}
int getType()
{
return ;
}
string getName()
{
return "dragon";
}
void yell(int t);//judge whether to yell void winFight(Soldier* p,int t)
{
BloodWin += ;
Morale += 0.2;
yell(t);
} void loseFight(Soldier * p,int t)
{ }
void drawFight(Soldier * p,int t)
{
Morale -= 0.2;
yell(t);
}
int fight()
{
int ad = Attack;
ad += getSwordAttack(mWeapon);
return ad;
}
int fightback()
{
int ad = Attack / ;
ad += getSwordAttack(mWeapon);
return ad;
}
void loseWeapon(int t)//战斗后调用
{
if (mWeapon&&mWeapon->getType()==t)
{
if (mWeapon->lose() == )
{
//delete mWeapon;
mWeapon = NULL;
}
}
}
void printWeapon()
{
string s = printNowTime() + getPrint() + " has ";
if (mWeapon == NULL)
s += "no weapon";
else
s += mWeapon->getPrint();
puts(s.c_str());
}
};
class Ninja :public Soldier
{
private: public:
Weapon *mWeapon1, *mWeapon2;
Ninja()
{
Blood = SoldierBlood[];
Attack = SoldierAd[];
mWeapon1 = NULL;
mWeapon2 = NULL;
}
int getWeaponType1()
{
if (mWeapon1 == NULL) return -;
return mWeapon1->getType();
}
int getWeaponType2()
{
if (mWeapon2 == NULL) return -;
return mWeapon2->getType();
}
int getType()
{
return ;
}
string getName()
{
return "ninja";
}
void init()
{
getWeapon(this,mWeapon1, Id % );
if (mWeapon1) mWeapon1->setBelong(getuId());//设置属于谁的
getWeapon(this,mWeapon2, (Id+) % );
if (mWeapon2) mWeapon2->setBelong(getuId());//设置属于谁的
}
void die()
{
//if (mWeapon1) delete mWeapon1;
//if (mWeapon2) delete mWeapon2;
isLive = ;
}
void winFight(Soldier *p, int t)
{
BloodWin += ;
}
void loseFight(Soldier *p, int t)
{ }
void drawFight(Soldier *p, int t)
{ }
int fight()
{
int ad = Attack;
if(mWeapon1)ad += getSwordAttack(mWeapon1);
if(mWeapon2)ad += getSwordAttack(mWeapon2);
return ad;
}
void loseWeapon(int t)//战斗后调用
{
if (mWeapon1&&mWeapon1->getType()==t)
{
if (mWeapon1->lose()==)
{
//delete mWeapon1;
mWeapon1 = NULL;
}
}
if (mWeapon2&&mWeapon2->getType() == t)
{
if (mWeapon2->lose()==)
{
//delete mWeapon2;
mWeapon2 = NULL;
}
}
}
int fightback()//不反击,所以认为是0
{
return ;
}
void printWeapon()
{
string s = printNowTime() + getPrint() + " has ";
if (mWeapon1 == NULL && mWeapon2 == NULL)
s += "no weapon";
else if (mWeapon1 == NULL)
{
s += mWeapon2->getPrint();
}
else if (mWeapon2 == NULL)
{
s += mWeapon1->getPrint();
}
else
{
if (mWeapon1->getType() == )
s += mWeapon1->getPrint();
if (mWeapon2->getType() == )
s += mWeapon2->getPrint();
if (mWeapon1->getType() == )
{
if (s[s.size() - ] != ' ') s += ',';
s += mWeapon1->getPrint();
}
if (mWeapon2->getType() == )
{
if (s[s.size() - ] != ' ') s += ',';
s += mWeapon2->getPrint();
}
if (mWeapon1->getType() == )
{
if (s[s.size() - ] != ' ') s += ',';
s += mWeapon1->getPrint();
}
if (mWeapon2->getType() == )
{
if (s[s.size() - ] != ' ') s += ',';
s += mWeapon2->getPrint();
}
}
puts(s.c_str());
}
}; class Iceman :public Soldier
{
private: public:
Weapon *mWeapon;
int Step;
Iceman()
{
Blood = SoldierBlood[];
Attack = SoldierAd[];
mWeapon = NULL;
Step = ;
}
int getWeaponType()
{
if (mWeapon == NULL) return -;
return mWeapon->getType();
}
int getType()
{
return ;
}
string getName()
{
return "iceman";
}
void init()
{
getWeapon(this,mWeapon, Id % );
if (mWeapon) mWeapon->setBelong(getuId());//设置属于谁的
}
void die()
{
//if (mWeapon) delete mWeapon;
isLive = ;
}
void winFight(Soldier *p, int t)
{
BloodWin += ;
}
void loseFight(Soldier *p, int t)
{ }
void drawFight(Soldier *p, int t)
{ }
void loseWeapon(int t)//战斗后调用
{
if (mWeapon&&mWeapon->getType() == t)
{
if (mWeapon->lose()== )
{
//delete mWeapon;
mWeapon = NULL;
}
}
}
void Move()
{
Step++;
if (Step == )
{
Step = ;
Blood -= ;
Attack += ;
if (Blood <= ) Blood = ;
}
}
int fight()
{
int ad = Attack;
ad += getSwordAttack(mWeapon);
return ad;
}
int fightback()
{
int ad = Attack / ;
ad += getSwordAttack(mWeapon);
return ad;
}
void printWeapon()
{
string s = printNowTime() + getPrint() + " has ";
if (mWeapon == NULL)
s += "no weapon";
else
s += mWeapon->getPrint();
puts(s.c_str());
}
};
class Lion :public Soldier
{
private:
int Loyalty;
int tBlood;//要转移的Blood
public:
Lion()
{
Blood = SoldierBlood[];
Attack = SoldierAd[];
}
void moveBlood()
{
tBlood = Blood-tArrow;
}
void init();
void die()
{
isLive = ;
}
int getLoyalty()
{
return Loyalty;
}
void loseLoyalty()
{
Loyalty -= K;
}
int fight()
{
return Attack;
}
int fightback()
{
return Attack / ;
}
void winFight(Soldier *p, int t)
{
BloodWin += ;
}
void loseFight(Soldier *p, int t)
{
if(dieByArrow==)p->Blood += tBlood;
}
void drawFight(Soldier *p, int t)
{
loseLoyalty();
}
void loseWeapon(int t)
{ }
int getType()
{
return ;
}
string getName()
{
return "lion";
}
void runAway()
{
Blood = ;
isLive = ;//暂时先设置为0吧,逃跑了就当他死了
}
void printWeapon()
{
string s = printNowTime() + getPrint() + " has ";
s += "no weapon";
puts(s.c_str());
}
};
class Wolf :public Soldier
{
private: public:
//Weapon* mWeapon;
Weapon* swordWeapon;
Arrow* arrowWeapon;
Bomb* bombWeapon;
Wolf()
{
Blood = SoldierBlood[];
Attack = SoldierAd[];
}
/*int getWeaponType()
{
return WeaponType;
}*/
void init()
{
swordWeapon = NULL;
arrowWeapon = NULL;
bombWeapon = NULL;
}
void die()
{
//if (swordWeapon) delete swordWeapon;
//if ()
isLive = ;
}
void winFight(Soldier *p, int t)
{
switch (p->getType())
{
case :
{
takeWeapon(((Dragon *)p)->mWeapon);
break;
}
case :
{
takeWeapon(((Ninja *)p)->mWeapon1);
takeWeapon(((Ninja *)p)->mWeapon2);
break;
}
case :
{
takeWeapon(((Iceman *)p)->mWeapon);
break;
}
case :
{
takeWeapon(((Wolf *)p)->swordWeapon);
takeWeapon(((Wolf *)p)->bombWeapon);
takeWeapon(((Wolf *)p)->arrowWeapon);
break;
}
}
BloodWin += ;
}
void loseFight(Soldier *p, int t)
{ }
void drawFight(Soldier *p, int t)
{ }
void takeWeapon(Weapon *p)//缴获武器
{
if (p == NULL) return;
if (p->getType() == &&swordWeapon==NULL)
{
swordWeapon = p;
}
if (p->getType() == && bombWeapon == NULL)
{
bombWeapon =(Bomb *) p;
hasBomb = (Bomb *)p;
}
if (p->getType() == && arrowWeapon == NULL)
{
arrowWeapon = (Arrow *)p;
hasArrow = (Arrow *)p;
}
}
int fight()
{
int ad = Attack;
ad += getSwordAttack(swordWeapon);
return ad;
}
int fightback()
{
int ad = Attack / ;
ad += getSwordAttack(swordWeapon);
return ad;
}
void loseWeapon(int t)//战斗后调用
{
if (swordWeapon&&t==)
{
if (swordWeapon->lose() == )
{
//delete swordWeapon;
swordWeapon = NULL;
}
}
if (bombWeapon&&t == )
{
if (bombWeapon->lose() == )
{
//delete bombWeapon;
bombWeapon = NULL;
}
}
if (arrowWeapon&&t == )
{
if (arrowWeapon->lose() == )
{
//delete arrowWeapon;
arrowWeapon = NULL;
}
}
}
int getType()
{
return ;
}
string getName()
{
return "wolf";
}
void printWeapon()
{
string s = printNowTime() + getPrint() + " has ";
if (swordWeapon == NULL && bombWeapon == NULL && arrowWeapon ==NULL)
s += "no weapon";
else
{
if (arrowWeapon != NULL) s += arrowWeapon->getPrint();
if (bombWeapon != NULL)
{
if (s[s.size() - ] != ' ') s += ',';
s += bombWeapon->getPrint();
}
if (swordWeapon != NULL)
{
if (s[s.size() - ] != ' ') s += ',';
s += swordWeapon->getPrint();
}
}
puts(s.c_str());
}
};
vector<Soldier *> mAllSoldier;//全部武士集合 class Base
{
public:
int Blood;
int BloodWin;
int SoidierNum;//从1开始编号
vector<Soldier*> mSoldier;//武士集合
int occupied;//0 no,1
void init()//初始化大本营
{
Blood = M;
SoidierNum = ;
/*for (int i = 0; i <= mSoldier.size(); i++)
{
delete mSoldier[i];
}*/
mSoldier.clear();
//mSoldier.push_back(NULL);
occupied = ;
}
void tackBloodWin()
{
Blood += BloodWin;
BloodWin = ;
}
virtual int creatwho(int t) = ;
virtual int getCamp() = ;
virtual string getCampName() = ;
Soldier* CreatSoldier();//返回指向soldier的指针,创建失败的时候的返回null
}; class RedBase :public Base
{
public:
int creatwho(int t)
{
return t % ;
}
int getCamp()
{
return ;
}
string getCampName()
{
return "red";
}
};
RedBase *RedBaseInstance;//红魔军实例
class BlueBase:public Base
{
public:
int creatwho(int t)
{
int k = t % ;
switch (k)
{
case :
return ;
case :
return ;
case :
return ;
case :
return ;
case :
return ;
}
}
int getCamp()
{
return ;
}
string getCampName()
{
return "blue";
}
};
BlueBase *BlueBaseInstance;//蓝魔军实例
class City
{
private: public:
int Id;
Soldier* RedSoldier;
Soldier* BlueSoldier;
int Blood;
int whoLastWin;//0 no 1 red 2 blue
int Flag;//as up
int FlagRaise;
int lastPrintFlag;
int whoFirst;//who first to attack,0 red,1 blue
int redReward, blueReward;
queue<string> output;
City(int t)
{
lastPrintFlag = ;
Id = t;
Blood = ;
whoLastWin = ;
Flag = ;
redReward = ;
blueReward = ;
FlagRaise = ;
RedSoldier = NULL;
BlueSoldier = NULL;
while (!output.empty()) output.pop();
}
void addBlood()
{
Blood += ;
}
int takeBlood(int who)//拿走所有的生命元
{
int t = Blood;
Blood = ;
string s = printNowTime();
if (who == )
{
s += RedSoldier->getPrint();
}
if (who == )
{
s += BlueSoldier->getPrint();
}
s += " earned " +toString(t)+ " elements for his headquarter";
puts(s.c_str());
return t;
}
int takeWinBlood(int c, int who)
{
int t = Blood;
Blood = ;
string s = printNowTime();
if (who == )
{
s += RedSoldier->getPrint();
}
if (who == )
{
s += BlueSoldier->getPrint();
}
s += " earned " + toString(t) + " elements for his headquarter";
//puts(s.c_str());
output.push(s);
return t;
}
void printFlag(int p)
{
string s=printNowTime();
if (Flag == )
{
s += "red flag raised in city " + toString(p);
}
else
{
s += "blue flag raised in city " + toString(p);
}
puts(s.c_str());
}
void changeFlag(int p,int t)//t 1 red win ,t 2 blue win
{
//if (Flag == t) return;
if (t == )
{
if (whoLastWin == )
{
Flag = ;
FlagRaise = ;
//printFlag(p);
whoFirst = ;
}
else
{
whoLastWin = ;
}
}
else
{
if (whoLastWin == )
{
Flag = ;
FlagRaise = ;
//printFlag(p);
whoFirst = ;
}
else
{
whoLastWin = ;
}
}
} };
vector <City*> mAllCity; void Init()//游戏初始化
{
cin >> M >> N >> R >> K >> T;
cin >> SoldierBlood[] >> SoldierBlood[] >> SoldierBlood[] >> SoldierBlood[] >> SoldierBlood[];
cin >> SoldierAd[] >> SoldierAd[] >> SoldierAd[] >> SoldierAd[] >> SoldierAd[]; N++;//使得编号N为bluebase for (int i = ; i < mAllSoldier.size(); i++)
{
delete mAllSoldier[i];
}
mAllSoldier.clear();
//mAllSoldier.push_back(NULL);//放入一个空指针占位 RedBaseInstance = new RedBase();
RedBaseInstance->init();
BlueBaseInstance = new BlueBase();
BlueBaseInstance->init(); for (int i = ; i < mAllCity.size(); i++)
{
delete mAllCity[i];
}
mAllCity.clear();
for (int i = ; i <= N; i++)//0号city为redbase,N号为bluebase
{ mAllCity.push_back(new City(i));
mAllCity[i]->whoFirst = (i+) % ;
}
} string printTime()
{
int t = CurrentTime;
int h = t / ;
int m = t % ;
string s;
s += h / + '';
s += (h / ) % + '';
s += h % + '';
s += ':';
s += m / + '';
s += m % + '';
return s;
} void printBorn(string s, Soldier* p)
{
if (p == NULL) return;//表示没有创建
s += ' ' + p->getPrint() + " born";
puts(s.c_str());
if (p->getType() == )//is dragon
{
double t = ((Dragon*)(p))->getMorale();
//t = (double)((int)(t * 100 + 0.5)) / 100.0;//四舍五入
//oj上的测试数据并没有四舍五入。。。直接截尾取得两位有效数字,坑啊
printf("Its morale is %.2lf\n", t);
}
if (p->getType() == )//is lion
{
printf("Its loyalty is %d\n", ((Lion*)(p))->getLoyalty());
}
}
void printLionRun(string s, Lion* p)//判断lion是否逃跑以及相应信息的输出
{
p->runAway();
s += " lion " + toString(p->getId()) + " ran away";
puts(s.c_str());
}
int printMove(int c)
{
Soldier *p = NULL;
string s;
int f = ; p = mAllCity[c]->RedSoldier;//red
if (p)
{
s = printNowTime() + p->getPrint();
if (c == )
s += " reached red headquarter";
else if (c == N) s += " reached blue headquarter";
else s += " marched to city " + toString(c);
s += " with " + toString(p->getBlood()) + " elements and force " + toString(p->getAttack());
puts(s.c_str()); if (BlueBaseInstance->occupied==&&c == N)
{
f = ;
printf("%sblue headquarter was taken\n", printNowTime().c_str());
}
} p = mAllCity[c]->BlueSoldier;//blue
if (p)
{
s = printNowTime() + p->getPrint();
if (c == )
s += " reached red headquarter";
else if (c == N) s += " reached blue headquarter";
else s += " marched to city " + toString(c);
s += " with " + toString(p->getBlood()) + " elements and force " + toString(p->getAttack());
puts(s.c_str()); if (RedBaseInstance->occupied==&&c == )
{
f = ;
printf("%sred headquarter was taken\n", printNowTime().c_str());
}
} return f;
}
int checkBomb(Soldier *p,Soldier *q,int t)//返回1即使需要使用bomb
{
//t 谁先攻击,这个应该在city中处理,0 red 1 blue
if (q->getBlood() - q->tArrow <= ) return ;
if (p->getBlood() - p->tArrow <= ) return ;
int a = , b = ,c = ,d = ;
if (t == )//p 先攻击
{
a = p->fight();
b = q->getBlood() - q->tArrow; if (a < b)//q 没死 反击
{
c = q->fightback();
d = p->getBlood() - p->tArrow;
if (c >= d) return ;
}
}
else//q 先攻击
{
a = q->fight();
b = p->getBlood() - p->tArrow; if (a >= b) return ;//q 打死了 p就返回1
}
return ;
}
int checkBomb2(Soldier *p, Soldier *q, int t)
{
//t 谁先攻击,这个应该在city中处理,0 red 1 blue
if (q->getBlood() - q->tArrow <= ) return ;
if (p->getBlood() - p->tArrow <= ) return ;
int a = , b = , c = , d = ;
if (t == )
{
a = p->fight();
b = q->getBlood() - q->tArrow;
if (a >= b) return ;
}
else
{
c = q->fight();
d = p->getBlood() - p->tArrow;
if (c < d)
{
a = p->fightback();
b = q->getBlood() - q->tArrow;
if (a >= b) return ;
}
}
return ;
}
int checkArrow(Soldier* p)//结算arrow的伤害并检查是否被射死了,返回1是被射死了
{
if (!p) return -;
p->loseBlood(p->tArrow);
p->tArrow = ;
if (p->getBlood() <= )
{
p->dieByArrow = ;
p->die();
return ;
}
return ;
}
int printFight(int i,Soldier *p,Soldier *q)//返回0,就是q死了
{
int a = p->fight();
int b = q->getBlood();
p->loseWeapon();//武器损耗
q->loseBlood(a);
string s = printTime();
string sp, sq;
if (p->getCamp() == ) sp = " red ";
else sp = " blue ";
if (q->getCamp() == ) sq = " red ";
else sq = " blue ";
s += sp + p->getName() + ' ' + toString(p->getId()) + " attacked" + sq;
s += q->getName() + ' ' + toString(q->getId()) + " in city " + toString(i);
s += " with " + toString(p->getBlood()) + " elements and force " + toString(p->getAttack());
//puts(s.c_str());
mAllCity[i]->output.push(s);
return q->isLive;
}
int printFightBack(int i, Soldier *p, Soldier *q)
{
if (p->getType() == )//ninjia no fightback
return ;
int a = p->fightback();
int b = q->getBlood();
p->loseWeapon();
q->loseBlood(a);
string s = printNowTime();
s += p->getPrint() + " fought back against " + q->getPrint() + " in city " + toString(i);
//puts(s.c_str());
mAllCity[i]->output.push(s);
return q->isLive;
}
void printKilled(int i,Soldier *p)
{
string s = printTime() + ' ' + p->getCampName() + ' ';
s += p->getName() + ' ' + toString(p->getId()) + " was killed in city " + toString(i);
//puts(s.c_str());
mAllCity[i]->output.push(s);
}
void afterbattle(int i, int t)//在city i 战斗结束后的事情,0 平局 1 red win 2 blue win
{
if (t == )//平局
{
mAllCity[i]->RedSoldier->drawFight(mAllCity[i]->BlueSoldier, i);
mAllCity[i]->BlueSoldier->drawFight(mAllCity[i]->RedSoldier, i);
mAllCity[i]->whoLastWin = ;
}
if (t == )//red win
{ mAllCity[i]->RedSoldier->winFight(mAllCity[i]->BlueSoldier, i);
mAllCity[i]->BlueSoldier->loseFight(mAllCity[i]->RedSoldier, i);
RedBaseInstance->BloodWin += mAllCity[i]->takeWinBlood(i, );
mAllCity[i]->changeFlag(i,);
}
if (t == )//blue win
{ mAllCity[i]->RedSoldier->loseFight(mAllCity[i]->BlueSoldier, i);
mAllCity[i]->BlueSoldier->winFight(mAllCity[i]->RedSoldier, i);
BlueBaseInstance->BloodWin += mAllCity[i]->takeWinBlood(i, );
mAllCity[i]->changeFlag(i,);
}
}
void redReward()
{
for (int i = N - ; i > ; i--)//red reward
{
if (!mAllCity[i]->RedSoldier) continue;
while (mAllCity[i]->RedSoldier->BloodWin>)
if (RedBaseInstance->Blood >= )
{
RedBaseInstance->Blood -= ;
mAllCity[i]->RedSoldier->Blood += ;
mAllCity[i]->RedSoldier->BloodWin -= ;
mAllCity[i]->redReward = ;
}
//mAllCity[i]->RedSoldier->BloodWin = 0;
}
}
void blueReward()
{
for (int i = ; i < N; i++)//blue reward
{
if (!mAllCity[i]->BlueSoldier) continue;
while (mAllCity[i]->BlueSoldier->BloodWin>)
if (BlueBaseInstance->Blood >= )
{
BlueBaseInstance->Blood -= ;
mAllCity[i]->BlueSoldier->Blood += ;
mAllCity[i]->BlueSoldier->BloodWin -= ;
mAllCity[i]->blueReward = ;
}
//mAllCity[i]->BlueSoldier->BloodWin = 0;
}
} void Game()//游戏进程
{
CurrentTime = -;
Soldier *RedOccSoldier = NULL;
Soldier *BlueOccSoldier = NULL;
while (CurrentTime <= T)
{
CurrentTime++;
if (CurrentTime > T) return;
int CurrentMinute = CurrentTime % ;
string st = printTime();
switch (CurrentMinute)
{
case :
{
string s = st;
//red
Soldier* p = RedBaseInstance->CreatSoldier();
printBorn(s, p); //blue
p = BlueBaseInstance->CreatSoldier();
printBorn(s, p); break;
}
case :
{
for (int i = ; i <= N; i++)//从西向东
{
//red
if (mAllCity[i]->RedSoldier&&mAllCity[i]->RedSoldier->getType() == )//is lion
if (i != N)//not at bluebase
{
Lion *p = (Lion *)mAllCity[i]->RedSoldier;
string s = st;
s += " red";
if (p->getLoyalty() <= )
{
printLionRun(s, p);
mAllCity[i]->RedSoldier = NULL;
}
} //blue
if (mAllCity[i]->BlueSoldier&&mAllCity[i]->BlueSoldier->getType() == )//is lion
if (i != )//not at redbase
{
Lion *p = (Lion *)mAllCity[i]->BlueSoldier;
string s = st;
s += " blue";
if (p->getLoyalty() <= )
{
printLionRun(s, p);
mAllCity[i]->BlueSoldier = NULL;
}
}
}
break;
}
case :
{
int f = ;//1即为占领了 //red
for (int i = N; i > ; i--)
{
if (i == N && mAllCity[N]->RedSoldier) RedOccSoldier = mAllCity[N]->RedSoldier;
mAllCity[i]->RedSoldier = mAllCity[i - ]->RedSoldier;
if (mAllCity[i]->RedSoldier&&mAllCity[i]->RedSoldier->getType() == )
{
((Iceman*)mAllCity[i]->RedSoldier)->Move();
}
}
mAllCity[]->RedSoldier = NULL;
if (mAllCity[N]->RedSoldier) { BlueBaseInstance->occupied++; } //blue
for (int i = ; i < N; i++)
{
if (i == && mAllCity[]->BlueSoldier) BlueOccSoldier = mAllCity[]->BlueSoldier;
mAllCity[i]->BlueSoldier = mAllCity[i + ]->BlueSoldier;
if (mAllCity[i]->BlueSoldier&&mAllCity[i]->BlueSoldier->getType() == )
{
((Iceman*)mAllCity[i]->BlueSoldier)->Move();
}
}
mAllCity[N]->BlueSoldier = NULL;
if (mAllCity[]->BlueSoldier) RedBaseInstance->occupied++;
for (int i = ; i <= N; i++)
{
if (printMove(i)) f = ;
} //be occupied?
if (f) return;
break;
}
case :
{
for (int i = ; i < N; i++)//不包括红蓝司令部
{
mAllCity[i]->addBlood();
}
/*RedBaseInstance->Blood += 10;
BlueBaseInstance->Blood += 10;*/
break;
}
case :
{
for (int i = ; i < N; i++)
{
if (mAllCity[i]->RedSoldier != NULL&&mAllCity[i]->BlueSoldier == NULL)
RedBaseInstance->Blood += mAllCity[i]->takeBlood();
if (mAllCity[i]->RedSoldier == NULL&&mAllCity[i]->BlueSoldier != NULL)
BlueBaseInstance->Blood += mAllCity[i]->takeBlood();
}
break;
}
case :
{
//red
for (int i = ; i < N; i++)
{
Soldier *p = mAllCity[i]->RedSoldier;
//if (!p) continue;
if (i < N-)
{
if (p&&p->hasArrow)
{
Arrow *q = p->hasArrow;
if (mAllCity[i + ]->BlueSoldier)
{
mAllCity[i + ]->BlueSoldier->tArrow += R; if (q->lose() == )//返回值为0说明arrow已经消失
{
//delete q;
int t = p->getType();
switch (t)
{
case :
{
((Dragon *)p)->mWeapon = NULL;
break;
}
case :
{
if (((Ninja *)p)->mWeapon1 && ((Ninja *)p)->mWeapon1->getType() == ) ((Ninja *)p)->mWeapon1 = NULL;
if (((Ninja *)p)->mWeapon2 && ((Ninja *)p)->mWeapon2->getType() == ) ((Ninja *)p)->mWeapon2 = NULL;
break;
}
case :
{
((Iceman *)p)->mWeapon = NULL;
break;
}
case :
{
((Wolf *)p)->arrowWeapon = NULL;
break;
}
}
p->hasArrow = NULL;
}
string s = st + " red ";
s += mAllCity[i]->RedSoldier->getName() + ' ' + toString(mAllCity[i]->RedSoldier->getId()) + " shot";
if (mAllCity[i + ]->BlueSoldier&&mAllCity[i + ]->BlueSoldier->getBlood() <= R)
s += " and killed blue " + mAllCity[i + ]->BlueSoldier->getName() + ' ' + toString(mAllCity[i + ]->BlueSoldier->getId());
puts(s.c_str());
}
}
}
//blue
if (i > )
{
Soldier *p = mAllCity[i]->BlueSoldier;
//if (!p) continue;
if (p&&p->hasArrow)
{
Arrow *q = p->hasArrow;
if (mAllCity[i - ]->RedSoldier)
{
mAllCity[i - ]->RedSoldier->tArrow += R;
if (q->lose() == )//返回值为0说明arrow已经消失
{
//delete q;
int t = p->getType();
switch (t)
{
case :
{
((Dragon *)p)->mWeapon = NULL;
break;
}
case :
{
if (((Ninja *)p)->mWeapon1 && ((Ninja *)p)->mWeapon1->getType() == ) ((Ninja *)p)->mWeapon1 = NULL;
if (((Ninja *)p)->mWeapon2 && ((Ninja *)p)->mWeapon2->getType() == ) ((Ninja *)p)->mWeapon2 = NULL;
break;
}
case :
{
((Iceman *)p)->mWeapon = NULL;
break;
}
case :
{
((Wolf *)p)->arrowWeapon = NULL;
break;
}
}
p->hasArrow = NULL;
}
string s = st + " blue ";
s += mAllCity[i]->BlueSoldier->getName() + ' ' + toString(mAllCity[i]->BlueSoldier->getId()) + " shot";
if (mAllCity[i - ]->RedSoldier&&mAllCity[i - ]->RedSoldier->getBlood() <= R)
s += " and killed red " + mAllCity[i - ]->RedSoldier->getName() + ' ' + toString(mAllCity[i - ]->RedSoldier->getId());
puts(s.c_str());
}
}
}
} break;
}
case :
{
for (int i = ; i < N; i++)
{
string s = st;
Soldier *p = mAllCity[i]->RedSoldier;
if (p&&p->hasBomb)
{
if (mAllCity[i]->BlueSoldier&&checkBomb(p,mAllCity[i]->BlueSoldier,mAllCity[i]->whoFirst))
{
p->loseWeapon();
s = st;
s += " red "+p->getName()+' '+toString(p->getId())+" used a bomb and killed blue ";
s += mAllCity[i]->BlueSoldier->getName()+' '+toString(mAllCity[i]->BlueSoldier->getId());
puts(s.c_str());
p->die();
mAllCity[i]->BlueSoldier->die();
mAllCity[i]->RedSoldier = NULL;
mAllCity[i]->BlueSoldier = NULL;
}
} s = st;
p = mAllCity[i]->BlueSoldier;
if (p&&p->hasBomb)
{
if (mAllCity[i]->RedSoldier&&checkBomb2(mAllCity[i]->RedSoldier,p, mAllCity[i]->whoFirst))
{
p->loseWeapon();
s = st;
s += " blue " + p->getName() + ' ' + toString(p->getId()) + " used a bomb and killed red ";
s += mAllCity[i]->RedSoldier->getName() + ' ' + toString(mAllCity[i]->RedSoldier->getId());
puts(s.c_str());
p->die();
mAllCity[i]->RedSoldier->die();
mAllCity[i]->RedSoldier = NULL;
mAllCity[i]->BlueSoldier = NULL;
}
} }
break;
}
case :
{
for (int i = ; i < N; i++)//记录lion战斗前的blood
{
if (mAllCity[i]->RedSoldier&&mAllCity[i]->RedSoldier->getType() == )
((Lion *)mAllCity[i]->RedSoldier)->moveBlood();
if (mAllCity[i]->BlueSoldier&&mAllCity[i]->BlueSoldier->getType() == )
((Lion *)mAllCity[i]->BlueSoldier)->moveBlood();
}
for (int i = ; i < N; i++)
{
//先处理arrow事件
int r = checkArrow(mAllCity[i]->RedSoldier);
int b = checkArrow(mAllCity[i]->BlueSoldier);
if (r == - || b == -)
{
if (r==) mAllCity[i]->RedSoldier = NULL;
if (b==) mAllCity[i]->BlueSoldier = NULL;
continue;//no fight
}
if (r == && b == ) { mAllCity[i]->RedSoldier = NULL; mAllCity[i]->BlueSoldier = NULL; }//all die,no fight
if (r == && b == ) { afterbattle(i, ); mAllCity[i]->BlueSoldier = NULL; redReward(); }//blue die,red win
if (r == && b == ) { afterbattle(i, ); mAllCity[i]->RedSoldier = NULL; blueReward(); }//blue win if (r == && b == )//all not die,begin to fight
{
int t = mAllCity[i]->whoFirst;
if (t == )//red attack,blue fightback
{
int tmp = printFight(i, mAllCity[i]->RedSoldier, mAllCity[i]->BlueSoldier);
if (tmp)//没杀死,等待反击
{
int tmp2=printFightBack(i, mAllCity[i]->BlueSoldier, mAllCity[i]->RedSoldier);
if (tmp2)//反击没死,平局
{
afterbattle(i, );
}
else// blue win
{
printKilled(i, mAllCity[i]->RedSoldier);
afterbattle(i, );
mAllCity[i]->RedSoldier = NULL;
}
}
else//杀死了,red win
{
printKilled(i, mAllCity[i]->BlueSoldier);
afterbattle(i, );
mAllCity[i]->BlueSoldier = NULL;
}
}
else
{
int tmp = printFight(i, mAllCity[i]->BlueSoldier, mAllCity[i]->RedSoldier);
if (tmp)//没杀死,等待反击
{
int tmp2 = printFightBack(i, mAllCity[i]->RedSoldier, mAllCity[i]->BlueSoldier);
if (tmp2)//反击没死,平局
{
afterbattle(i, );
}
else// red win
{
printKilled(i, mAllCity[i]->BlueSoldier);
afterbattle(i, );
mAllCity[i]->BlueSoldier = NULL;
}
}
else//杀死了,blue win
{
printKilled(i, mAllCity[i]->RedSoldier);
afterbattle(i, );
mAllCity[i]->RedSoldier = NULL;
}
}
}
}
redReward();
blueReward();
RedBaseInstance->tackBloodWin();
BlueBaseInstance->tackBloodWin();
for (int i = ; i < N; i++)//output
{
while (!mAllCity[i]->output.empty())
{
puts(mAllCity[i]->output.front().c_str());
mAllCity[i]->output.pop();
}
/*if (mAllCity[i]->redReward)
{
mAllCity[i]->redReward = 0;
string s = printNowTime() + mAllCity[i]->RedSoldier->getPrint() + " earned 8 elements for his headquarter";
puts(s.c_str());
}
if (mAllCity[i]->blueReward)
{
mAllCity[i]->blueReward = 0;
string s = printNowTime() + mAllCity[i]->BlueSoldier->getPrint() + " earned 8 elements for his headquarter";
puts(s.c_str());
}*/
if (mAllCity[i]->FlagRaise == && mAllCity[i]->lastPrintFlag!=)
{
mAllCity[i]->FlagRaise = ;
mAllCity[i]->lastPrintFlag = ;
string s = printNowTime() + "red flag raised in city " + toString(i);
puts(s.c_str());
}
if (mAllCity[i]->FlagRaise == && mAllCity[i]->lastPrintFlag!=)
{
mAllCity[i]->FlagRaise = ;
mAllCity[i]->lastPrintFlag = ;
string s = printNowTime() + "blue flag raised in city " + toString(i);
puts(s.c_str());
}
}
break;
}
case :
{
string s = printNowTime() + toString(RedBaseInstance->Blood) + " elements in red headquarter";
puts(s.c_str());
s = printNowTime() + toString(BlueBaseInstance->Blood) + " elements in blue headquarter";
puts(s.c_str());
break;
}
case :
{
for (int i = ; i <= N; i++)
{
if (mAllCity[i]->RedSoldier)
{
mAllCity[i]->RedSoldier->printWeapon();
}
}
if (RedOccSoldier) RedOccSoldier->printWeapon();
if (BlueOccSoldier) BlueOccSoldier->printWeapon();
for (int i = ; i <= N; i++)
{
if (mAllCity[i]->BlueSoldier)
{
mAllCity[i]->BlueSoldier->printWeapon();
}
}
break;
}
}
}
}
int main()
{
int t;
cin >> t;
int c = ;
while (t--)
{
cout << "Case " << ++c << ":" << endl;
Init();
Game();
}
system("pause");
return ;
} void Sword::init()
{
//Attack = mAllSoldier[Belong]->getAttack()*0.2;
}
void Lion::init()
{
if (getCamp() == ) {
Loyalty = RedBaseInstance->Blood;
}
else {
Loyalty = BlueBaseInstance->Blood;
}
}
void getWeapon(Soldier * t,Weapon* &p, int code)
{
switch (code)
{
case :
{
p = new Sword();
((Sword *)p)->init();
((Sword *)p)->setAttack(t->getAttack());
if (p->getAttack() == )
{
delete p;
p = NULL;
}
break;
}
case :
{
p = new Bomb();
((Bomb *)p)->init();
t->hasBomb = ((Bomb *)p);
break;
}
case :
{
p = new Arrow();
((Arrow *)p)->init();
t->hasArrow = ((Arrow *)p);
break;
}
}
//p->setBelong(getuId());//设置属于谁的
}
void Dragon::init()
{
getWeapon(this,mWeapon, Id % );
if(mWeapon) mWeapon->setBelong(getuId());//设置属于谁的
if (Camp == )//3.初始化morale
{
Morale = (double)RedBaseInstance->Blood / (double)SoldierBlood[];
}
else
{
Morale = (double)BlueBaseInstance->Blood / (double)SoldierBlood[];
}
}
Soldier* Base::CreatSoldier()
{
Soldier *tmp=NULL;
switch (creatwho(SoidierNum))
{
case ://iceman
tmp = new Iceman();
break;
case ://lion
tmp = new Lion();
break;
case ://wolf
tmp = new Wolf();
break;
case ://ninja
tmp = new Ninja();
break;
case ://dragon
tmp = new Dragon();
break;
}
if (tmp->getBlood() <= Blood)//可以制造出生命值为 m 的武士
{
Blood -= tmp->getBlood();
SoidierNum++;
tmp->setCamp(getCamp());
tmp->setId(SoidierNum);
tmp->init();
mSoldier.push_back(tmp);
mAllSoldier.push_back(tmp); if (getCamp() == )
{
mAllCity[]->RedSoldier = tmp;
}
else
{
mAllCity[N]->BlueSoldier = tmp;
}
return tmp;
}
else
{
delete tmp;//
return NULL;
}
} void Dragon::yell(int t)//judge whether to yell
{
if (mAllCity[t]->whoFirst == Camp)//检测是否先攻击
{
if (Morale > 0.8)
{
string s = printTime();
if (Camp == ) s += " red ";
if (Camp == ) s += " blue ";
s += getName() + ' ' + toString(getId())+" yelled in city "+toString(t);
mAllCity[t]->output.push(s);
//puts(s.c_str());
}
}
}