本文章依据山东大学C++课程期末大作业要求实现,其中代码注释详细,需要的同学可用于参考。
本代码的实现分为五个文件,分别为武器类的设计,勇士类的设计,司令部的设计,城市的设计以及具体的游戏流程。
一些提醒:
1.实现的顺序为武器类的设计->勇士类的设计->司令部的设计->城市的设计->游戏过程
2.实现多态时需要注意内存的管理
下面的代码为为实现图形界面的代码,如有需要,联系作者byemail tor108@outlook.com
首先是武器类的实现:
#pragma once
#pragma once
#include<iostream>
using namespace std;
class Fight_time {
public:
int min;
int hour;
Fight_time() {
min = 0;
hour = 0;
}
Fight_time operator +(int min1) {
Fight_time g = *this;
g.min += min1;
g.hour += min / 60;
g.min %= 60;
return g;
}
Fight_time operator -(int min1) {
min += min1;
hour += min / 60;
min %= 60;
return *this;
}
friend ostream& operator<<(ostream& os, const Fight_time& f) {
if (f.hour < 10) {
if (f.min < 10) {
os << "00" << f.hour << ':' << '0' << f.min;
}
else {
os << "00" << f.hour << ':' << f.min;
}
}
else if (f.hour >= 10 && f.hour <= 100) {
if (f.min < 10) {
os << '0' << f.hour << ':' << '0' << f.min;
}
else {
os << '0' << f.hour << ':' << f.min;
}
}
else {
if (f.min < 10) {
os << f.hour << ':' << '0' << f.min;
}
else {
os << f.hour << ':' << f.min;
}
}
return os;
}
};
Fight_time fight_time;
int city_sum;
class Weapon {//ÎäÆ÷Àà
private:
string kind;//ÎäÆ÷ÖÖÀàµÄ±êŒÇ
public:
int use_times = 0;//ʹÓÃŽÎÊý
int durability;//Ä͟öÈ
int hurt;//É˺Š
int id;//ÎäÆ÷±àºÅ
Weapon() {
kind = "NONE";
hurt = 0;
}
Weapon(string kind1, int a) :kind(kind1), hurt(a) {}//¹¹Ô캯Êý
virtual string get_weapon_kind() {//»ñÈ¡ÎäÆ÷ÀàÐÍ
return kind;
}
string get_kind() {
return kind;
}
virtual int get_id() {
return id;
}
virtual int get_hurt() {//»ñÈ¡É˺Š
return hurt;
}
virtual bool used() {//ŒìÑéÕâžöÎäÆ÷ÊÇ·ñ±š·Ï
use_times++;
return use_times < durability;
}
bool operator <(Weapon& w) {//×÷Ϊ֮ºóµÄµÃµœÎäÆ÷µÄÅÅÐòÒÔŒ°ÎäÆ÷µÄʹÓÃ˳Ðò
if (kind == "arrow" && w.get_weapon_kind() == "arrow") {
return use_times > w.use_times;
}
else {
return this->get_id() < w.get_id();//žùŸÝ±àºÅÅÅÐò
}
}
bool operator==(Weapon& w) {//ÖØÔØÔËËã·û=£¬ÎªÖ®ºóÀǵĻñÈ¡ÎäÆ÷Ìṩ·œ±ã
return kind == w.get_kind();
}
virtual ~Weapon() {}
};
class Arrow :public Weapon {
public:
Arrow(int hurt1) :Weapon("arrow", hurt1) {
id = 2;
hurt = hurt1*3/10;
durability = 2;//ÌØÕ÷ʹÓÃŽÎÊý
}
virtual bool used()override {//ŒìÑéÕâžöÎäÆ÷ÊÇ·ñ±š·Ï
use_times++;
return use_times < durability;
}
};
class Sword :public Weapon {
public:
Sword(int hurt1):Weapon("sword",hurt1) {
hurt = hurt1*2/10;
id = 0;
durability = 10086;//ÌØÕ÷ʹÓÃŽÎÊý
}
virtual bool used()override {//ʹÓò¢ÅжÏÊÇ·ñʹÓÃÍê
use_times++;
return use_times < durability;
}
};
class Bomb :public Weapon {
public:
Bomb(int hurt1):Weapon("bomb",hurt1) {
hurt = hurt*4/10;
id = 1;
durability = 1;//ÌØÕ÷ʹÓÃŽÎÊý
}
virtual bool used() override {//ʹÓò¢ÅжÏÊÇ·ñʹÓÃÍê
use_times++;
return use_times < durability;
}
};
然后是勇士类:
#pragma once
#include"weapon.h"
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<string>weapon_order({ "sword","bomb","arrow" });//ÎäÆ÷˳Ðò
vector<int>input_warrior_life(5, 0);//ÎäÊ¿µÄÉúÃüÖµ£¬Ë³ÐòΪdragon,ninjia,icecream,lion,wolf
vector<int>input_warrior_hurt(5, 0);//ÎäÊ¿µÄ¹¥»÷ÁŠ£¬Ë³ÐòͬÉÏ
int K;//lionÓÂʿÿ×ßÒ»²œÊ¿ÆøµÄœµµÍÖµ
bool decide_weapons(vector<Weapon>w) {
if (w.size() == 0) {
return 1;
}
else {
if (w[w.size()-1].get_kind() == "sword") {
return 1;
}
}
return 0;
}
class Warrior {
private:
string camp;//ËùÊôµÄÕóÓª
int life;//ѪÌõ
int id;//ÎäÊ¿µÄ±àºÅ
string kind;//ÎäÊ¿µÄÀàÐÍ
int hurt;//¹¥»÷ÁŠ
int step;//ÎäÊ¿×ߵIJœÊý
public:
int honest;//Êš×ÓµÄÌØÐÔ
vector<Weapon>weapons;//ÎäÆ÷°ü
vector<int> get_weapons_sum(){
int arrow_sum=0;
int sword_sum=0;
int bomb_sum=0;
for (int i = 0; i < weapons.size(); i++) {
if (weapons[i].get_kind() == "sword") {
sword_sum++;
}
else if (weapons[i].get_kind() == "arrow") {
arrow_sum++;
}
else if (weapons[i].get_kind() == "bomb") {
bomb_sum++;
}
}
return { sword_sum,bomb_sum,arrow_sum };
}
string get_camp() {
return camp;
}
Warrior() {
id = 10086;
life = 0;
hurt = 0;
step = 0;
}
void set_kind(string s) {
kind = s;
}
string get_kind() {
return kind;
}
bool subtract_life_if_die(int hurt_from_enemy) {//Ϊ՜¶·Ê±¶ÔµÐÈ˵IJÙ×÷ÌṩÏ÷ŒõѪÁ¿µÄœÓ¿Ú²¢·µ»ØÊÇ·ñËÀÍö
life -= hurt_from_enemy;
if (life <= 0) {
return 1;
}
else {
return 0;
}
}
Warrior(int life1, int id1, int hurt1) :life(life1), id(id1+1), hurt(hurt1) { step = 0; honest = 1; }//¹¹Ô캯Êý£¬³õÊŒÎäÆ÷µÄ»ñÈ¡ÔÚ×ÓÀàÖÐʵÏÖ
int get_life() {
return life;
}
void set_camp(string camp1) {
camp = camp1;
if (camp == "blue") {//ŽÓÓұ߿ªÊŒ×ßÕâžö
set_step(city_sum + 1);
}
}
int get_id() {//»ñÈ¡ÎäÊ¿±àºÅ
return id;
}
double get_hurt() {//»ñÈ¡ÎäÊ¿¹¥»÷ÁŠ
return hurt;
}
int get_step() {//»ñÈ¡ÎäÊ¿²œÊý
return step;
}
void set_step(int step1) {
step = step1;
}
void add_step() {//Ϊ×ÓÀàlionÌṩœÓ¿Ú
step++;
}
void subtract_step() {
step--;
}
virtual bool walk(string camp) {//×ß·
bool if_reach_command = 0;
if (camp == "red")
step++;//ÿ×ßÒ»²œ£¬ŸÍ¶Ô²œÊýœøÐвÙ×÷
else
step--;
if (this->get_step() == 0 && this->get_camp() == "blue") {
if_reach_command = 1;
std::cout << fight_time+10 << ' ' << this->get_camp() << ' ' << this->get_kind() << ' ' << this->get_id() << " reached red headquarter with " << this->get_life() << " elements and force " << this->get_hurt() << endl;
std::cout << fight_time+10 << ' ' << "red headquarter was taken" << endl;
}
else if (this->get_step() == city_sum + 1 && this->get_camp() == "red") {
if_reach_command = 1;
std::cout << fight_time+10 << ' ' << this->get_camp() << ' ' << this->get_kind() << ' ' << this->get_id() << " reached blue headquarter with " << this->get_life() << " elements and force " << this->get_hurt() << endl;
std::cout << fight_time+10 << ' ' << "blue headquarter was taken" << endl;
}
else
std::cout << fight_time+10 << ' ' << this->get_camp() << ' ' << this->get_kind() << ' ' << this->get_id()<<" marched to city " << this->get_step() << ' ' << "with " << this->get_life() << " elements and force " << this->get_hurt() << endl;
return if_reach_command;
}
void sort_weapon() {//¶ÔÎäÆ÷ÅÅÐò
sort(weapons.begin(), weapons.end());
}
void seize_weapon(Warrior& w){//œÉ»ñÎäÆ÷
w.sort_weapon();//ÎäÆ÷ÅÅÐò
while (weapons.size() < 10) {//×îŽóµÄÎäÆ÷ÈÝÄÉÁ¿Îª10
if (w.weapons.size() != 0) {
weapons.push_back(w.weapons[0]);//»ñÈ¡ÎäÆ÷
int rate;//±ÈÂÊ
if (weapons[weapons.size()-1].get_kind() == "sword") {
rate = 2;
}
else if (weapons[weapons.size() - 1].get_kind() == "bomb") {
rate = 4;
}
else if (weapons[weapons.size() - 1].get_kind() == "arrow") {
rate = 3;
}
this->weapons[weapons.size() - 1].hurt = this->get_hurt() * rate / 10;
w.weapons.erase(w.weapons.begin(), w.weapons.begin()+1);//ÎäÆ÷±»ÂÓ¶á
}
else {
break;//ÂÓ¶áÍêŸÍÍ˳ö
}
}
}
virtual int use_weapon(Warrior& enemy,int&use_order) {//ʹÓÃÎäÆ÷ŽòµÐÈË£¬Í¬Ê±ÅжÏÎäÆ÷ÊÇ·ñ¿ÉÓò¢œøÐÐŽŠÀí,·µ»ØÖµŽú±í0ûËÀÈË£¬1µÐÈËËÀ£¬2¶ŒËÀ
if (weapons.size() == 0) {//ûÓÐÎäÆ÷ŸÍ·µ»Ø
return 0;
}
int return_val;//·µ»ØµÄÖµ
return_val=enemy.subtract_life_if_die(weapons[use_order].get_hurt());//ʹÓÃÎäÆ÷µÄÉ˺Š
if (return_val != 0) {//Èç¹ûŽòËÀÁ˶Էœ£¬ÄÇÃŽreturnvalÒ»¶šŽóÓÚµÈÓÚ1
return_val = 1;
}
if (kind != "ninja") {
if (weapons[use_order].get_weapon_kind() == "bomb") {//Èç¹ûÊÇbomb£¬Òª¶Ô×ÔÉí¿ÛѪ
life -= weapons[use_order].get_hurt() / 2;
if (life <= 0) {
return_val += 7;//·µ»ØֵΪ1ËÀÁËÒ»žöµÐÈË£¬·µ»ØֵΪ7ËÀÁË×ÔŒº£¬·µ»ØֵΪ°ËËÀÁËÁ©
}
}
}
if (!weapons[use_order].used()) {//ŒõȥʹÓÃŽÎÊý£¬²¢ÅжÏÊÇ·ñ±š·Ï
weapons.erase(weapons.begin()+use_order, weapons.begin()+use_order+1);//Èç¹û±š·Ï£¬°ÑÕâžöÎäÆ÷ÈÓÁË
use_order--;
}
return return_val;//·µ»ØËÀÉËÇé¿ö
}
/****************************************************************************************************************************************************************************/
/*ÀǵÄÌØÊâ²Ù×÷£¬·ÅÔÚÕâÀïΪÁË·œ±ãÕœ¶·Ê±µÄÉèÖÃ*/
virtual void rub_weapon(Warrior& enemy){}
/************************************************************************************************************************************************************************************/
virtual bool if_escape() { return 0; };//lionµÄÊôÐÔ£¬ŽýʵÏÖ
virtual string fight(Warrior& enemy) {//ÓëµÐ·œÊ¿±ø¶ÔÕœ
int if_end = 0;
sort_weapon();//Œº·œÎäÆ÷ÅÅÐò
enemy.sort_weapon();//µÐ·œÎäÆ÷ÅÅÐò
int last_me_life=life;//ŒÇÂŒŒº·œÉÏÒ»ÂÖµÄÉúÃüÖµ£¬ÓëµÐ·œµÄÉúÃüÖµœáºÏÅжÏÊÇ·ñÆœŸÖ
int last_enemy_life = enemy.get_life();
int this_use_order = 0;
int enemy_use_order = 0;
while (!if_end) {
if_end = use_weapon(enemy,this_use_order);//ÏÈÊÖʹÓÃÎäÆ÷
if (if_end) {//Èç¹ûŽòËÀÁË£¬ŽòÆÆÑ»·
if (if_end == 1) {//Ò»žöÏÈÊÖÓ®
seize_weapon(enemy);
return "win";
}
else if (if_end == 8) {//ÁœžöÆœŸÖ
return "equal";
}
else if (if_end == 7) {
return "lose";
}
break;
}
else {
if_end = enemy.use_weapon(*this,enemy_use_order);//ºóÊÖʹÓÃÎäÆ÷
}
if (last_me_life == life && last_enemy_life == enemy.get_life()&&decide_weapons(this->weapons)&&decide_weapons(enemy.weapons)) {//Èç¹ûÕâÒ»ŸÖµÄÉúÃüֵûÓб仯£¬ŸÍ·µ»ØÆœŸÖ
return "equal";
}
else {
last_me_life = life;
last_enemy_life = enemy.get_life();
}
this_use_order++;
if (this->weapons.size() != 0)
this_use_order %= this->weapons.size();
enemy_use_order++;
if(enemy.weapons.size()!=0)
enemy_use_order %= enemy.weapons.size();
}
if (if_end == 1) {//Ò»žöºóÊÖÓ®
enemy.seize_weapon(*this);
return "lose";
}
else if (if_end == 8) {//ÁœžöÆœŸÖ
return "equal";
}
}
};
Weapon original_weapon(int id,Warrior w) {//³õÊŒµÄÎäÆ÷ÉèÖÃ
if (id % 3 == 0) {
return Sword(w.get_hurt());
}
else if(id%3==1) {
return Bomb(w.get_hurt());
}
else {
return Arrow(w.get_hurt());
}
}
class Lion:public Warrior{
public:
Lion(int id,int honesty) :Warrior(input_warrior_life[3], id, input_warrior_hurt[3]) {//idŽýʵÏÖ£¬ÊÇÎäÊ¿µÄ±àºÅ
this->weapons.push_back(original_weapon(this->get_id(),*this));
this->set_kind("lion");//ÉèÖÃÎäÊ¿ÀàÐÍ
this->honest = honesty;
};//¹¹Ô캯Êý
bool if_escape() {//Èç¹ûÌÓÅÜ·µ»Øtrue
return this->honest <= 0;
}
bool walk(string camp) {//ÖØÐŽwalkº¯Êý£¬Ã¿×ßÒ»²œÖҳ϶ȜµµÍK
this->honest -= K;
return Warrior::walk(camp);
}
};
class Ninjia :public Warrior {
public:
Ninjia(int id) :Warrior(input_warrior_life[1], id, input_warrior_hurt[1]) {
this->weapons.push_back(original_weapon(this->get_id(), *this));
this->weapons.push_back(original_weapon(this->get_id()+1, *this));//»ñÈ¡ÁœžöÎäÆ÷
this->set_kind("ninja");
}
/*ÖØЎʹÓÃÎäÆ÷º¯Êý£¬±£Ö€ÆäʹÓÃbombÎäÆ÷²»»á±»·ŽÉË*/
int use_weapon(Warrior& enemy) {//ʹÓÃÎäÆ÷ŽòµÐÈË£¬Í¬Ê±ÅжÏÎäÆ÷ÊÇ·ñ¿ÉÓò¢œøÐÐŽŠÀí,·µ»ØÖµŽú±í0ûËÀÈË£¬1µÐÈËËÀ£¬2¶ŒËÀ
if (weapons.size() == 0) {//ûÓÐÎäÆ÷ŸÍ·µ»Ø
return 0;
}
int return_val;//·µ»ØµÄÖµ
return_val = enemy.subtract_life_if_die(weapons[0].get_hurt());//ʹÓÃÎäÆ÷µÄÉ˺Š
if (return_val != 0) {//Èç¹ûŽòËÀÁ˶Էœ£¬ÄÇÃŽreturnvalÒ»¶šŽóÓÚµÈÓÚ1
return_val = 1;
}
/*°ÑÕâžö¶Ô×ÔÉí¿ÛѪµÄ²Ù×÷ÉŸ³ý
if (weapons[0].get_weapon_kind() == "bomb") {//Èç¹ûÊÇbomb£¬Òª¶Ô×ÔÉí¿ÛѪ
life -= weapons[0].get_hurt() / 2;
if (life <= 0) {
return_val += 1;//±íÊŸËÀÁËÁ©
}
}*/
if (!weapons[0].used()) {//ŒõȥʹÓÃŽÎÊý£¬²¢ÅжÏÊÇ·ñ±š·Ï
weapons.erase(weapons.begin(), weapons.begin() + 1);//Èç¹û±š·Ï£¬°ÑÕâžöÎäÆ÷ÈÓÁË
}
return return_val;//·µ»ØËÀÉËÇé¿ö
}
};
class Iceman:public Warrior {
public:
Iceman(int id) :Warrior(input_warrior_life[2], id, input_warrior_hurt[2]) {//idŽýʵÏÖ£¬ÊÇÎäÊ¿µÄ±àºÅ
this->weapons.push_back(original_weapon(this->get_id(), *this));
this->set_kind("iceman");//ÉèÖÃÎäÊ¿ÀàÐÍ
};//¹¹Ô캯Êý
bool walk(string camp) {
this->subtract_life_if_die(this->get_life()/10);
return Warrior::walk(camp);
}
};
class Dragon :public Warrior{
public:
int morale;
Dragon(int id, int morale1) :Warrior(input_warrior_life[0], id, input_warrior_hurt[0]) {//idŽýʵÏÖ£¬ÊÇÎäÊ¿µÄ±àºÅ
this->weapons.push_back(original_weapon(this->get_id(), *this));
this->set_kind("dragon");//ÉèÖÃÎäÊ¿ÀàÐÍ
morale = morale1;
};//¹¹Ô캯Êý
void cheer() {//ÌØÕ÷£º»¶ºô
}
};
class Wolf :public Warrior {
public:
Wolf(int id) :Warrior(input_warrior_life[4], id, input_warrior_hurt[4]) {
this->set_kind("wolf");
}
void rub_weapon(Warrior& enemy) {
if (enemy.get_kind() == "wolf") {//Èç¹û¶Ô·œÒ²ÊÇÀÇ£¬ŸÍ²»ÇÀÁË
return;
}
else {
enemy.sort_weapon();
int is_rub = 0;//ŸßÌåÇÀÁËû
while (enemy.weapons.size() != 0) {
Weapon weapon_g = enemy.weapons[0];
this->weapons.push_back(weapon_g);//ÌíŒÓÎäÆ÷
int rate;//±ÈÂÊ
if (weapon_g.get_kind() == "sword") {
rate = 2;
}
else if (weapon_g.get_kind() == "bomb") {
rate = 4;
}
else if (weapon_g.get_kind() == "arrow") {
rate = 3;
}
this->weapons[weapons.size() - 1].hurt = this->get_hurt() * rate / 10;
enemy.weapons.erase(enemy.weapons.begin(), enemy.weapons.begin() + 1);//ÎäÆ÷±»ÇÀÁË
is_rub++;
if (this->weapons.size() >= 10) {//Èç¹ûÒª³¬¹ýÊ®žöÎäÆ÷£¬ŸÍŽò¶Ï
break;
}
if (enemy.weapons.size() != 0 && this->weapons[this->weapons.size() - 1] == enemy.weapons[0]) {//Èç¹ûÏÂÒ»žöµÄÎäÆ÷ºÍÕâžöÒ»Ñù£¬ŸÍŒÌÐøÇÀ
continue;
}
else {
break;
}
}
if (is_rub) {//Êä³öÀÇÇÀµÄÎäÆ÷µÄ¹ý³Ì
std::cout << fight_time + 35 << ' ' << this->get_camp() << ' ' << this->get_kind() << ' ' << this->get_id() << ' ' << "took " << is_rub << ' ' << weapons[weapons.size() - 1].get_weapon_kind() << " from " << enemy.get_camp() << ' ' << enemy.get_kind() << ' ' << enemy.get_id() << " in city " << this->get_step() << endl;
}
}
}
};
然后是司令部类:
#pragma once
#include"warrior.h"
vector<string>red_create_order = { "iceman","lion","wolf","ninja","dragon" };//ºì·œµÄÖÆÔì˳Ðò
vector<string>blue_create_order = { "lion","dragon","ninja","iceman","wolf" };//À¶·œµÄÖÆÔì˳Ðò
class Command {//ËŸÁµÄ¶šÒå
public:
string camp;//ÕóÓª
int num=0;//Éú²úÎäÊ¿µÄ±àºÅ
int element;//ÉúÃüÔŽ
bool if_can_create;
vector<Warrior*>warrior_birth_set;//ÉúÏÂÀŽµÄÎäÊ¿ŽæÔÚÕâÀÓÉÎäÊ¿ÌôÑ¡
Command(string camp1, int element1) :camp(camp1), element(element1) { if_can_create = 1; }
void out_message_birth(Warrior w) {//Êä³ö³öÉúÐÅÏ¢µÄº¯Êý
std::cout << fight_time << ' ' << camp << ' ' << w.get_kind() << ' ' << w.get_id() << ' ' << "born"<<endl;
if (w.get_kind() == "lion") {
std::cout << "Its" << ' ' << "loyalty" << " is " << w.honest << endl;
}
}
void create_warrior(int order) {//ŽŽœšÎäÊ¿
if (if_can_create) {
Warrior w;
if ((camp == "red" ? red_create_order[num % 5] : blue_create_order[num % 5]) == "iceman" && element - input_warrior_life[2] >= 0) {
element -= input_warrior_life[2];
warrior_birth_set.push_back(new Iceman(num));
w = Iceman(num);
}
else if ((camp == "red" ? red_create_order[num % 5] : blue_create_order[num % 5]) == "lion" && element - input_warrior_life[3] >= 0) {
element -= input_warrior_life[3];
warrior_birth_set.push_back(new Lion(num, element));
w = Lion(num, element);
}
else if ((camp == "red" ? red_create_order[num % 5] : blue_create_order[num % 5]) == "wolf" && element - input_warrior_life[4] >= 0) {
element -= input_warrior_life[4];
warrior_birth_set.push_back(new Wolf(num));
w = Wolf(num);
}
else if ((camp == "red" ? red_create_order[num % 5] : blue_create_order[num % 5]) == "ninja" && element - input_warrior_life[1] >= 0) {
element -= input_warrior_life[1];
warrior_birth_set.push_back(new Ninjia(num));
w = Ninjia(num);
}
else if ((camp == "red" ? red_create_order[num % 5] : blue_create_order[num % 5]) == "dragon" && element - input_warrior_life[0] >= 0)
{
element -= input_warrior_life[0];
warrior_birth_set.push_back(new Dragon(num, element));
w = Dragon(num, element);
}
else {
if_can_create = 0;//ÅжÏÊÇ·ñ¿ÉÒÔŒÌÐøÖÆÔìÊ¿±ø
}
if (if_can_create) {//Èç¹ûÖÆÔìÁËÔÙÊä³ö
(*warrior_birth_set[warrior_birth_set.size() - 1]).set_camp(camp);
out_message_birth(*warrior_birth_set[num]);//Êä³ö³öÉúÐÅÏ¢
}
num++;//ÿŽÎµ÷ÓöŒ¶Ô±àºÅŒÓ
}
/*else
cout << camp << " command can't create warriors anymore!" << endl;*/
}
~Command() {
for (int i = 0; i < this->warrior_birth_set.size(); i++) {
delete this->warrior_birth_set[i];
}
}
};
再然后是城市类,战斗发生在城市中:
#pragma once
#include"commend.h"
class City {
public:
int id;//³ÇÊеıàºÅ
vector<Warrior*>red_warrior;//ÔÚijһžö³ÇÊÐÖк췜µÄÊ¿±ø
vector<Warrior*>blue_warrior;//ÔÚijһžö³ÇÊÐÖÐÀ¶·œµÄÊ¿±ø
bool check_lion() {//Œì²éÊš×ÓÊÇ·ñÒªÌÓÅÜ
for (int i = 0; i < red_warrior.size(); i++) {
if ((red_warrior[i])->get_kind() == "lion") {
if (red_warrior[i]->if_escape()) {
red_warrior.erase(red_warrior.begin() + i, red_warrior.begin() + i + 1);//¿ª³ý±øŒ®
return 1;
}
}
}
for (int i = 0; i < blue_warrior.size(); i++) {
if ((blue_warrior[i])->get_kind() == "lion") {
if (blue_warrior[i]->if_escape()) {
blue_warrior.erase(blue_warrior.begin() + i, blue_warrior.begin() + i + 1);//¿ª³ý±øŒ®
return 1;
}
}
}
return 0;
};
void fight(){
if (blue_warrior.size() != 0 && red_warrior.size() != 0) {//Ê×ÏÈÈ·±£²»Îª¿Õ
string fight_result = id % 2 == 0 ? (*blue_warrior[0]).fight(*red_warrior[0]) : (*red_warrior[0]).fight(*blue_warrior[0]);
if (fight_result == "equal"&& (*red_warrior[0]).get_life()>0) {//Êä³öÕœ¶·ÐÅÏ¢
std::cout << fight_time+40<<" both red " << (*red_warrior[0]).get_kind() << ' ' << (*red_warrior[0]).get_id() << " and blue " << ((*blue_warrior[0])).get_kind() << ' ' << (*blue_warrior[0]).get_id() << " were alive in city " << (*red_warrior[0]).get_step() << endl;
if (red_warrior[0]->get_kind() == "dragon") {
cout << fight_time + 40 << ' ' << "red dragon " << red_warrior[0]->get_id() << " yelled in city " << red_warrior[0]->get_step() << endl;
}
if (blue_warrior[0]->get_kind() == "dragon") {
cout << fight_time + 40 << ' ' << "blue dragon " << blue_warrior[0]->get_id() << " yelled in city " << blue_warrior[0]->get_step() << endl;
}
}
else if (fight_result == "equal" && (*red_warrior[0]).get_life() <= 0) {
std::cout << fight_time + 40<<" both red " << (*red_warrior[0]).get_kind() << ' ' << (*red_warrior[0]).get_id() << " and blue " << ((*blue_warrior[0])).get_kind() << ' ' << (*blue_warrior[0]).get_id() << " died in city " << (*red_warrior[0]).get_step() << endl;
}
else if (fight_result == "win") {
if (id % 2 == 0) {//À¶Ó®
std::cout << fight_time + 40 << ' '<< (*blue_warrior[0] ).get_camp()<<' ' << (*blue_warrior[0]).get_kind() << ' ' << (*blue_warrior[0]).get_id() << " killed"<< ' ' << (*red_warrior[0]).get_camp()<<' ' << (*red_warrior[0]).get_kind() << ' ' << (*red_warrior[0]).get_id() <<" in city "<< (*red_warrior[0]) .get_step()<< " remaining " << ((*blue_warrior[0])).get_life() << " elements" << endl;
if (blue_warrior[0]->get_kind() == "dragon") {
cout << fight_time + 40 << ' ' << "blue dragon " << blue_warrior[0]->get_id() << " yelled in city " << blue_warrior[0]->get_step() << endl;
}
}
else {
std::cout << fight_time + 40 <<' '<< (*red_warrior[0]).get_camp()<<' '<< (*red_warrior[0]).get_kind() << ' ' << (*red_warrior[0]).get_id() << " killed" << ' '<< (*blue_warrior[0]).get_camp()<<' '<<(*blue_warrior[0]).get_kind() << ' ' << (*blue_warrior[0]).get_id() << " in city " << (*red_warrior[0]).get_step() << " remaining " << ((*red_warrior[0])).get_life() << " elements" << endl;
if (red_warrior[0]->get_kind() == "dragon") {
cout << fight_time + 40 << ' ' << "red dragon " << red_warrior[0]->get_id() << " yelled in city " << red_warrior[0]->get_step() << endl;
}
}
}
else if (fight_result == "lose") {
if (id % 2 == 1) {//À¶Ó®
std::cout << fight_time + 40 << ' ' << (*blue_warrior[0]).get_camp() << ' ' << (*blue_warrior[0]).get_kind() << ' ' << (*blue_warrior[0]).get_id() << " killed" << ' ' << (*red_warrior[0]).get_camp() << ' ' << (*red_warrior[0]).get_kind() << ' ' << (*red_warrior[0]).get_id() << " in city " << (*red_warrior[0]).get_step() << " remaining " << ((*blue_warrior[0])).get_life() << " elements" << endl;
if (blue_warrior[0]->get_kind() == "dragon") {
cout << fight_time + 40 << ' ' << "blue dragon " << blue_warrior[0]->get_id() << " yelled in city " << blue_warrior[0]->get_step() << endl;
}
}
else {
std::cout << fight_time + 40 << ' ' << (*red_warrior[0]).get_camp() << ' ' << (*red_warrior[0]).get_kind() << ' ' << (*red_warrior[0]).get_id() << " killed" << ' ' << (*blue_warrior[0]).get_camp() << ' ' << (*blue_warrior[0]).get_kind() << ' ' << (*blue_warrior[0]).get_id() << " in city " << (*red_warrior[0]).get_step() << " remaining " << ((*red_warrior[0])).get_life() << " elements" << endl;
if (red_warrior[0]->get_kind() == "dragon") {
cout << fight_time + 40 << ' ' << "red dragon " << red_warrior[0]->get_id() << " yelled in city " << red_warrior[0]->get_step() << endl;
}
}
}
blue_warrior.erase(blue_warrior.begin(), blue_warrior.end());
red_warrior.erase(red_warrior.begin(), red_warrior.end());
}
}
};
最后是主函数:
#include"city.h"
#include<unordered_map>
using namespace std;
void game(int i) {//ÓÎÏ·¹ý³Ìº¯Êý
int if_over_time;
int element;
cin >> element;
cin >> city_sum;
cin >> K;//Öҳ϶ȵÄÊÂ
int end_time;
cin >> end_time;
for (int i = 0; i < 5; i++) {//ÊäÈëÉúÃüÖµ
cin >> input_warrior_life[i];
}
for (int i = 0; i < 5; i++) {//ÊäÈë¹¥»÷ÁŠ
cin >> input_warrior_hurt[i];
}
Command red("red", element);
Command blue("blue", element);
vector<City>city_set(city_sum);
/*ÉèÖÃcity±àºÅ*/
for (int i = 0; i < city_sum; i++) {
city_set[i].id = i + 1;
}
int order = 0;
std::cout << "Case " << i + 1 << ':' << endl;
while (1) {
/*²úÉúÎäÊ¿*/
if ((fight_time).hour * 60 + (fight_time).min > end_time) {
break;
}
red.create_warrior(order);
blue.create_warrior(order);
if ((fight_time + 5).hour * 60 + (fight_time + 5).min > end_time) {
break;
}
/*Êš×ÓÌÓÅÜ*/
for (int i = 0; i < red.warrior_birth_set.size(); i++) {
if (red.warrior_birth_set[i]->get_life() > 0 && red.warrior_birth_set[i]->get_kind() == "lion" && red.warrior_birth_set[i]->honest <= 0) {
red.warrior_birth_set[i]->subtract_life_if_die(9999999);//ÌÓ±øÓŠžÃ±»É±ËÀ
std::cout << fight_time + 5 << ' ' << red.warrior_birth_set[i]->get_camp() << ' ' << red.warrior_birth_set[red.warrior_birth_set.size() - 1]->get_kind() << ' ' << red.warrior_birth_set[red.warrior_birth_set.size() - 1]->get_id() << ' ' << "ran away" << endl;
}
}
for (int i = 0; i < blue.warrior_birth_set.size(); i++) {
if (blue.warrior_birth_set[i]->get_life() > 0 && blue.warrior_birth_set[i]->get_kind() == "lion" && blue.warrior_birth_set[i]->honest <= 0) {
blue.warrior_birth_set[i]->subtract_life_if_die(9999999);//ÌÓ±øÓŠžÃ±»É±ËÀ
std::cout << fight_time + 5 << ' ' << blue.warrior_birth_set[i]->get_camp() << ' ' << blue.warrior_birth_set[blue.warrior_birth_set.size() - 1]->get_kind() << ' ' << blue.warrior_birth_set[blue.warrior_birth_set.size() - 1]->get_id() << ' ' << "ran away" << endl;
}
}
order++;//ÎäÊ¿µÄ±àºÅ++
/*ÐÂÒ»ÂÖÇ°œø֮ǰÏȶԳÇÊÐÇå³ý*/
for (int i = 0; i < city_set.size(); i++) {
city_set[i].blue_warrior.erase(city_set[i].blue_warrior.begin(), city_set[i].blue_warrior.end());
city_set[i].red_warrior.erase(city_set[i].red_warrior.begin(), city_set[i].red_warrior.end());
}
if ((fight_time + 10).hour * 60 + (fight_time + 10).min > end_time) {
break;
}
/*ÎäÊ¿Ç°œø*/
bool if_red_reach_command = 0;
bool if_blue_reach_commmand = 0;
unordered_map<Warrior*, int>m;
for (int k = 0; k <= city_sum + 1; k++) {
for (int i = 0; i < red.warrior_birth_set.size(); i++) {
if (red.warrior_birth_set[i]->get_life() > 0 && red.warrior_birth_set[i]->get_step() + 1 == k && m[red.warrior_birth_set[i]] != 1412) {
red.warrior_birth_set[i]->walk("red") ? if_red_reach_command = 1 : if_red_reach_command = if_red_reach_command;
m[red.warrior_birth_set[i]] = 1412;
}
}
for (int i = 0; i < blue.warrior_birth_set.size(); i++) {
if (blue.warrior_birth_set[i]->get_life() > 0 && blue.warrior_birth_set[i]->get_step() - 1 == k && m[blue.warrior_birth_set[i]] != 1412) {
blue.warrior_birth_set[i]->walk("blue") ? if_blue_reach_commmand = 1 : if_blue_reach_commmand = if_blue_reach_commmand;
m[blue.warrior_birth_set[i]] == 1412;
}
}
}
/*Œì²éÊÇ·ñÓезœÎäÊ¿»î×ÅœøÈëËŸÁ*/
if (if_blue_reach_commmand || if_red_reach_command) {
break;
}
/*ÎäÊ¿œøÈë³ÇÊÐ*/
for (int i = 0; i < city_sum; i++) {//ÿһžö³ÇÊжŒŒìÑéÊÇ·ñÓÐÎäÊ¿œøÈë
for (int u = 0; u < red.warrior_birth_set.size(); u++) {//ºìÉ«ÕóÓª
if (red.warrior_birth_set[u]->get_step() == city_set[i].id && red.warrior_birth_set[u]->get_life() > 0) {
city_set[i].red_warrior.push_back(red.warrior_birth_set[u]);
}
}
for (int u = 0; u < blue.warrior_birth_set.size(); u++) {//À¶É«ÕóÓª
if (blue.warrior_birth_set[u]->get_step() == city_set[i].id && blue.warrior_birth_set[u]->get_life() > 0) {
city_set[i].blue_warrior.push_back(blue.warrior_birth_set[u]);
}
}
}
if ((fight_time + 35).hour * 60 + (fight_time + 35).min > end_time) {
break;
}
for (int i = 0; i < city_sum; i++) {
if (city_set[i].blue_warrior.size() != 0 && city_set[i].red_warrior.size() != 0) {
if (city_set[i].blue_warrior[0]->get_kind() == "wolf" && city_set[i].red_warrior[0]->get_kind() != "wolf") {
city_set[i].blue_warrior[0]->rub_weapon(*city_set[i].red_warrior[0]);
}
else if (city_set[i].blue_warrior[0]->get_kind() != "wolf" && city_set[i].red_warrior[0]->get_kind() == "wolf") {
city_set[i].red_warrior[0]->rub_weapon(*city_set[i].blue_warrior[0]);
}
}
}
if ((fight_time + 40).hour * 60 + (fight_time + 40).min > end_time) {
break;
}
/*ÿžö³ÇÊÐÕ¹¿ªÕœ¶·*/
for (int i = 0; i < city_sum; i++) {
if (city_set[i].check_lion())continue;
city_set[i].fight();
}
if ((fight_time + 20).hour * 60 + (fight_time + 50).min >= end_time) {
break;
}
/*±šžæÓµÓеÄÉúÃüÔªÊýÁ¿*/
std::cout << fight_time + 50 << ' ' << red.element << " elements in red headquarter" << endl;
std::cout << fight_time + 50 << ' ' << blue.element << " elements in blue headquarter" << endl;
if ((fight_time + 55).hour * 60 + (fight_time + 55).min >= end_time) {
break;
}
/*ÎäÊ¿±šžæÇé¿ö*/
for (int k = 0; k < city_sum + 1; k++) {
for (int i = 0; i < red.warrior_birth_set.size(); i++) {
if (red.warrior_birth_set[i]->get_life() > 0 && red.warrior_birth_set[i]->get_step() == k) {
std::cout << fight_time + 55 << ' ' << red.warrior_birth_set[i]->get_camp() << ' ' << red.warrior_birth_set[i]->get_kind() << ' ' << red.warrior_birth_set[i]->get_id() << " has " << red.warrior_birth_set[i]->get_weapons_sum()[0] << " sword " << red.warrior_birth_set[i]->get_weapons_sum()[1] << " bomb " << red.warrior_birth_set[i]->get_weapons_sum()[2] << " arrow and " << red.warrior_birth_set[i]->get_life() << " elements" << endl;
}
}
for (int i = 0; i < blue.warrior_birth_set.size(); i++) {
if (blue.warrior_birth_set[i]->get_life() > 0 && blue.warrior_birth_set[i]->get_step() == k) {
std::cout << fight_time + 55 << ' ' << blue.warrior_birth_set[i]->get_camp() << ' ' << blue.warrior_birth_set[i]->get_kind() << ' ' << blue.warrior_birth_set[i]->get_id() << " has " << blue.warrior_birth_set[i]->get_weapons_sum()[0] << " sword " << blue.warrior_birth_set[i]->get_weapons_sum()[1] << " bomb " << blue.warrior_birth_set[i]->get_weapons_sum()[2] << " arrow and " << blue.warrior_birth_set[i]->get_life() << " elements" << endl;
}
}
}
fight_time - 60;
}
}
using namespace std;
int main() {
int game_sum;
cin >> game_sum;
for (int i = 0; i < game_sum; i++) {
game(i);
fight_time = Fight_time();
}
}
有一点点乱码,凑合着看吧