魔兽终极版
呜呜呜……vs其实运行结果早就对了,只是dev一直发生随机错误,每次错的地方都不一样,错的数也不一样,简直像是量子错误…差点把我整崩溃了。结果找了足足一天才发现!原来是sword_attck这个成员变量在Soldier的构造函数里没有初始化!!!而且其实这个问题vs也有警告提醒我了,但我没在意。其实vs能跑,dev跑出来不对,还能有什么可能呢。哎,跟图老师交流过好像跟开了光一样,自己又静下心调了一会儿就发现问题了。

👆惯例纪念!
魔兽终极版审题很重要!幸好我开始写的比较晚,所以随时可以在群里查消息记录,这不禁让我少走了不少弯路。
放一个题目描述:



题目真的好长……
放ac代码!(其实不到700行!)
1 #define _CRT_SECURE_NO_WARNINGS 2 // 魔兽世界3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 3 // erase的用法!删了之后就不能再移下标了!! 4 //缴获武器啊!!!!!!!!!!!!!!!!!!!!! 5 #include<iostream> 6 #include<string> 7 #include<cmath> 8 #include<cstring> 9 #include<cstdio> 10 #include<vector> 11 #include<algorithm> 12 using namespace std; 13 //武士一共有 dragon 、ninja、iceman、lion、wolf 五种。每种武士都有编号、生命值、攻击力这三种属性 14 //红方司令部按照iceman、lion、wolf、ninja、dragon的顺序循环制造武士。 15 //蓝方司令部按照lion、dragon、ninja、iceman、wolf的顺序循环制造武士。 16 //编程规范:变量名中间字母大写,类名、函数名首字母大写 17 //莫名其妙的问题看看循环的下标是不是写错了! 18 const int interval[10] = { 5, 5, 10, 10, 5, 3, 2, 10, 5, 5 }; 19 int t, M, N, R, K, T; 20 int winb, winr; 21 22 class Command; //复合关系 23 24 class Soldier { //武士 25 protected: 26 Command* com; //属于哪个司令部 27 int kind; //是哪种武士 28 int id; //编号 29 int health;//生命值 30 int attack; //攻击力 31 int curcity; //当前所在城市 32 int loyalty; //忠诚度(狮子独有) 33 double morale;//士气(龙独有)降生后其司令部剩余生命元的数量除以造dragon所需的生命元数量 34 bool has_sword; //有没有剑 35 int sword_attack; //剑的攻击力 36 int arrow_num; //箭的数量 37 bool has_bomb; 38 39 public: 40 bool arrived; //有没有到达司令部 41 Soldier(Command* p, int kindd, int idd); 42 void Print(int time); 43 static int life[5]; //生命值 44 static string weaponname[3]; //武器 45 static string name[5]; //dragon 、ninja、iceman、lion、wolf 46 static int attacklist[5]; //攻击力 47 virtual void PrintInfo() {}; 48 void init_add_weapon(int id) { 49 if (id == 0) { 50 has_sword = true; 51 sword_attack = attack * 2 / 10; 52 if (sword_attack <= 0) { 53 sword_attack = 0; 54 has_sword = false; 55 } 56 } 57 else if (id == 2) arrow_num = 3; 58 else if (id == 1) has_bomb = true; 59 } 60 int& get_curcity() { return curcity; } 61 int get_id() const { return id; } 62 int& get_health() { return health; } 63 int get_kind() const { return kind; } 64 int& get_loyalty() { return loyalty; } 65 int& get_attack() { return attack; } 66 int launch_attack() const { return int(attack + sword_attack); } //攻击 67 int counter_attack() const{ return int(attack * 0.5) + sword_attack; } //反击 68 void under_attack(int damage) { health -= damage; } //被打 69 void sword_used() { //用一次剑,剑变钝 70 sword_attack = sword_attack * 8 / 10; 71 if (sword_attack <= 0) { 72 has_sword = false; 73 sword_attack = 0; 74 } 75 } 76 bool can_bomb(Soldier& enemy, int ishost) {//1 is host while 0 isn't host 77 bool ret = false; 78 if (has_bomb) { 79 if (ishost == 1 && enemy.health > launch_attack() && enemy.kind != 1 && health <= enemy.counter_attack()) //如果是自己先进攻 80 ret = true; 81 else if (ishost == 0 && health <= enemy.launch_attack())//如果是自己挨打 82 ret = true; 83 } 84 return ret; 85 } 86 void grab_weapon(Soldier& a) { //wolf抢武器 87 if (!has_sword) { 88 this->has_sword = a.has_sword; 89 this->sword_attack = a.sword_attack; 90 } 91 if (!this->has_bomb) this->has_bomb = a.has_bomb; 92 if (this->arrow_num == 0) this->arrow_num = a.arrow_num; 93 } 94 void decrease_morale() { morale -= 0.2; } 95 void decrease_loyalty() { loyalty -= K; } 96 void increase_morale() { morale += 0.2; } 97 void increase_health(int tmp) { health += tmp; } 98 void report(int col, int hour) {//000:55 blue wolf 2 has arrow(2),bomb,sword(23) 99 printf("%03d:55 ", hour); 100 if (col == 0) cout << "red "; else cout << "blue "; 101 cout << Soldier::name[kind]; 102 printf(" %d has ", id); 103 int cnt = 0; 104 if (arrow_num != 0) { 105 cout << "arrow(" << arrow_num << ")"; 106 cnt++; 107 } 108 if (has_bomb) { 109 if (cnt != 0) cout << ","; 110 cout << "bomb"; 111 cnt++; 112 } 113 if (has_sword) { 114 if (cnt != 0) cout << ","; 115 cout << "sword(" << sword_attack << ")"; 116 cnt++; 117 } 118 if (cnt == 0) cout << "no weapon"; 119 cout << endl; 120 } 121 friend class City; 122 }; 123 124 class Command { //司令部 125 private: 126 Soldier* mySoldier[1000]; //都有哪些士兵 127 int HP; //总血量 128 int color; //蓝方为1, 红方为0 129 int sNum[5]; //存放每种士兵的数量 130 int curID; //当前制造到了第curID个士兵 131 int curkind; //当前制造的种类(用在该颜色的order里是第几个表示,即相对编号) 132 bool flag; //用来记录生命值用完了没有 133 int accumulation; //暂时保存回收的生命元 134 bool isenemy; 135 vector<Soldier>enemies; 136 public: 137 static int order[2][5]; //制造顺序 138 void Init(int col, int life); 139 bool Creat(int time); 140 ~Command(); 141 string get_color() { 142 if (color == 0) return "red"; 143 else return "blue"; 144 } 145 void Match(int time) {//行军 146 for (int i = 1; i < curID; i++) { 147 if (mySoldier[i] == NULL) continue; //因为有狮子会逃走,所以会出现空指针 148 if (mySoldier[i]->get_health() <= 0) {//清理牺牲士兵 149 delete mySoldier[i]; 150 mySoldier[i] = NULL; 151 continue; 152 } 153 if (mySoldier[i]->get_kind() == 2 && color == 0 && (mySoldier[i]->get_curcity()+1)%2==0){//如果是红iceman 154 mySoldier[i]->get_health() = (mySoldier[i]->get_health()-9)>0 ? mySoldier[i]->get_health()-9:1; 155 mySoldier[i]->get_attack() += 20; 156 } 157 else if (mySoldier[i]->get_kind() == 2 && color == 1 && (N + 2- mySoldier[i]->get_curcity()) % 2 == 0) {//如果是蓝iceman 158 mySoldier[i]->get_health() = (mySoldier[i]->get_health() - 9) > 0 ? mySoldier[i]->get_health() - 9 : 1; 159 mySoldier[i]->get_attack() += 20; 160 } 161 if (color == 0 && mySoldier[i]->get_curcity() < N + 1) { 162 mySoldier[i]->get_curcity()++; 163 if (mySoldier[i]->get_curcity() == N + 1){ 164 if (isenemy == false) isenemy = true; 165 else winr = 1; //赢了 166 enemies.push_back(*mySoldier[i]); 167 } 168 } 169 else if (color == 1 && mySoldier[i]->get_curcity() > 0) { 170 mySoldier[i]->get_curcity()--; 171 if (mySoldier[i]->get_curcity() == 0) { 172 if (isenemy == false) { 173 isenemy = true; 174 } 175 else winb = 1;//赢了 176 enemies.push_back(*mySoldier[i]); 177 } 178 } 179 } 180 } 181 void Escape(int time) { 182 for (int i = 1; i < curID; i++) { 183 if (mySoldier[i] == NULL) continue; 184 if (mySoldier[i]->get_kind() == 3 && mySoldier[i]->get_loyalty() <= 0) { 185 printf("%03d:05 %s %s %d ran away\n", 186 time, get_color().c_str(), Soldier::name[mySoldier[i]->get_kind()].c_str(), mySoldier[i]->get_id()); 187 //从城市中删掉狮子 188 delete mySoldier[i]; 189 mySoldier[i] = NULL; 190 } 191 } 192 } 193 void increase_accumulation(int tmp) { accumulation += tmp; } 194 void clear_accumulation() { 195 HP += accumulation; 196 accumulation = 0; 197 } 198 void SentSoldier(); //送士兵到城市 199 int &get_HP() { return HP; } 200 void report(int col, int hour) { 201 for (unsigned int i = 0; i < enemies.size(); i++) { 202 enemies[i].report(col, hour); 203 } 204 } 205 friend class Soldier; //友元关系不可继承 206 friend class Dragon; 207 friend class Lion; 208 friend class City; 209 }; 210 211 class Dragon : public Soldier { 212 public: 213 Dragon(Command* p, int kindd, int idd) :Soldier(p, kindd, idd){ 214 init_add_weapon(idd % 3); 215 } 216 217 virtual void PrintInfo() { 218 printf("Its morale is %.2lf\n", morale); 219 } 220 }; 221 222 class Ninja : public Soldier { 223 public: 224 Ninja(Command* p, int kindd, int idd) :Soldier(p, kindd, idd) { 225 init_add_weapon(id % 3); 226 init_add_weapon((id+1) % 3); 227 } 228 void Printinfo() { 229 printf("It has a %s and a %s\n", weaponname[id % 3].c_str(), weaponname[(id + 1) % 3].c_str()); 230 } 231 }; 232 233 class Iceman : public Soldier { 234 public: 235 Iceman(Command* p, int kindd, int idd) :Soldier(p, kindd, idd) { 236 init_add_weapon(id % 3); 237 } 238 }; 239 240 class Lion : public Soldier { 241 public: 242 Lion(Command* p, int kindd, int idd) :Soldier(p, kindd, idd) {} 243 virtual void PrintInfo() { //狮子诞生 244 printf("Its loyalty is %d\n", loyalty); 245 } 246 }; 247 248 class Wolf : public Soldier { 249 public: 250 Wolf(Command* p, int kindd, int idd) :Soldier(p, kindd, idd) {} 251 }; 252 253 class City { 254 private: 255 int id; 256 int elements; //生命元 257 int flag; //旗子颜色:-1为平局,0为红,1为蓝 258 int history; //用来记上一场有效战争是谁赢了,-1为平局,0为红赢了一次,1为蓝赢了一次.这个没能体现双方被箭射死的情况 259 public: 260 Soldier* red; 261 Soldier* blue; 262 int last; //上一次的战争时间这里打仗了没有。-1是没打或平局,0是红赢,1是蓝赢 263 City(int idd = -1) { 264 id = idd; 265 elements = 0; 266 flag = -1; 267 history = -1; 268 last = -1; 269 red = NULL; 270 blue = NULL; 271 } 272 void Produce() { //+10个生命元 273 elements += 10; 274 } 275 void Sent_elements(int t) { 276 if (red == NULL && blue != NULL) { 277 blue->com->HP += elements; 278 printf("%03d:30 blue %s %d earned %d elements for his headquarter\n", t, Soldier::name[blue->get_kind()].c_str(), blue->get_id(), elements); 279 elements = 0; 280 } 281 else if (red != NULL && blue == NULL) { 282 red->com->HP += elements; 283 printf("%03d:30 red %s %d earned %d elements for his headquarter\n", t, Soldier::name[red->get_kind()].c_str(), red->get_id(), elements); 284 elements = 0; 285 } 286 }//送生命元 287 void RedShoot(City & ob, int t) { 288 if (red != NULL && red->arrow_num != 0 && id != N) {//如果城市里只有一方,被箭射死了,也应当处理一下 289 if (ob.blue != NULL) { 290 red->arrow_num--; 291 ob.blue->health -= R; 292 if (ob.blue->health <= 0) { 293 printf("%03d:35 red %s %d shot and killed blue %s %d\n", t, Soldier::name[red->get_kind()].c_str(), red->get_id(), 294 Soldier::name[ob.blue->get_kind()].c_str(), ob.blue->get_id()); 295 } 296 else 297 printf("%03d:35 red %s %d shot\n", t, Soldier::name[red->get_kind()].c_str(), red->get_id()); 298 } 299 } 300 } 301 void BlueShoot(City& ob, int t) { 302 if (blue != NULL && blue->arrow_num != 0 && id != 1) { 303 if (ob.red != NULL) { 304 blue->arrow_num--; 305 ob.red->health -= R; 306 if (ob.red->health <= 0) { 307 printf("%03d:35 blue %s %d shot and killed red %s %d\n", t, Soldier::name[blue->get_kind()].c_str(), blue->get_id(), 308 Soldier::name[ob.red->get_kind()].c_str(), ob.red->get_id()); 309 } 310 else 311 printf("%03d:35 blue %s %d shot\n", t, Soldier::name[blue->get_kind()].c_str(), blue->get_id()); 312 } 313 } 314 } 315 void Bomb(int t) {//判断用不用炸弹 316 if (red == NULL || blue == NULL) return; 317 if (red->health <= 0 || blue->health <= 0)return; //可能之前已经被射死了 318 int host; 319 if (flag == -1) host = id % 2 == 1 ? 0 : 1;//0 is red and 1 is blue 320 else host = flag; 321 bool result = red->can_bomb(*blue, !host); //!host很巧妙啊。看看红的要不要用炸弹 322 if (!result)//如果红的不行,看看蓝的能不能用炸弹 323 result = blue->can_bomb(*red, host); 324 else {// 如果红的会用炸弹 000:38 blue dragon 1 used a bomb and killed red lion 7 325 red->health = 0; 326 blue->health = 0; 327 printf("%03d:38 red %s %d used a bomb and killed blue %s %d\n", t, Soldier::name[red->get_kind()].c_str(), red->get_id(), 328 Soldier::name[blue->get_kind()].c_str(), blue->get_id()); 329 red = NULL; 330 blue = NULL; 331 return; 332 } 333 if (result == true) { //如果蓝的会用炸弹 334 red->health = 0; 335 blue->health = 0; 336 printf("%03d:38 blue %s %d used a bomb and killed red %s %d\n", t, Soldier::name[blue->get_kind()].c_str(), blue->get_id(), 337 Soldier::name[red->get_kind()].c_str(), red->get_id()); 338 red = NULL; 339 blue = NULL; 340 return; 341 } 342 } 343 void Battle(int t) {//如果城市里只有一方,被箭射死了,也应当处理一下 344 if (red == NULL || blue == NULL) { 345 if (red != NULL && blue == NULL) { 346 if (red->health <= 0) red = NULL; //本来只有一个人,那个人又被射死了 347 } 348 if (red == NULL && blue != NULL) { 349 if (blue->health <= 0) blue = NULL; 350 } 351 last = -1; return; 352 } 353 if (red->health <= 0 && blue->health <= 0) { //如果之前都被箭射死了,算没发生战斗 354 red = NULL; blue = NULL; last = -1; 355 return; 356 } 357 int winner = -1; //谁赢了,0为红,1为蓝 358 int redlife = 0, bluelife = 0; 359 bool arrow_die = false; //是不是用箭射死的 360 int host = -1;//判断是谁主动进攻 361 if (flag == -1) host = id % 2 == 1 ? 0 : 1;//0 is red and 1 is blue 362 else host = flag; 363 if (red->health <= 0) { winner = 1; arrow_die = true; }//红的之前被射死了,lion的生命值按0算 364 else if (blue->health <= 0) { winner = 0; arrow_die = true; }//蓝的之前被射死了 365 else { 366 redlife = red->health; 367 bluelife = blue->health; 368 if (!host) {//红的先进攻 000:40 red iceman 1 attacked blue lion 1 in city 1 with 20 elements and force 30 369 printf("%03d:40 red %s %d attacked blue %s %d in city %d with %d elements and force %d\n", t, Soldier::name[red->get_kind()].c_str(), 370 red->get_id(), Soldier::name[blue->get_kind()].c_str(), blue->get_id(), id, redlife, red->attack); 371 // printf("在进攻之前的蓝方生命值:%d\n",blue->health); 372 // printf("打的攻击力:%d\n", red->launch_attack()); 373 blue->health-=red->launch_attack(); 374 // printf("被打后蓝方生命值:%d\n",blue->health); 375 red->sword_used(); 376 if (blue->health <= 0) winner = 0; 377 else if (blue->health > 0 && blue->get_kind()!=1) {//反击 001:40 blue dragon 2 fought back against red lion 2 in city 1 378 printf("%03d:40 blue %s %d fought back against red %s %d in city %d\n", t, Soldier::name[blue->get_kind()].c_str(), blue->get_id(), 379 Soldier::name[red->get_kind()].c_str(), red->get_id(), id); 380 // printf("在反击之前的红方生命值:%d\n",red->health); 381 red->health -= blue->counter_attack(); 382 // printf("反击攻击力:%d\n", blue->counter_attack()); 383 // printf("在反击之后的红方生命值:%d\n",red->health); 384 blue->sword_used(); 385 if (red->health <= 0)winner = 1; 386 } 387 } 388 else { //蓝的先进攻,注意ninja不反击 389 printf("%03d:40 blue %s %d attacked red %s %d in city %d with %d elements and force %d\n", t, Soldier::name[blue->get_kind()].c_str(), 390 blue->get_id(), Soldier::name[red->get_kind()].c_str(), red->get_id(), id, bluelife, blue->attack); 391 // printf("在进攻之前的红方生命值:%d\n",red->health); 392 // printf("打的攻击力:%d\n", blue->launch_attack()); 393 red->health-=blue->launch_attack(); 394 // printf("被打后红方生命值:%d\n",red->health); 395 blue->sword_used(); 396 if (red->health <= 0) winner = 1; 397 else if (red->health > 0 && red->get_kind()!=1) {//反击 398 printf("%03d:40 red %s %d fought back against blue %s %d in city %d\n", t, Soldier::name[red->get_kind()].c_str(), red->get_id(), 399 Soldier::name[blue->get_kind()].c_str(), blue->get_id(), id); 400 // printf("在反击之前的蓝方生命值:%d\n",blue->health); 401 blue->health -= red->counter_attack(); 402 // printf("反击攻击力:%d\n", red->counter_attack()); 403 // printf("在反击之后的蓝方生命值:%d\n",blue->health); 404 red->sword_used(); 405 if (blue->health <= 0)winner = 0; 406 } 407 } 408 } 409 //战斗之后要处理:wolf缴获武器,没杀死对方的lion减少士气,死了的lion的生命值转移,龙欢呼,奖励生命元,回收生命元,换旗帜,更新history,清空指针 410 if (winner == 0) { //红方赢了 411 if (blue->get_kind() == 3)red->health += bluelife; //生命值转移 412 if (!arrow_die) printf("%03d:40 blue %s %d was killed in city %d\n", t, Soldier::name[blue->get_kind()].c_str(), blue->get_id(), id);//001:40 red lion 2 was killed in city 1 413 if (red->get_kind() == 0) { //dragon 增加士气并且欢呼 414 red->increase_morale(); 415 if ((host == 0) && red->morale > 0.8)//003:40 blue dragon 2 yelled in city 4 416 printf("%03d:40 red dragon %d yelled in city %d\n", t, red->get_id(), id); 417 } 418 else if (red->get_kind() == 4) { //wolf要夺武器 419 red->grab_weapon(*blue); 420 } 421 if (elements > 0) { //回收生命元(发生在奖励生命元之后,奖励有顺序,怎么解决?先设置一个暂时的变量accumulation来存) 422 red->com->increase_accumulation(elements); 423 printf("%03d:40 red %s %d earned %d elements for his headquarter\n", t, Soldier::name[red->get_kind()].c_str(), red->get_id(), elements); 424 elements = 0; 425 } 426 //004:40 blue flag raised in city 4 427 if(history==0){ //如果上一场也是红方赢了 428 if (flag != 0) { 429 flag = 0; 430 printf("%03d:40 red flag raised in city %d\n", t, id); 431 } 432 } 433 history = 0; 434 last = 0; 435 blue = NULL; 436 } 437 else if (winner == 1) { //蓝方赢了 438 if (red->get_kind() == 3) blue->health += redlife; //lion生命值转移 439 if (!arrow_die) printf("%03d:40 red %s %d was killed in city %d\n", t, Soldier::name[red->get_kind()].c_str(), red->get_id(), id);//001:40 red lion 2 was killed in city 1 440 if (blue->get_kind() == 0) { //dragon 增加士气并且欢呼 441 blue->increase_morale(); 442 if ((host == 1) && blue->morale > 0.8)//003:40 blue dragon 2 yelled in city 4 443 printf("%03d:40 blue dragon %d yelled in city %d\n", t, blue->get_id(), id); 444 } 445 else if (blue->get_kind() == 4) { //wolf要夺武器 446 blue->grab_weapon(*red); 447 } 448 if (elements > 0) { //回收生命元(发生在奖励生命元之后,奖励有顺序,怎么解决?先设置一个暂时的变量accumulation来存) 449 blue->com->increase_accumulation(elements); 450 printf("%03d:40 blue %s %d earned %d elements for his headquarter\n", t, Soldier::name[blue->get_kind()].c_str(), blue->get_id(), elements); 451 elements = 0; 452 } 453 //004:40 blue flag raised in city 4 454 if (history == 1) { //如果上一场也是蓝方赢了 455 if (flag != 1) { 456 flag = 1; 457 printf("%03d:40 blue flag raised in city %d\n", t, id); 458 } 459 } 460 history = 1; 461 last = 1; 462 red = NULL; 463 } 464 else {//平局 465 if (red->get_kind() == 0) red->decrease_morale();//dragon灰心丧气 466 else if (red->get_kind() == 3) red->decrease_loyalty();//狮子丧失信心 467 if (blue->get_kind() == 0) blue->decrease_morale(); 468 else if (blue->get_kind() == 3) blue->decrease_loyalty(); 469 history = -1; 470 last = -1; 471 if ((red->get_kind() == 0 && host == 0) && red->morale > 0.8) //dragon欢呼 472 printf("%03d:40 red dragon %d yelled in city %d\n", t, red->get_id(), id); 473 if ((blue->get_kind() == 0 && host == 1) && blue->morale > 0.8) 474 printf("%03d:40 blue dragon %d yelled in city %d\n", t, blue->get_id(), id); 475 } 476 } 477 void red_gain() { red->increase_health(8); } 478 void blue_gain() { blue->increase_health(8); } 479 void red_report(int hour) {//000:55 blue wolf 2 has arrow(2),bomb,sword(23) 480 if (red!=NULL) red->report(0, hour); 481 } 482 void blue_report(int hour) { 483 if (blue!=NULL) blue->report(1, hour); 484 } 485 friend class Command; 486 friend class Soldier; 487 }; 488 489 string Soldier::name[5] = { "dragon","ninja","iceman","lion","wolf" }; 490 int Soldier::life[5]; 491 int Soldier::attacklist[5]; 492 string Soldier::weaponname[3] = { "sword", "bomb", "arrow" };//编号分别为0,1,2 493 int Command::order[2][5] = { {2,3,4,1,0},{3,0,1,2,4} }; 494 vector<City>cities; 495 496 Soldier::Soldier(Command* p, int kindd, int idd) { //哪一方/士兵类型/id 497 com = p; 498 kind = kindd; 499 id = idd; 500 if (kindd != 3) 501 loyalty = 0; 502 else loyalty = p->HP - life[kindd]; 503 if (kindd != 0) 504 morale = 0; 505 else 506 morale = (double(p->HP) - Soldier::life[kindd]) / life[0]; 507 health = life[kindd]; 508 attack = attacklist[kindd]; 509 curcity = -1; 510 has_sword = false; 511 has_bomb = false; 512 arrow_num = 0; 513 sword_attack = 0; //操不会真的是你有问题吧!!! 514 arrived = false; 515 } 516 517 void Soldier::Print(int time) { 518 string color = "red"; 519 if (com->color == 1) color = "blue"; //char字符串可以直接用=初始化,但不能这么赋值 520 else color = "red"; 521 printf("%03d:00 %s %s %d born\n", 522 time, color.c_str(), name[kind].c_str(), id); 523 } 524 525 void Command::Init(int col, int life) { //初始化 526 color = col; 527 HP = life; 528 flag = false; 529 curID = 1; 530 curkind = 0; 531 accumulation = 0; 532 isenemy = false; 533 for (int i = 0; i < 1000; i++) 534 mySoldier[i] = NULL; 535 memset(sNum, 0, sizeof(sNum)); 536 } 537 538 //string Soldier::name[5]={"dragon","ninja","iceman","lion","wolf"}; 539 bool Command::Creat(int time) { 540 if (flag) return false; //已经不能再造了 541 int i, kind; 542 kind = order[color][curkind]; //这里是绝对编号 543 if (Soldier::life[kind] > HP) return false; 544 switch (kind) { //case里不能定义局部变量,除非加作用域符号{} 545 case 0: 546 {mySoldier[curID] = new Dragon(this, kind, curID); break; } 547 case 1: 548 {mySoldier[curID] = new Ninja(this, kind, curID); break; } 549 case 2: 550 {mySoldier[curID] = new Iceman(this, kind, curID); break; } 551 case 3: 552 {mySoldier[curID] = new Lion(this, kind, curID); break; } 553 case 4: 554 {mySoldier[curID] = new Wolf(this, kind, curID); break; } 555 } 556 mySoldier[curID]->get_curcity() = (color == 0 ? 0 : N + 1); //确定是在哪个大本营里 557 sNum[kind]++; 558 HP -= Soldier::life[kind]; 559 mySoldier[curID]->Print(time); 560 if (kind == 0) mySoldier[curID]->PrintInfo();//dragon 报morale 561 else if (kind == 3) mySoldier[curID]->PrintInfo(); //lion报loyalty 562 //各种序号要+1 563 curkind = (curkind + 1) % 5; 564 curID++; 565 return true; 566 } 567 568 void Command::SentSoldier() { //把士兵到城市 569 for (int i = 1; i < curID; i++) { 570 if (mySoldier[i] != NULL) { 571 if (color == 0) { 572 cities[mySoldier[i]->get_curcity()].red = mySoldier[i]; 573 } 574 else cities[mySoldier[i]->get_curcity()].blue = mySoldier[i]; 575 } 576 } 577 //000:10 red iceman 1 marched to city 1 with 20 elements and force 30 578 } 579 580 Command::~Command() { 581 for (int i = 1; i < curID; i++) 582 delete mySoldier[i]; 583 } 584 585 586 int main() { 587 // freopen("E:\\data.in", "r", stdin); 588 // freopen("E:\\datam.out", "w", stdout); 589 scanf("%d", &t); 590 for (int casetime = 1; casetime <= t; casetime++) { 591 Command Blue, Red; 592 printf("Case %d:\n", casetime); 593 scanf("%d %d %d %d %d", &M, &N, &R, &K, &T); 594 winb = 0; 595 winr = 0; 596 Blue.Init(1, M); 597 Red.Init(0, M); 598 for (int j = 0; j <= 4; j++) 599 scanf("%d", &Soldier::life[j]); 600 for (int j = 0; j <= 4; j++) 601 scanf("%d", &Soldier::attacklist[j]); 602 int time = 0; 603 for (int j = 0; j <= N + 1; j++) 604 cities.push_back(City(j)); 605 606 for (int timing = 0, con = 0, hour = -1; timing <= T; timing += interval[con], con = (con + 1) % 10) { 607 if (winb != 0 || winr != 0) break; 608 if (con == 0)hour++; 609 if (con == 0) {//整点造新战士 610 bool flagred = Red.Creat(hour); 611 bool flagblue = Blue.Creat(hour); 612 } 613 if (con == 1) { //狮子逃走 614 Red.Escape(hour); 615 Blue.Escape(hour); 616 } 617 if (con == 2) { //行军 618 Red.Match(hour); 619 Blue.Match(hour); 620 for (int i = 0; i < cities.size(); i++) { //先把所有指针都清空 621 cities[i].red = NULL; cities[i].blue = NULL; 622 } 623 Red.SentSoldier(); 624 Blue.SentSoldier(); 625 if (cities[0].blue != NULL&&cities[0].blue->arrived==false) {//001:10 red iceman 1 reached blue headquarter with 20 elements and force 30 626 printf("%03d:10 blue %s %d reached red headquarter with %d elements and force %d\n", hour, 627 Soldier::name[cities[0].blue->get_kind()].c_str(), cities[0].blue->get_id(), 628 cities[0].blue->get_health(), cities[0].blue->get_attack()); 629 cities[0].blue->arrived = true; 630 if(winb == 1) printf("%03d:10 red headquarter was taken\n", hour); 631 } 632 for (int i = 1; i < cities.size() - 1; i++) { //要按城市顺序输出 633 if (cities[i].red != NULL) { 634 printf("%03d:10 red %s %d marched to city %d with %d elements and force %d\n", hour, 635 Soldier::name[cities[i].red->get_kind()].c_str(), cities[i].red->get_id(), cities[i].red->get_curcity(), 636 cities[i].red->get_health(), cities[i].red->get_attack()); 637 } 638 if (cities[i].blue != NULL) { 639 printf("%03d:10 blue %s %d marched to city %d with %d elements and force %d\n", hour, 640 Soldier::name[cities[i].blue->get_kind()].c_str(), cities[i].blue->get_id(), cities[i].blue->get_curcity(), 641 cities[i].blue->get_health(), cities[i].blue->get_attack()); 642 } 643 } 644 if (cities[N + 1].red != NULL&&cities[N+1].red->arrived == false) {//001:10 red iceman 1 reached blue headquarter with 20 elements and force 30 645 printf("%03d:10 red %s %d reached blue headquarter with %d elements and force %d\n", hour, 646 Soldier::name[cities[N + 1].red->get_kind()].c_str(), cities[N + 1].red->get_id(), 647 cities[N + 1].red->get_health(), cities[N + 1].red->get_attack()); 648 cities[N + 1].red->arrived = true; 649 if(winr == 1) printf("%03d:10 blue headquarter was taken\n", hour); 650 651 } 652 if (winb != 0||winr != 0) break; 653 } 654 if (con == 3) { //城市产生生命元 655 for (int i = 1; i <= N; i++) 656 cities[i].Produce(); 657 } 658 if (con == 4) { //取走生命元 659 for (int i = 1; i <= N; i++) { 660 cities[i].Sent_elements(hour); 661 } 662 } 663 if (con == 5) {//放箭 664 for (int i = 1; i <= N; i++) { 665 cities[i].RedShoot(cities[i + 1],hour); 666 cities[i].BlueShoot(cities[i - 1], hour); 667 } 668 } 669 if (con == 6) { //判断用不用炸弹 670 for (int i = 1; i <= N; i++) { 671 cities[i].Bomb(hour); 672 } 673 } 674 if (con == 7) { // 发生战斗 675 for (int i = 1; i <= N; i++) 676 cities[i].Battle(hour); 677 for (int i = 1; i <= N; i++) { 678 if (cities[i].last == 0 && Red.get_HP() >= 8) { 679 cities[i].red_gain(); 680 Red.get_HP() -= 8; 681 } 682 } 683 for (int i = N; i >= 1; i--) { 684 if (cities[i].last == 1 && Blue.get_HP() >= 8) { 685 cities[i].blue_gain(); 686 Blue.get_HP() -= 8; 687 } 688 } 689 Red.clear_accumulation(); 690 Blue.clear_accumulation(); 691 } 692 if (con == 8) {//001:50 20 elements in red headquarter 693 printf("%03d:50 %d elements in red headquarter\n", hour, Red.get_HP()); 694 printf("%03d:50 %d elements in blue headquarter\n", hour, Blue.get_HP()); 695 } 696 if (con == 9) { 697 for (int i = 1; i <= N; i++) cities[i].red_report(hour); 698 Red.report(0, hour); 699 Blue.report(1, hour); //注意在总部的敌人也得报告 700 for (int i = 1; i <= N; i++) cities[i].blue_report(hour); 701 } 702 } 703 cities.clear(); 704 } 705 return 0; 706 }
再见了魔兽!

浙公网安备 33010602011771号