ouc老师太闲了,搞了几天晚上
#include<iostream>
#include<cstring>
#include<typeinfo>
#include<cstdio>
#include<iomanip>
#include<vector>
#include<deque>
#include<string>
using namespace std;
class Tank;
char tank_[4][20] = { "Bear", "Phantom", "Prism", "Apocalypse" };
int pro_time[4] = { 1,2,4,6 };
int pro_cost[4] = { 600,1000,1200,1750 };
int pro_blood[4] = { 100,100,100,120 };
int my_espionage;//我的间谍星级
int my_spy_way[3] = { 40,20,5 };
int my_spy_time;
deque<string> emny;
deque<Tank*>s_emny;
int my_star = 0;//是否升星
int w_l_flag = 0;//结束标志位
class Tank {
public:
string name;
int id;
int Attack, defense, blood_volume;
virtual ~Tank() {};
friend class battlefield;
bool is_in_home = true;
bool islast = false;
int star;//是否升星
int back_time;
};
class Bear :Tank {
public:
Bear() :Tank() {
name = "Bear";
id = 0;
Attack = 30;
defense = 15;
blood_volume = 100;
star = 0;
back_time = 0;
}
~Bear() {};
friend class battlefield;
};
class Phantom :Tank {
public:
int Hit_time;
Phantom() :Tank() {
name = "Phantom";
id = 1;
Attack = 40;
defense = 15;
blood_volume = 100;
Hit_time = 0;//单数时可以免疫
star = 0;
back_time = 0;
}
~Phantom() {};
friend class battlefield;
};
class Prism :Tank {
public:
int Light_Attack;
Prism() :Tank() {
name = "Prism";
id = 2;
Attack = 0;
defense = 10;
blood_volume = 100;
Light_Attack = 50;
star = 0;
back_time = 0;
}
~Prism() {};
friend class battlefield;
};
class Apocalypse :Tank {
public:
double Damage_reduction;
Apocalypse() :Tank() {
name = "Apocalypse";
id = 3;
Attack = 60;
defense = 20;
blood_volume = 120;
Damage_reduction = 0.2;
star = 0;
back_time = 0;
}
~Apocalypse() {};
friend class battlefield;
};
class battlefield {
private:
double time;
double spytime;//我方间谍到达时间
int my_tank_total;
deque<Tank*> head;
int money;
int s_time;//敌方间谍到达时间
public:
battlefield(int n, int _time) :money(n), time(0), s_time(_time) {
my_tank_total = 0;
}
~battlefield() {
head.clear();
}
//生产
void product() {
int num, t = 0;
int x = 0;//第几个
while (time + pro_time[x] <= s_time) {
if (money < pro_cost[0]) {
break;
}
switch (x) {
case 0:
if (money >= pro_cost[x]) {
Tank* p = new Bear();
head.push_back(p);
money = money - pro_cost[x];
time = time + pro_time[x];
if (money >= pro_cost[(x + 1) % 4])
x = (x + 1) % 4;
else x = 0;
my_tank_total++;
}
else {
x = 0;
}
break;
case 1:
if (money >= pro_cost[x]) {
Tank* p = new Phantom();
head.push_back(p);
money = money - pro_cost[x];
time = time + pro_time[x];
if (money >= pro_cost[(x + 1) % 4])
x = (x + 1) % 4;
else x = 0;
my_tank_total++;
}
else x = 0;
break;
case 2:
if (money >= pro_cost[x]) {
Tank* p = new Prism();
head.push_back(p);
money = money - pro_cost[x];
time = time + pro_time[x];
if (money >= pro_cost[(x + 1) % 4])
x = (x + 1) % 4;
else x = 0;
my_tank_total++;
}
else x = 0;
break;
case 3:
if (money >= pro_cost[x]) {
Tank* p = new Apocalypse();
head.push_back(p);
money = money - pro_cost[x];
time = time + pro_time[x];
if (money >= pro_cost[(x + 1) % 4])
x = (x + 1) % 4;
else x = 0;
my_tank_total++;
}
else x = 0;
break;
}//switch
}//while
if (my_tank_total != 0 && s_emny.size() > 0)print_product();//防止第二种情况,两者都有兵
else if (my_tank_total == 0 && s_emny.size() > 0)
{
printf("Time:0 You lost the war!");
w_l_flag = 1;
} //我方没兵对方有兵
else if (my_tank_total == 0 && s_emny.size() == 0) {
printf("Time:0 It ended in a draw!");
w_l_flag = 1;
}//都没兵平局;
else if (my_tank_total > 0 && s_emny.size() == 0) {
printf("Time:0 You won the war!");
w_l_flag = 1;
}
}
void print_product() {
if (money < pro_cost[0] && time == s_time) {//间谍来并且钱花光的情况
cout << "Time:" << setprecision(4) << time << " ";
printf("Money is empty, ");
printf("Invaded by enemy spies and production stalled, ");
for (deque<Tank*>::iterator it = head.begin(); it < head.end() - 1; it++) {
printf("%s ", tank_[(*it)->id]);
}
printf("%s.", tank_[head[my_tank_total - 1]->id]);
}
else if (money < pro_cost[0]) {//钱先花光
cout << "Time:" << setprecision(4) << time << " ";
printf("Money is empty and production stalled, ");
for (deque<Tank*>::iterator it = head.begin(); it < head.end() - 1; it++) {
printf("%s ", tank_[(*it)->id]);
}
printf("%s.", tank_[head[my_tank_total - 1]->id]);
//间谍入侵
time = s_time;
cout << endl << "Time:" << setprecision(4) << time << " ";
printf("Invaded by enemy spies.");
}
else {//间谍先来
time = s_time;
cout << "Time:" << setprecision(4) << time << " ";
printf("Invaded by enemy spies and production stalled, ");
for (deque<Tank*>::iterator it = head.begin(); it < head.end() - 1; it++) {
printf("%s ", tank_[(*it)->id]);
}
printf("%s.", tank_[head[my_tank_total - 1]->id]);
}
}
//战斗过程
int flag = 0;//标志调用次数 0/1
void Fight(Tank*& attacker1, Tank*& attacker2) {
//1打二
int a = attacker1->id;
int b = attacker2->id;
switch (a) {
case 0:
switch (b) {
case 0:
attacker2->blood_volume = attacker2->blood_volume - (attacker1->Attack - attacker2->defense);
break;
case 1:
if (((Phantom*)attacker2)->Hit_time % 2 == 1) {
((Phantom*)attacker2)->Hit_time++;
}//免疫
else {
attacker2->blood_volume = attacker2->blood_volume - (attacker1->Attack - attacker2->defense);
((Phantom*)attacker2)->Hit_time++;
}
break;
case 2:
attacker2->blood_volume = attacker2->blood_volume - (attacker1->Attack - attacker2->defense);
break;
case 3:
int hurt = (attacker1->Attack - attacker2->defense) * (1 - 0.2);
break;
}//switch
break;
case 1:
switch (b) {
case 0:
attacker2->blood_volume = attacker2->blood_volume - (attacker1->Attack - attacker2->defense);
break;
case 1:
if (((Phantom*)attacker2)->Hit_time % 2 == 1) {
((Phantom*)attacker2)->Hit_time++;
}//免疫
else {
attacker2->blood_volume = attacker2->blood_volume - (attacker1->Attack - attacker2->defense);
((Phantom*)attacker2)->Hit_time++;
}
break;
case 2:
attacker2->blood_volume = attacker2->blood_volume - (attacker1->Attack - attacker2->defense);
break;
case 3:
int hurt = (attacker1->Attack - attacker2->defense) * (1 - 0.2);
break;
}//switch
break;
case 2:
switch (b) {
case 0:
attacker2->blood_volume = attacker2->blood_volume - ((Prism*)attacker1)->Light_Attack;
break;
case 1:
if (((Phantom*)attacker2)->Hit_time % 2 == 1) {
((Phantom*)attacker2)->Hit_time++;
}//免疫
else {
attacker2->blood_volume = attacker2->blood_volume - ((Prism*)attacker1)->Light_Attack;
((Phantom*)attacker2)->Hit_time++;
}
break;
case 2:
attacker2->blood_volume = attacker2->blood_volume - ((Prism*)attacker1)->Light_Attack;
break;
case 3:
attacker2->blood_volume = attacker2->blood_volume - (1 - 0.2) * ((Prism*)attacker1)->Light_Attack;
break;
}
break;
case 3:
switch (b) {
case 0:
attacker2->blood_volume = attacker2->blood_volume - (attacker1->Attack - attacker2->defense);
break;
case 1:
if (((Phantom*)attacker2)->Hit_time % 2 == 1) {
((Phantom*)attacker2)->Hit_time++;
}//免疫
else {
attacker2->blood_volume = attacker2->blood_volume - (attacker1->Attack - attacker2->defense);
((Phantom*)attacker2)->Hit_time++;
}
break;
case 2:
attacker2->blood_volume = attacker2->blood_volume - (attacker1->Attack - attacker2->defense);
break;
case 3:
int hurt = (attacker1->Attack - attacker2->defense) * (1 - 0.2);
break;
}//switch
break;
}//switch
if (flag == 0) {//2打1
flag++;
Fight(attacker2, attacker1);
}
else {
flag = 0;
}
}
void attack_to(Tank* attacker1, Tank* attacker2) {
//一回合中,死一个结束
while (attacker1->blood_volume > 0 && attacker2->blood_volume > 0) {
if (my_star == 0 && time + 1 > my_spy_time)
upStar(0);
Fight(attacker2, attacker1);
/*if(attacker1->blood_volume > 0 && attacker2->blood_volume > 0)*/time++;
}
}
//升星
void upStar(int w) {
my_star = 1;
spytime = 0.5 + s_time + my_spy_way[my_espionage - 1];
printf("\nTime:%0.1lf Spy successfully steal enemy tank technology", spytime);
deque<int>a;
int q = 0;
int it = 0;
if (w == 1) {
it++;
}
for (; it < head.size(); it++) {
q++;
if (q == 1)cout << ",";
if ((head[it])->is_in_home == true) {
(head[it])->star = 1;
a.push_back((head[it])->id);
switch ((head[it])->id) {
case 0:
cout << " Bear";
(head[it])->name = "Bear*";
(head[it])->defense += 5;
(head[it])->Attack += 5;
break;
case 1:
cout << " Phantom";
(head[it])->name = "Phantom*";
(head[it])->defense += 5;
(head[it])->Attack += 5;
break;
case 2:
cout << " Prism";
(head[it])->name = "Prism*";
(head[it])->defense += 5;
(head[it])->Attack += 5;
break;
case 3:
cout << " Apocalypse";
(head[it])->name = "Apocalypse*";
(head[it])->defense += 5;
(head[it])->Attack += 5;
break;
}
}
}
if (q != 0) {
cout << " will be upgraded!";
}
else {
cout << ".";
}
}
void tran(int nums) {//敌人队列
for (int i = 0; i < nums; i++) {
if (emny[i] == "Bear") {
Tank* p = new Bear();
s_emny.push_back(p);
}
else if (emny[i] == "Phantom") {
Tank* p = new Phantom();
s_emny.push_back(p);
}
else if (emny[i] == "Prism") {
Tank* p = new Prism();
s_emny.push_back(p);
}
else if (emny[i] == "Apocalypse") {
Tank* p = new Apocalypse();
s_emny.push_back(p);
}
}
}
void roundPlay(Tank* en_tank, Tank* my_tank, int round) {
printf("\nTime:%d Round %d Start,", (int)time, round);
cout << " " << my_tank->name << " vs " << tank_[en_tank->id] << ".";
//cout << "\n";
attack_to(my_tank, en_tank);
}
void begin_fight() {//开始战斗
time = s_time;
int round = 0;
int fflag = 0;
while (!s_emny.empty() && !head.empty()) {
//出基地
round++;
Tank* en_tank = s_emny.front();
Tank* my_tank = head.front();
s_emny.pop_front();
head.pop_front();
//if (fflag == 0) {
// fflag = 1;
//}
//else
// cout << endl;
//治疗
if (en_tank->id == 1) {
((Phantom*)en_tank)->Hit_time = 0;
}
if (my_tank->id == 1) {
((Phantom*)my_tank)->Hit_time = 0;
}
if (en_tank->islast == false)
en_tank->blood_volume = min(int(en_tank->blood_volume + 4 * (time - en_tank->back_time)), pro_blood[en_tank->id]);
if (my_tank->islast == false)
my_tank->blood_volume = min(int(my_tank->blood_volume + 4 * (time - my_tank->back_time)), pro_blood[en_tank->id]);
if (my_star == 0 && (time + 5) > my_spy_time)
upStar(0);
time += 5;
roundPlay(en_tank, my_tank, round);
printf("\nTime:%d Round %d End, ", (int)time, round);
if (en_tank->blood_volume <= 0 && my_tank->blood_volume <= 0) {//同归
printf("Deadlock!");
int i = 0;
for (i = 0; i < (int)head.size(); i++) {
cout << " ";
cout << head[i]->name;
}
if (i != 0)
cout << ".";
}
else if (en_tank->blood_volume > 0 && my_tank->blood_volume <= 0) {//你输了
printf("You lost the encounter!");
en_tank->back_time = time + 5;
if (s_emny.empty())en_tank->islast = true;
s_emny.push_back(en_tank);
int i = 0;
for (i = 0; i < (int)head.size(); i++) {
cout << " ";
cout << head[i]->name;
}
if (i != 0)
cout << ".";
}
else if (en_tank->blood_volume <= 0 && my_tank->blood_volume > 0) {//你赢了
printf("You won the encounter!");
my_tank->back_time = time + 5;
if (head.empty())my_tank->islast = true;
int i = 0;
for (i = 0; i < (int)head.size(); i++) {
cout << " ";
cout << head[i]->name;
}
if (i != 0)
cout << ".";
if (my_star == 0 && (time + 5) > my_spy_time)
upStar(1);//此时前面那个坦克出发了,后面那个坦克还没到
head.push_back(my_tank);
}
}
if (s_emny.empty() && head.empty()) {
cout << " It ended in a draw!";
}
else if (s_emny.empty() && head.size() > 0) {
cout << " You won the war!";
}
else
cout << " You lost the war!";
}
};
int main() {
int Toal_monty = 0, intrusion_time = 0;
cin >> Toal_monty;//输入金钱
string temp;
int nums = 0;
while (cin >> temp) {
emny.push_back(temp);
++nums;
if (cin.get() == '\n')break;
}
my_espionage = atoi(emny.back().c_str());//间谍星星
emny.pop_back();
intrusion_time = atoi((emny.back()).c_str());//间谍入侵
my_spy_time = intrusion_time + my_spy_way[my_espionage - 1];//己方间谍到达时间
int Longth = 0;
battlefield battal(Toal_monty, intrusion_time);
battal.tran(nums - 2);
battal.product();
if (w_l_flag == 0)
battal.begin_fight();
return 0;
}