coursera_poj_魔兽世界终结版
五个下午的时间!!!!终于过了!!
有史以来做的最复杂的一个题
这是我迄今为止做的最复杂也最具有挑战的一个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上,需要改动一些地方
1 // World of Warcraft.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 6 #include <cstdio> 7 #include <string> 8 #include <cstdlib> 9 #include <cstring> 10 #include <iostream> 11 #include <vector> 12 #include <map> 13 #include <queue> 14 15 using namespace std; 16 #define _CRT_SECURE_NO_WARNINGS 17 int M, N, R, K, T;//基础数据 18 int SoldierBlood[5]; 19 int SoldierAd[5]; 20 int CurrentTime; 21 22 string toString(int t) 23 { 24 char buf[10]; 25 sprintf_s(buf, "%d", t); 26 string b = buf; 27 return b; 28 } 29 string printNowTime() 30 { 31 int t = CurrentTime; 32 int h = t / 60; 33 int m = t % 60; 34 string s; 35 s += h / 100 + '0'; 36 s += (h / 10) % 10 + '0'; 37 s += h % 10 + '0'; 38 s += ':'; 39 s += m / 10 + '0'; 40 s += m % 10 + '0'; 41 return s + ' '; 42 } 43 44 class Weapon 45 { 46 protected: 47 int Attack; 48 int Belong;//存放的soldier的uid 49 public: 50 int getAttack() 51 { 52 return Attack; 53 } 54 void setBelong(int t) 55 { 56 Belong = t; 57 } 58 virtual void init() = 0; 59 virtual int lose() = 0;//返回0的时候表示失去该武器 60 virtual int getType() = 0;//0 sword 1 bomb 2 arrow 61 virtual string getPrint() = 0; 62 virtual ~Weapon() {}; 63 }; 64 class Sword:public Weapon 65 { 66 public: 67 void init(); 68 int lose() 69 { 70 Attack *= 0.8; 71 return Attack; 72 } 73 int getType() 74 { 75 return 0; 76 } 77 void setAttack(int t) 78 { 79 Attack = t*0.2; 80 } 81 string getPrint() 82 { 83 return "sword(" + toString(Attack) + ')'; 84 } 85 }; 86 class Arrow :public Weapon 87 { 88 private: 89 90 public: 91 int time; 92 void init() 93 { 94 Attack = 0; 95 time = 3; 96 } 97 int lose() 98 { 99 time--; 100 return time; 101 } 102 int getType() 103 { 104 return 2; 105 } 106 string getPrint() 107 { 108 return "arrow(" + toString(time) + ')'; 109 } 110 }; 111 class Bomb :public Weapon 112 { 113 public: 114 void init() 115 { 116 Attack = 0; 117 } 118 int lose()//只能用一次,只要调用了就没了 119 { 120 return 0; 121 } 122 int getType() 123 { 124 return 1; 125 } 126 string getPrint() 127 { 128 return "bomb"; 129 } 130 }; 131 132 class Soldier 133 { 134 protected: 135 136 int Attack; 137 int Id; 138 int uId; 139 int Camp;//0:red,1:blue; 140 141 142 public: 143 int Blood;//<=0即为死亡 144 int BloodWin;// 145 int isLive;//0 die 1 live -1 runaway 146 int tArrow;//实现同时射箭的处理。先记录下中箭的数值,在战争开始时再结算中箭的伤害。 147 Arrow* hasArrow;//没有即为NULL 148 Bomb* hasBomb;// 149 int dieByArrow; 150 string getCampName() 151 { 152 if (Camp == 0) return "red"; 153 return "blue"; 154 } 155 string getPrint()//return like this "red lion 2" 156 { 157 string s; 158 s += getCampName() + ' ' + getName() + ' ' +toString(Id); 159 return s; 160 } 161 Soldier() 162 { 163 isLive = 1; 164 hasArrow = NULL; 165 hasBomb = NULL; 166 tArrow = 0; 167 BloodWin = 0; 168 dieByArrow = 0; 169 } 170 void attackByArrow(int t) 171 { 172 tArrow += t; 173 } 174 void loseByArrow() 175 { 176 loseBlood(tArrow); 177 tArrow = 0; 178 } 179 void loseBlood(int t)//降低生命值 180 { 181 Blood -= t; 182 if (Blood <= 0) 183 { 184 isLive = 0; 185 //Todo 186 die(); 187 } 188 } 189 int getBlood() 190 { 191 return Blood; 192 } 193 int getAttack() 194 { 195 return Attack; 196 } 197 void setId(int t) 198 { 199 Id = t; 200 } 201 int getId() 202 { 203 return Id; 204 } 205 void setuId(int t) 206 { 207 uId = t; 208 } 209 int getuId() 210 { 211 return uId; 212 } 213 void setCamp(int t) 214 { 215 Camp = t; 216 } 217 int getCamp() 218 { 219 return Camp; 220 } 221 virtual void init() = 0;//武士初始化,推迟到子类中实现 222 virtual int fight() = 0;//返回攻击的攻击力 223 virtual int fightback() = 0;//返回反击的攻击力 224 virtual int getType() = 0;//返回该武士的类型编号,0: dragon 、1:ninja、2:iceman、3:lion、4:wolf 225 virtual string getName() = 0;//返回该武士的类型 226 virtual void die() = 0;//死亡 227 virtual void winFight(Soldier* p,int t) = 0;//获胜 228 virtual void loseFight(Soldier* p,int t) = 0;//输掉 229 virtual void drawFight(Soldier* p,int t) = 0;//平局 230 virtual void loseWeapon(int t) = 0; 231 virtual void printWeapon() = 0; 232 virtual ~Soldier() {}//虚函数析构 233 }; 234 235 int getSwordAttack(Weapon* p)//fight时处理sword的事件,返回sword的攻击力 236 { 237 if (!p) return 0; 238 if (p->getType() != 0) return 0;//not sword,return 0 239 int t = p->getAttack(); 240 //p->lose();//sword 变钝 // 对打时处理吧 241 //if (p->getAttack() == 0) 242 //{ 243 // delete p; 244 // p = NULL; 245 //} 246 return t; 247 } 248 void getWeapon(Soldier * t,Weapon* &p, int code);//获得武器 249 250 class Dragon :public Soldier 251 { 252 private: 253 254 public: 255 double Morale; 256 Weapon* mWeapon;//NULL代表没有武器 257 Dragon() 258 { 259 Blood = SoldierBlood[0]; 260 Attack = SoldierAd[0]; 261 mWeapon = NULL; 262 } 263 double getMorale() 264 { 265 return Morale; 266 } 267 int getWeaponType()//-1 no weapon,0 sword 268 { 269 if (mWeapon == NULL) return -1; 270 return mWeapon->getType(); 271 } 272 void init(); 273 274 void die() 275 { 276 //if (mWeapon != NULL) delete mWeapon; 277 isLive = 0; 278 Blood = 0; 279 } 280 int getType() 281 { 282 return 0; 283 } 284 string getName() 285 { 286 return "dragon"; 287 } 288 void yell(int t);//judge whether to yell 289 290 void winFight(Soldier* p,int t) 291 { 292 BloodWin += 8; 293 Morale += 0.2; 294 yell(t); 295 } 296 297 void loseFight(Soldier * p,int t) 298 { 299 300 } 301 void drawFight(Soldier * p,int t) 302 { 303 Morale -= 0.2; 304 yell(t); 305 } 306 int fight() 307 { 308 int ad = Attack; 309 ad += getSwordAttack(mWeapon); 310 return ad; 311 } 312 int fightback() 313 { 314 int ad = Attack / 2; 315 ad += getSwordAttack(mWeapon); 316 return ad; 317 } 318 void loseWeapon(int t)//战斗后调用 319 { 320 if (mWeapon&&mWeapon->getType()==t) 321 { 322 if (mWeapon->lose() == 0) 323 { 324 //delete mWeapon; 325 mWeapon = NULL; 326 } 327 } 328 } 329 void printWeapon() 330 { 331 string s = printNowTime() + getPrint() + " has "; 332 if (mWeapon == NULL) 333 s += "no weapon"; 334 else 335 s += mWeapon->getPrint(); 336 puts(s.c_str()); 337 } 338 }; 339 class Ninja :public Soldier 340 { 341 private: 342 343 public: 344 Weapon *mWeapon1, *mWeapon2; 345 Ninja() 346 { 347 Blood = SoldierBlood[1]; 348 Attack = SoldierAd[1]; 349 mWeapon1 = NULL; 350 mWeapon2 = NULL; 351 } 352 int getWeaponType1() 353 { 354 if (mWeapon1 == NULL) return -1; 355 return mWeapon1->getType(); 356 } 357 int getWeaponType2() 358 { 359 if (mWeapon2 == NULL) return -1; 360 return mWeapon2->getType(); 361 } 362 int getType() 363 { 364 return 1; 365 } 366 string getName() 367 { 368 return "ninja"; 369 } 370 void init() 371 { 372 getWeapon(this,mWeapon1, Id % 3); 373 if (mWeapon1) mWeapon1->setBelong(getuId());//设置属于谁的 374 getWeapon(this,mWeapon2, (Id+1) % 3); 375 if (mWeapon2) mWeapon2->setBelong(getuId());//设置属于谁的 376 } 377 void die() 378 { 379 //if (mWeapon1) delete mWeapon1; 380 //if (mWeapon2) delete mWeapon2; 381 isLive = 0; 382 } 383 void winFight(Soldier *p, int t) 384 { 385 BloodWin += 8; 386 } 387 void loseFight(Soldier *p, int t) 388 { 389 390 } 391 void drawFight(Soldier *p, int t) 392 { 393 394 } 395 int fight() 396 { 397 int ad = Attack; 398 if(mWeapon1)ad += getSwordAttack(mWeapon1); 399 if(mWeapon2)ad += getSwordAttack(mWeapon2); 400 return ad; 401 } 402 void loseWeapon(int t)//战斗后调用 403 { 404 if (mWeapon1&&mWeapon1->getType()==t) 405 { 406 if (mWeapon1->lose()==0) 407 { 408 //delete mWeapon1; 409 mWeapon1 = NULL; 410 } 411 } 412 if (mWeapon2&&mWeapon2->getType() == t) 413 { 414 if (mWeapon2->lose()==0) 415 { 416 //delete mWeapon2; 417 mWeapon2 = NULL; 418 } 419 } 420 } 421 int fightback()//不反击,所以认为是0 422 { 423 return 0; 424 } 425 void printWeapon() 426 { 427 string s = printNowTime() + getPrint() + " has "; 428 if (mWeapon1 == NULL && mWeapon2 == NULL) 429 s += "no weapon"; 430 else if (mWeapon1 == NULL) 431 { 432 s += mWeapon2->getPrint(); 433 } 434 else if (mWeapon2 == NULL) 435 { 436 s += mWeapon1->getPrint(); 437 } 438 else 439 { 440 if (mWeapon1->getType() == 2) 441 s += mWeapon1->getPrint(); 442 if (mWeapon2->getType() == 2) 443 s += mWeapon2->getPrint(); 444 if (mWeapon1->getType() == 1) 445 { 446 if (s[s.size() - 1] != ' ') s += ','; 447 s += mWeapon1->getPrint(); 448 } 449 if (mWeapon2->getType() == 1) 450 { 451 if (s[s.size() - 1] != ' ') s += ','; 452 s += mWeapon2->getPrint(); 453 } 454 if (mWeapon1->getType() == 0) 455 { 456 if (s[s.size() - 1] != ' ') s += ','; 457 s += mWeapon1->getPrint(); 458 } 459 if (mWeapon2->getType() == 0) 460 { 461 if (s[s.size() - 1] != ' ') s += ','; 462 s += mWeapon2->getPrint(); 463 } 464 } 465 puts(s.c_str()); 466 } 467 }; 468 469 class Iceman :public Soldier 470 { 471 private: 472 473 public: 474 Weapon *mWeapon; 475 int Step; 476 Iceman() 477 { 478 Blood = SoldierBlood[2]; 479 Attack = SoldierAd[2]; 480 mWeapon = NULL; 481 Step = 0; 482 } 483 int getWeaponType() 484 { 485 if (mWeapon == NULL) return -1; 486 return mWeapon->getType(); 487 } 488 int getType() 489 { 490 return 2; 491 } 492 string getName() 493 { 494 return "iceman"; 495 } 496 void init() 497 { 498 getWeapon(this,mWeapon, Id % 3); 499 if (mWeapon) mWeapon->setBelong(getuId());//设置属于谁的 500 } 501 void die() 502 { 503 //if (mWeapon) delete mWeapon; 504 isLive = 0; 505 } 506 void winFight(Soldier *p, int t) 507 { 508 BloodWin += 8; 509 } 510 void loseFight(Soldier *p, int t) 511 { 512 513 } 514 void drawFight(Soldier *p, int t) 515 { 516 517 } 518 void loseWeapon(int t)//战斗后调用 519 { 520 if (mWeapon&&mWeapon->getType() == t) 521 { 522 if (mWeapon->lose()== 0) 523 { 524 //delete mWeapon; 525 mWeapon = NULL; 526 } 527 } 528 } 529 void Move() 530 { 531 Step++; 532 if (Step == 2) 533 { 534 Step = 0; 535 Blood -= 9; 536 Attack += 20; 537 if (Blood <= 0) Blood = 1; 538 } 539 } 540 int fight() 541 { 542 int ad = Attack; 543 ad += getSwordAttack(mWeapon); 544 return ad; 545 } 546 int fightback() 547 { 548 int ad = Attack / 2; 549 ad += getSwordAttack(mWeapon); 550 return ad; 551 } 552 void printWeapon() 553 { 554 string s = printNowTime() + getPrint() + " has "; 555 if (mWeapon == NULL) 556 s += "no weapon"; 557 else 558 s += mWeapon->getPrint(); 559 puts(s.c_str()); 560 } 561 }; 562 class Lion :public Soldier 563 { 564 private: 565 int Loyalty; 566 int tBlood;//要转移的Blood 567 public: 568 Lion() 569 { 570 Blood = SoldierBlood[3]; 571 Attack = SoldierAd[3]; 572 } 573 void moveBlood() 574 { 575 tBlood = Blood-tArrow; 576 } 577 void init(); 578 void die() 579 { 580 isLive = 0; 581 } 582 int getLoyalty() 583 { 584 return Loyalty; 585 } 586 void loseLoyalty() 587 { 588 Loyalty -= K; 589 } 590 int fight() 591 { 592 return Attack; 593 } 594 int fightback() 595 { 596 return Attack / 2; 597 } 598 void winFight(Soldier *p, int t) 599 { 600 BloodWin += 8; 601 } 602 void loseFight(Soldier *p, int t) 603 { 604 if(dieByArrow==0)p->Blood += tBlood; 605 } 606 void drawFight(Soldier *p, int t) 607 { 608 loseLoyalty(); 609 } 610 void loseWeapon(int t) 611 { 612 613 } 614 int getType() 615 { 616 return 3; 617 } 618 string getName() 619 { 620 return "lion"; 621 } 622 void runAway() 623 { 624 Blood = 0; 625 isLive = 0;//暂时先设置为0吧,逃跑了就当他死了 626 } 627 void printWeapon() 628 { 629 string s = printNowTime() + getPrint() + " has "; 630 s += "no weapon"; 631 puts(s.c_str()); 632 } 633 }; 634 class Wolf :public Soldier 635 { 636 private: 637 638 public: 639 //Weapon* mWeapon; 640 Weapon* swordWeapon; 641 Arrow* arrowWeapon; 642 Bomb* bombWeapon; 643 Wolf() 644 { 645 Blood = SoldierBlood[4]; 646 Attack = SoldierAd[4]; 647 } 648 /*int getWeaponType() 649 { 650 return WeaponType; 651 }*/ 652 void init() 653 { 654 swordWeapon = NULL; 655 arrowWeapon = NULL; 656 bombWeapon = NULL; 657 } 658 void die() 659 { 660 //if (swordWeapon) delete swordWeapon; 661 //if () 662 isLive = 0; 663 } 664 void winFight(Soldier *p, int t) 665 { 666 switch (p->getType()) 667 { 668 case 0: 669 { 670 takeWeapon(((Dragon *)p)->mWeapon); 671 break; 672 } 673 case 1: 674 { 675 takeWeapon(((Ninja *)p)->mWeapon1); 676 takeWeapon(((Ninja *)p)->mWeapon2); 677 break; 678 } 679 case 2: 680 { 681 takeWeapon(((Iceman *)p)->mWeapon); 682 break; 683 } 684 case 4: 685 { 686 takeWeapon(((Wolf *)p)->swordWeapon); 687 takeWeapon(((Wolf *)p)->bombWeapon); 688 takeWeapon(((Wolf *)p)->arrowWeapon); 689 break; 690 } 691 } 692 BloodWin += 8; 693 } 694 void loseFight(Soldier *p, int t) 695 { 696 697 } 698 void drawFight(Soldier *p, int t) 699 { 700 701 } 702 void takeWeapon(Weapon *p)//缴获武器 703 { 704 if (p == NULL) return; 705 if (p->getType() == 0&&swordWeapon==NULL) 706 { 707 swordWeapon = p; 708 } 709 if (p->getType() == 1 && bombWeapon == NULL) 710 { 711 bombWeapon =(Bomb *) p; 712 hasBomb = (Bomb *)p; 713 } 714 if (p->getType() == 2 && arrowWeapon == NULL) 715 { 716 arrowWeapon = (Arrow *)p; 717 hasArrow = (Arrow *)p; 718 } 719 } 720 int fight() 721 { 722 int ad = Attack; 723 ad += getSwordAttack(swordWeapon); 724 return ad; 725 } 726 int fightback() 727 { 728 int ad = Attack / 2; 729 ad += getSwordAttack(swordWeapon); 730 return ad; 731 } 732 void loseWeapon(int t)//战斗后调用 733 { 734 if (swordWeapon&&t==0) 735 { 736 if (swordWeapon->lose() == 0) 737 { 738 //delete swordWeapon; 739 swordWeapon = NULL; 740 } 741 } 742 if (bombWeapon&&t == 1) 743 { 744 if (bombWeapon->lose() == 0) 745 { 746 //delete bombWeapon; 747 bombWeapon = NULL; 748 } 749 } 750 if (arrowWeapon&&t == 2) 751 { 752 if (arrowWeapon->lose() == 0) 753 { 754 //delete arrowWeapon; 755 arrowWeapon = NULL; 756 } 757 } 758 } 759 int getType() 760 { 761 return 4; 762 } 763 string getName() 764 { 765 return "wolf"; 766 } 767 void printWeapon() 768 { 769 string s = printNowTime() + getPrint() + " has "; 770 if (swordWeapon == NULL && bombWeapon == NULL && arrowWeapon ==NULL) 771 s += "no weapon"; 772 else 773 { 774 if (arrowWeapon != NULL) s += arrowWeapon->getPrint(); 775 if (bombWeapon != NULL) 776 { 777 if (s[s.size() - 1] != ' ') s += ','; 778 s += bombWeapon->getPrint(); 779 } 780 if (swordWeapon != NULL) 781 { 782 if (s[s.size() - 1] != ' ') s += ','; 783 s += swordWeapon->getPrint(); 784 } 785 } 786 puts(s.c_str()); 787 } 788 }; 789 vector<Soldier *> mAllSoldier;//全部武士集合 790 791 class Base 792 { 793 public: 794 int Blood; 795 int BloodWin; 796 int SoidierNum;//从1开始编号 797 vector<Soldier*> mSoldier;//武士集合 798 int occupied;//0 no,1 799 void init()//初始化大本营 800 { 801 Blood = M; 802 SoidierNum = 0; 803 /*for (int i = 0; i <= mSoldier.size(); i++) 804 { 805 delete mSoldier[i]; 806 }*/ 807 mSoldier.clear(); 808 //mSoldier.push_back(NULL); 809 occupied = 0; 810 } 811 void tackBloodWin() 812 { 813 Blood += BloodWin; 814 BloodWin = 0; 815 } 816 virtual int creatwho(int t) = 0; 817 virtual int getCamp() = 0; 818 virtual string getCampName() = 0; 819 Soldier* CreatSoldier();//返回指向soldier的指针,创建失败的时候的返回null 820 }; 821 822 823 class RedBase :public Base 824 { 825 public: 826 int creatwho(int t) 827 { 828 return t % 5; 829 } 830 int getCamp() 831 { 832 return 0; 833 } 834 string getCampName() 835 { 836 return "red"; 837 } 838 }; 839 RedBase *RedBaseInstance;//红魔军实例 840 class BlueBase:public Base 841 { 842 public: 843 int creatwho(int t) 844 { 845 int k = t % 5; 846 switch (k) 847 { 848 case 0: 849 return 1; 850 case 1: 851 return 4; 852 case 2: 853 return 3; 854 case 3: 855 return 0; 856 case 4: 857 return 2; 858 } 859 } 860 int getCamp() 861 { 862 return 1; 863 } 864 string getCampName() 865 { 866 return "blue"; 867 } 868 }; 869 BlueBase *BlueBaseInstance;//蓝魔军实例 870 class City 871 { 872 private: 873 874 public: 875 int Id; 876 Soldier* RedSoldier; 877 Soldier* BlueSoldier; 878 int Blood; 879 int whoLastWin;//0 no 1 red 2 blue 880 int Flag;//as up 881 int FlagRaise; 882 int lastPrintFlag; 883 int whoFirst;//who first to attack,0 red,1 blue 884 int redReward, blueReward; 885 queue<string> output; 886 City(int t) 887 { 888 lastPrintFlag = 0; 889 Id = t; 890 Blood = 0; 891 whoLastWin = 0; 892 Flag = 0; 893 redReward = 0; 894 blueReward = 0; 895 FlagRaise = 0; 896 RedSoldier = NULL; 897 BlueSoldier = NULL; 898 while (!output.empty()) output.pop(); 899 } 900 void addBlood() 901 { 902 Blood += 10; 903 } 904 int takeBlood(int who)//拿走所有的生命元 905 { 906 int t = Blood; 907 Blood = 0; 908 string s = printNowTime(); 909 if (who == 0) 910 { 911 s += RedSoldier->getPrint(); 912 } 913 if (who == 1) 914 { 915 s += BlueSoldier->getPrint(); 916 } 917 s += " earned " +toString(t)+ " elements for his headquarter"; 918 puts(s.c_str()); 919 return t; 920 } 921 int takeWinBlood(int c, int who) 922 { 923 int t = Blood; 924 Blood = 0; 925 string s = printNowTime(); 926 if (who == 0) 927 { 928 s += RedSoldier->getPrint(); 929 } 930 if (who == 1) 931 { 932 s += BlueSoldier->getPrint(); 933 } 934 s += " earned " + toString(t) + " elements for his headquarter"; 935 //puts(s.c_str()); 936 output.push(s); 937 return t; 938 } 939 void printFlag(int p) 940 { 941 string s=printNowTime(); 942 if (Flag == 1) 943 { 944 s += "red flag raised in city " + toString(p); 945 } 946 else 947 { 948 s += "blue flag raised in city " + toString(p); 949 } 950 puts(s.c_str()); 951 } 952 void changeFlag(int p,int t)//t 1 red win ,t 2 blue win 953 { 954 //if (Flag == t) return; 955 if (t == 1) 956 { 957 if (whoLastWin == 1) 958 { 959 Flag = 1; 960 FlagRaise = 1; 961 //printFlag(p); 962 whoFirst = 0; 963 } 964 else 965 { 966 whoLastWin = 1; 967 } 968 } 969 else 970 { 971 if (whoLastWin == 2) 972 { 973 Flag = 2; 974 FlagRaise = 2; 975 //printFlag(p); 976 whoFirst = 1; 977 } 978 else 979 { 980 whoLastWin = 2; 981 } 982 } 983 } 984 985 }; 986 vector <City*> mAllCity; 987 988 void Init()//游戏初始化 989 { 990 cin >> M >> N >> R >> K >> T; 991 cin >> SoldierBlood[0] >> SoldierBlood[1] >> SoldierBlood[2] >> SoldierBlood[3] >> SoldierBlood[4]; 992 cin >> SoldierAd[0] >> SoldierAd[1] >> SoldierAd[2] >> SoldierAd[3] >> SoldierAd[4]; 993 994 N++;//使得编号N为bluebase 995 996 for (int i = 0; i < mAllSoldier.size(); i++) 997 { 998 delete mAllSoldier[i]; 999 } 1000 mAllSoldier.clear(); 1001 //mAllSoldier.push_back(NULL);//放入一个空指针占位 1002 1003 RedBaseInstance = new RedBase(); 1004 RedBaseInstance->init(); 1005 BlueBaseInstance = new BlueBase(); 1006 BlueBaseInstance->init(); 1007 1008 for (int i = 0; i < mAllCity.size(); i++) 1009 { 1010 delete mAllCity[i]; 1011 } 1012 mAllCity.clear(); 1013 for (int i = 0; i <= N; i++)//0号city为redbase,N号为bluebase 1014 { 1015 1016 mAllCity.push_back(new City(i)); 1017 mAllCity[i]->whoFirst = (i+1) % 2; 1018 } 1019 } 1020 1021 string printTime() 1022 { 1023 int t = CurrentTime; 1024 int h = t / 60; 1025 int m = t % 60; 1026 string s; 1027 s += h / 100 + '0'; 1028 s += (h / 10) % 10 + '0'; 1029 s += h % 10 + '0'; 1030 s += ':'; 1031 s += m / 10 + '0'; 1032 s += m % 10 + '0'; 1033 return s; 1034 } 1035 1036 void printBorn(string s, Soldier* p) 1037 { 1038 if (p == NULL) return;//表示没有创建 1039 s += ' ' + p->getPrint() + " born"; 1040 puts(s.c_str()); 1041 if (p->getType() == 0)//is dragon 1042 { 1043 double t = ((Dragon*)(p))->getMorale(); 1044 //t = (double)((int)(t * 100 + 0.5)) / 100.0;//四舍五入 1045 //oj上的测试数据并没有四舍五入。。。直接截尾取得两位有效数字,坑啊 1046 printf("Its morale is %.2lf\n", t); 1047 } 1048 if (p->getType() == 3)//is lion 1049 { 1050 printf("Its loyalty is %d\n", ((Lion*)(p))->getLoyalty()); 1051 } 1052 } 1053 void printLionRun(string s, Lion* p)//判断lion是否逃跑以及相应信息的输出 1054 { 1055 p->runAway(); 1056 s += " lion " + toString(p->getId()) + " ran away"; 1057 puts(s.c_str()); 1058 } 1059 int printMove(int c) 1060 { 1061 Soldier *p = NULL; 1062 string s; 1063 int f = 0; 1064 1065 p = mAllCity[c]->RedSoldier;//red 1066 if (p) 1067 { 1068 s = printNowTime() + p->getPrint(); 1069 if (c == 0) 1070 s += " reached red headquarter"; 1071 else if (c == N) s += " reached blue headquarter"; 1072 else s += " marched to city " + toString(c); 1073 s += " with " + toString(p->getBlood()) + " elements and force " + toString(p->getAttack()); 1074 puts(s.c_str()); 1075 1076 if (BlueBaseInstance->occupied==2&&c == N) 1077 { 1078 f = 1; 1079 printf("%sblue headquarter was taken\n", printNowTime().c_str()); 1080 } 1081 } 1082 1083 1084 p = mAllCity[c]->BlueSoldier;//blue 1085 if (p) 1086 { 1087 s = printNowTime() + p->getPrint(); 1088 if (c == 0) 1089 s += " reached red headquarter"; 1090 else if (c == N) s += " reached blue headquarter"; 1091 else s += " marched to city " + toString(c); 1092 s += " with " + toString(p->getBlood()) + " elements and force " + toString(p->getAttack()); 1093 puts(s.c_str()); 1094 1095 if (RedBaseInstance->occupied==2&&c == 0) 1096 { 1097 f = 1; 1098 printf("%sred headquarter was taken\n", printNowTime().c_str()); 1099 } 1100 } 1101 1102 return f; 1103 } 1104 int checkBomb(Soldier *p,Soldier *q,int t)//返回1即使需要使用bomb 1105 { 1106 //t 谁先攻击,这个应该在city中处理,0 red 1 blue 1107 if (q->getBlood() - q->tArrow <= 0) return 0; 1108 if (p->getBlood() - p->tArrow <= 0) return 0; 1109 int a = 0, b = 0,c = 0,d = 0; 1110 if (t == 0)//p 先攻击 1111 { 1112 a = p->fight(); 1113 b = q->getBlood() - q->tArrow; 1114 1115 if (a < b)//q 没死 反击 1116 { 1117 c = q->fightback(); 1118 d = p->getBlood() - p->tArrow; 1119 if (c >= d) return 1; 1120 } 1121 } 1122 else//q 先攻击 1123 { 1124 a = q->fight(); 1125 b = p->getBlood() - p->tArrow; 1126 1127 if (a >= b) return 1;//q 打死了 p就返回1 1128 } 1129 return 0; 1130 } 1131 int checkBomb2(Soldier *p, Soldier *q, int t) 1132 { 1133 //t 谁先攻击,这个应该在city中处理,0 red 1 blue 1134 if (q->getBlood() - q->tArrow <= 0) return 0; 1135 if (p->getBlood() - p->tArrow <= 0) return 0; 1136 int a = 0, b = 0, c = 0, d = 0; 1137 if (t == 0) 1138 { 1139 a = p->fight(); 1140 b = q->getBlood() - q->tArrow; 1141 if (a >= b) return 1; 1142 } 1143 else 1144 { 1145 c = q->fight(); 1146 d = p->getBlood() - p->tArrow; 1147 if (c < d) 1148 { 1149 a = p->fightback(); 1150 b = q->getBlood() - q->tArrow; 1151 if (a >= b) return 1; 1152 } 1153 } 1154 return 0; 1155 } 1156 int checkArrow(Soldier* p)//结算arrow的伤害并检查是否被射死了,返回1是被射死了 1157 { 1158 if (!p) return -1; 1159 p->loseBlood(p->tArrow); 1160 p->tArrow = 0; 1161 if (p->getBlood() <= 0) 1162 { 1163 p->dieByArrow = 1; 1164 p->die(); 1165 return 1; 1166 } 1167 return 0; 1168 } 1169 int printFight(int i,Soldier *p,Soldier *q)//返回0,就是q死了 1170 { 1171 int a = p->fight(); 1172 int b = q->getBlood(); 1173 p->loseWeapon(0);//武器损耗 1174 q->loseBlood(a); 1175 string s = printTime(); 1176 string sp, sq; 1177 if (p->getCamp() == 0) sp = " red "; 1178 else sp = " blue "; 1179 if (q->getCamp() == 0) sq = " red "; 1180 else sq = " blue "; 1181 s += sp + p->getName() + ' ' + toString(p->getId()) + " attacked" + sq; 1182 s += q->getName() + ' ' + toString(q->getId()) + " in city " + toString(i); 1183 s += " with " + toString(p->getBlood()) + " elements and force " + toString(p->getAttack()); 1184 //puts(s.c_str()); 1185 mAllCity[i]->output.push(s); 1186 return q->isLive; 1187 } 1188 int printFightBack(int i, Soldier *p, Soldier *q) 1189 { 1190 if (p->getType() == 1)//ninjia no fightback 1191 return 1; 1192 int a = p->fightback(); 1193 int b = q->getBlood(); 1194 p->loseWeapon(0); 1195 q->loseBlood(a); 1196 string s = printNowTime(); 1197 s += p->getPrint() + " fought back against " + q->getPrint() + " in city " + toString(i); 1198 //puts(s.c_str()); 1199 mAllCity[i]->output.push(s); 1200 return q->isLive; 1201 } 1202 void printKilled(int i,Soldier *p) 1203 { 1204 string s = printTime() + ' ' + p->getCampName() + ' '; 1205 s += p->getName() + ' ' + toString(p->getId()) + " was killed in city " + toString(i); 1206 //puts(s.c_str()); 1207 mAllCity[i]->output.push(s); 1208 } 1209 void afterbattle(int i, int t)//在city i 战斗结束后的事情,0 平局 1 red win 2 blue win 1210 { 1211 if (t == 0)//平局 1212 { 1213 mAllCity[i]->RedSoldier->drawFight(mAllCity[i]->BlueSoldier, i); 1214 mAllCity[i]->BlueSoldier->drawFight(mAllCity[i]->RedSoldier, i); 1215 mAllCity[i]->whoLastWin = 0; 1216 } 1217 if (t == 1)//red win 1218 { 1219 1220 mAllCity[i]->RedSoldier->winFight(mAllCity[i]->BlueSoldier, i); 1221 mAllCity[i]->BlueSoldier->loseFight(mAllCity[i]->RedSoldier, i); 1222 RedBaseInstance->BloodWin += mAllCity[i]->takeWinBlood(i, 0); 1223 mAllCity[i]->changeFlag(i,1); 1224 } 1225 if (t == 2)//blue win 1226 { 1227 1228 mAllCity[i]->RedSoldier->loseFight(mAllCity[i]->BlueSoldier, i); 1229 mAllCity[i]->BlueSoldier->winFight(mAllCity[i]->RedSoldier, i); 1230 BlueBaseInstance->BloodWin += mAllCity[i]->takeWinBlood(i, 1); 1231 mAllCity[i]->changeFlag(i,2); 1232 } 1233 } 1234 void redReward() 1235 { 1236 for (int i = N - 1; i > 0; i--)//red reward 1237 { 1238 if (!mAllCity[i]->RedSoldier) continue; 1239 while (mAllCity[i]->RedSoldier->BloodWin>0) 1240 if (RedBaseInstance->Blood >= 8) 1241 { 1242 RedBaseInstance->Blood -= 8; 1243 mAllCity[i]->RedSoldier->Blood += 8; 1244 mAllCity[i]->RedSoldier->BloodWin -= 8; 1245 mAllCity[i]->redReward = 1; 1246 } 1247 //mAllCity[i]->RedSoldier->BloodWin = 0; 1248 } 1249 } 1250 void blueReward() 1251 { 1252 for (int i = 1; i < N; i++)//blue reward 1253 { 1254 if (!mAllCity[i]->BlueSoldier) continue; 1255 while (mAllCity[i]->BlueSoldier->BloodWin>0) 1256 if (BlueBaseInstance->Blood >= 8) 1257 { 1258 BlueBaseInstance->Blood -= 8; 1259 mAllCity[i]->BlueSoldier->Blood += 8; 1260 mAllCity[i]->BlueSoldier->BloodWin -= 8; 1261 mAllCity[i]->blueReward = 1; 1262 } 1263 //mAllCity[i]->BlueSoldier->BloodWin = 0; 1264 } 1265 } 1266 1267 1268 void Game()//游戏进程 1269 { 1270 CurrentTime = -1; 1271 Soldier *RedOccSoldier = NULL; 1272 Soldier *BlueOccSoldier = NULL; 1273 while (CurrentTime <= T) 1274 { 1275 CurrentTime++; 1276 if (CurrentTime > T) return; 1277 int CurrentMinute = CurrentTime % 60; 1278 string st = printTime(); 1279 switch (CurrentMinute) 1280 { 1281 case 0: 1282 { 1283 string s = st; 1284 //red 1285 Soldier* p = RedBaseInstance->CreatSoldier(); 1286 printBorn(s, p); 1287 1288 //blue 1289 p = BlueBaseInstance->CreatSoldier(); 1290 printBorn(s, p); 1291 1292 break; 1293 } 1294 case 5: 1295 { 1296 for (int i = 0; i <= N; i++)//从西向东 1297 { 1298 //red 1299 if (mAllCity[i]->RedSoldier&&mAllCity[i]->RedSoldier->getType() == 3)//is lion 1300 if (i != N)//not at bluebase 1301 { 1302 Lion *p = (Lion *)mAllCity[i]->RedSoldier; 1303 string s = st; 1304 s += " red"; 1305 if (p->getLoyalty() <= 0) 1306 { 1307 printLionRun(s, p); 1308 mAllCity[i]->RedSoldier = NULL; 1309 } 1310 } 1311 1312 //blue 1313 if (mAllCity[i]->BlueSoldier&&mAllCity[i]->BlueSoldier->getType() == 3)//is lion 1314 if (i != 0)//not at redbase 1315 { 1316 Lion *p = (Lion *)mAllCity[i]->BlueSoldier; 1317 string s = st; 1318 s += " blue"; 1319 if (p->getLoyalty() <= 0) 1320 { 1321 printLionRun(s, p); 1322 mAllCity[i]->BlueSoldier = NULL; 1323 } 1324 } 1325 } 1326 break; 1327 } 1328 case 10: 1329 { 1330 int f = 0;//1即为占领了 1331 1332 //red 1333 for (int i = N; i > 0; i--) 1334 { 1335 if (i == N && mAllCity[N]->RedSoldier) RedOccSoldier = mAllCity[N]->RedSoldier; 1336 mAllCity[i]->RedSoldier = mAllCity[i - 1]->RedSoldier; 1337 if (mAllCity[i]->RedSoldier&&mAllCity[i]->RedSoldier->getType() == 2) 1338 { 1339 ((Iceman*)mAllCity[i]->RedSoldier)->Move(); 1340 } 1341 } 1342 mAllCity[0]->RedSoldier = NULL; 1343 if (mAllCity[N]->RedSoldier) { BlueBaseInstance->occupied++; } 1344 1345 1346 //blue 1347 for (int i = 0; i < N; i++) 1348 { 1349 if (i == 0 && mAllCity[0]->BlueSoldier) BlueOccSoldier = mAllCity[0]->BlueSoldier; 1350 mAllCity[i]->BlueSoldier = mAllCity[i + 1]->BlueSoldier; 1351 if (mAllCity[i]->BlueSoldier&&mAllCity[i]->BlueSoldier->getType() == 2) 1352 { 1353 ((Iceman*)mAllCity[i]->BlueSoldier)->Move(); 1354 } 1355 } 1356 mAllCity[N]->BlueSoldier = NULL; 1357 if (mAllCity[0]->BlueSoldier) RedBaseInstance->occupied++; 1358 for (int i = 0; i <= N; i++) 1359 { 1360 if (printMove(i)) f = 1; 1361 } 1362 1363 //be occupied? 1364 if (f) return; 1365 break; 1366 } 1367 case 20: 1368 { 1369 for (int i = 1; i < N; i++)//不包括红蓝司令部 1370 { 1371 mAllCity[i]->addBlood(); 1372 } 1373 /*RedBaseInstance->Blood += 10; 1374 BlueBaseInstance->Blood += 10;*/ 1375 break; 1376 } 1377 case 30: 1378 { 1379 for (int i = 1; i < N; i++) 1380 { 1381 if (mAllCity[i]->RedSoldier != NULL&&mAllCity[i]->BlueSoldier == NULL) 1382 RedBaseInstance->Blood += mAllCity[i]->takeBlood(0); 1383 if (mAllCity[i]->RedSoldier == NULL&&mAllCity[i]->BlueSoldier != NULL) 1384 BlueBaseInstance->Blood += mAllCity[i]->takeBlood(1); 1385 } 1386 break; 1387 } 1388 case 35: 1389 { 1390 //red 1391 for (int i = 1; i < N; i++) 1392 { 1393 Soldier *p = mAllCity[i]->RedSoldier; 1394 //if (!p) continue; 1395 if (i < N-1) 1396 { 1397 if (p&&p->hasArrow) 1398 { 1399 Arrow *q = p->hasArrow; 1400 if (mAllCity[i + 1]->BlueSoldier) 1401 { 1402 mAllCity[i + 1]->BlueSoldier->tArrow += R; 1403 1404 if (q->lose() == 0)//返回值为0说明arrow已经消失 1405 { 1406 //delete q; 1407 int t = p->getType(); 1408 switch (t) 1409 { 1410 case 0: 1411 { 1412 ((Dragon *)p)->mWeapon = NULL; 1413 break; 1414 } 1415 case 1: 1416 { 1417 if (((Ninja *)p)->mWeapon1 && ((Ninja *)p)->mWeapon1->getType() == 2) ((Ninja *)p)->mWeapon1 = NULL; 1418 if (((Ninja *)p)->mWeapon2 && ((Ninja *)p)->mWeapon2->getType() == 2) ((Ninja *)p)->mWeapon2 = NULL; 1419 break; 1420 } 1421 case 2: 1422 { 1423 ((Iceman *)p)->mWeapon = NULL; 1424 break; 1425 } 1426 case 4: 1427 { 1428 ((Wolf *)p)->arrowWeapon = NULL; 1429 break; 1430 } 1431 } 1432 p->hasArrow = NULL; 1433 } 1434 string s = st + " red "; 1435 s += mAllCity[i]->RedSoldier->getName() + ' ' + toString(mAllCity[i]->RedSoldier->getId()) + " shot"; 1436 if (mAllCity[i + 1]->BlueSoldier&&mAllCity[i + 1]->BlueSoldier->getBlood() <= R) 1437 s += " and killed blue " + mAllCity[i + 1]->BlueSoldier->getName() + ' ' + toString(mAllCity[i + 1]->BlueSoldier->getId()); 1438 puts(s.c_str()); 1439 } 1440 } 1441 } 1442 //blue 1443 if (i > 1) 1444 { 1445 Soldier *p = mAllCity[i]->BlueSoldier; 1446 //if (!p) continue; 1447 if (p&&p->hasArrow) 1448 { 1449 Arrow *q = p->hasArrow; 1450 if (mAllCity[i - 1]->RedSoldier) 1451 { 1452 mAllCity[i - 1]->RedSoldier->tArrow += R; 1453 if (q->lose() == 0)//返回值为0说明arrow已经消失 1454 { 1455 //delete q; 1456 int t = p->getType(); 1457 switch (t) 1458 { 1459 case 0: 1460 { 1461 ((Dragon *)p)->mWeapon = NULL; 1462 break; 1463 } 1464 case 1: 1465 { 1466 if (((Ninja *)p)->mWeapon1 && ((Ninja *)p)->mWeapon1->getType() == 2) ((Ninja *)p)->mWeapon1 = NULL; 1467 if (((Ninja *)p)->mWeapon2 && ((Ninja *)p)->mWeapon2->getType() == 2) ((Ninja *)p)->mWeapon2 = NULL; 1468 break; 1469 } 1470 case 2: 1471 { 1472 ((Iceman *)p)->mWeapon = NULL; 1473 break; 1474 } 1475 case 4: 1476 { 1477 ((Wolf *)p)->arrowWeapon = NULL; 1478 break; 1479 } 1480 } 1481 p->hasArrow = NULL; 1482 } 1483 string s = st + " blue "; 1484 s += mAllCity[i]->BlueSoldier->getName() + ' ' + toString(mAllCity[i]->BlueSoldier->getId()) + " shot"; 1485 if (mAllCity[i - 1]->RedSoldier&&mAllCity[i - 1]->RedSoldier->getBlood() <= R) 1486 s += " and killed red " + mAllCity[i - 1]->RedSoldier->getName() + ' ' + toString(mAllCity[i - 1]->RedSoldier->getId()); 1487 puts(s.c_str()); 1488 } 1489 } 1490 } 1491 } 1492 1493 break; 1494 } 1495 case 38: 1496 { 1497 for (int i = 1; i < N; i++) 1498 { 1499 string s = st; 1500 Soldier *p = mAllCity[i]->RedSoldier; 1501 if (p&&p->hasBomb) 1502 { 1503 if (mAllCity[i]->BlueSoldier&&checkBomb(p,mAllCity[i]->BlueSoldier,mAllCity[i]->whoFirst)) 1504 { 1505 p->loseWeapon(1); 1506 s = st; 1507 s += " red "+p->getName()+' '+toString(p->getId())+" used a bomb and killed blue "; 1508 s += mAllCity[i]->BlueSoldier->getName()+' '+toString(mAllCity[i]->BlueSoldier->getId()); 1509 puts(s.c_str()); 1510 p->die(); 1511 mAllCity[i]->BlueSoldier->die(); 1512 mAllCity[i]->RedSoldier = NULL; 1513 mAllCity[i]->BlueSoldier = NULL; 1514 } 1515 } 1516 1517 s = st; 1518 p = mAllCity[i]->BlueSoldier; 1519 if (p&&p->hasBomb) 1520 { 1521 if (mAllCity[i]->RedSoldier&&checkBomb2(mAllCity[i]->RedSoldier,p, mAllCity[i]->whoFirst)) 1522 { 1523 p->loseWeapon(1); 1524 s = st; 1525 s += " blue " + p->getName() + ' ' + toString(p->getId()) + " used a bomb and killed red "; 1526 s += mAllCity[i]->RedSoldier->getName() + ' ' + toString(mAllCity[i]->RedSoldier->getId()); 1527 puts(s.c_str()); 1528 p->die(); 1529 mAllCity[i]->RedSoldier->die(); 1530 mAllCity[i]->RedSoldier = NULL; 1531 mAllCity[i]->BlueSoldier = NULL; 1532 } 1533 } 1534 1535 } 1536 break; 1537 } 1538 case 40: 1539 { 1540 for (int i = 1; i < N; i++)//记录lion战斗前的blood 1541 { 1542 if (mAllCity[i]->RedSoldier&&mAllCity[i]->RedSoldier->getType() == 3) 1543 ((Lion *)mAllCity[i]->RedSoldier)->moveBlood(); 1544 if (mAllCity[i]->BlueSoldier&&mAllCity[i]->BlueSoldier->getType() == 3) 1545 ((Lion *)mAllCity[i]->BlueSoldier)->moveBlood(); 1546 } 1547 for (int i = 1; i < N; i++) 1548 { 1549 //先处理arrow事件 1550 int r = checkArrow(mAllCity[i]->RedSoldier); 1551 int b = checkArrow(mAllCity[i]->BlueSoldier); 1552 if (r == -1 || b == -1) 1553 { 1554 if (r==1) mAllCity[i]->RedSoldier = NULL; 1555 if (b==1) mAllCity[i]->BlueSoldier = NULL; 1556 continue;//no fight 1557 } 1558 if (r == 1 && b == 1) { mAllCity[i]->RedSoldier = NULL; mAllCity[i]->BlueSoldier = NULL; }//all die,no fight 1559 if (r == 0 && b == 1) { afterbattle(i, 1); mAllCity[i]->BlueSoldier = NULL; redReward(); }//blue die,red win 1560 if (r == 1 && b == 0) { afterbattle(i, 2); mAllCity[i]->RedSoldier = NULL; blueReward(); }//blue win 1561 1562 if (r == 0 && b == 0)//all not die,begin to fight 1563 { 1564 int t = mAllCity[i]->whoFirst; 1565 if (t == 0)//red attack,blue fightback 1566 { 1567 int tmp = printFight(i, mAllCity[i]->RedSoldier, mAllCity[i]->BlueSoldier); 1568 if (tmp)//没杀死,等待反击 1569 { 1570 int tmp2=printFightBack(i, mAllCity[i]->BlueSoldier, mAllCity[i]->RedSoldier); 1571 if (tmp2)//反击没死,平局 1572 { 1573 afterbattle(i, 0); 1574 } 1575 else// blue win 1576 { 1577 printKilled(i, mAllCity[i]->RedSoldier); 1578 afterbattle(i, 2); 1579 mAllCity[i]->RedSoldier = NULL; 1580 } 1581 } 1582 else//杀死了,red win 1583 { 1584 printKilled(i, mAllCity[i]->BlueSoldier); 1585 afterbattle(i, 1); 1586 mAllCity[i]->BlueSoldier = NULL; 1587 } 1588 } 1589 else 1590 { 1591 int tmp = printFight(i, mAllCity[i]->BlueSoldier, mAllCity[i]->RedSoldier); 1592 if (tmp)//没杀死,等待反击 1593 { 1594 int tmp2 = printFightBack(i, mAllCity[i]->RedSoldier, mAllCity[i]->BlueSoldier); 1595 if (tmp2)//反击没死,平局 1596 { 1597 afterbattle(i, 0); 1598 } 1599 else// red win 1600 { 1601 printKilled(i, mAllCity[i]->BlueSoldier); 1602 afterbattle(i, 1); 1603 mAllCity[i]->BlueSoldier = NULL; 1604 } 1605 } 1606 else//杀死了,blue win 1607 { 1608 printKilled(i, mAllCity[i]->RedSoldier); 1609 afterbattle(i, 2); 1610 mAllCity[i]->RedSoldier = NULL; 1611 } 1612 } 1613 } 1614 } 1615 redReward(); 1616 blueReward(); 1617 RedBaseInstance->tackBloodWin(); 1618 BlueBaseInstance->tackBloodWin(); 1619 for (int i = 1; i < N; i++)//output 1620 { 1621 while (!mAllCity[i]->output.empty()) 1622 { 1623 puts(mAllCity[i]->output.front().c_str()); 1624 mAllCity[i]->output.pop(); 1625 } 1626 /*if (mAllCity[i]->redReward) 1627 { 1628 mAllCity[i]->redReward = 0; 1629 string s = printNowTime() + mAllCity[i]->RedSoldier->getPrint() + " earned 8 elements for his headquarter"; 1630 puts(s.c_str()); 1631 } 1632 if (mAllCity[i]->blueReward) 1633 { 1634 mAllCity[i]->blueReward = 0; 1635 string s = printNowTime() + mAllCity[i]->BlueSoldier->getPrint() + " earned 8 elements for his headquarter"; 1636 puts(s.c_str()); 1637 }*/ 1638 if (mAllCity[i]->FlagRaise == 1&& mAllCity[i]->lastPrintFlag!=1) 1639 { 1640 mAllCity[i]->FlagRaise = 0; 1641 mAllCity[i]->lastPrintFlag = 1; 1642 string s = printNowTime() + "red flag raised in city " + toString(i); 1643 puts(s.c_str()); 1644 } 1645 if (mAllCity[i]->FlagRaise == 2&& mAllCity[i]->lastPrintFlag!=2) 1646 { 1647 mAllCity[i]->FlagRaise = 0; 1648 mAllCity[i]->lastPrintFlag = 2; 1649 string s = printNowTime() + "blue flag raised in city " + toString(i); 1650 puts(s.c_str()); 1651 } 1652 } 1653 break; 1654 } 1655 case 50: 1656 { 1657 string s = printNowTime() + toString(RedBaseInstance->Blood) + " elements in red headquarter"; 1658 puts(s.c_str()); 1659 s = printNowTime() + toString(BlueBaseInstance->Blood) + " elements in blue headquarter"; 1660 puts(s.c_str()); 1661 break; 1662 } 1663 case 55: 1664 { 1665 for (int i = 0; i <= N; i++) 1666 { 1667 if (mAllCity[i]->RedSoldier) 1668 { 1669 mAllCity[i]->RedSoldier->printWeapon(); 1670 } 1671 } 1672 if (RedOccSoldier) RedOccSoldier->printWeapon(); 1673 if (BlueOccSoldier) BlueOccSoldier->printWeapon(); 1674 for (int i = 0; i <= N; i++) 1675 { 1676 if (mAllCity[i]->BlueSoldier) 1677 { 1678 mAllCity[i]->BlueSoldier->printWeapon(); 1679 } 1680 } 1681 break; 1682 } 1683 } 1684 } 1685 } 1686 int main() 1687 { 1688 int t; 1689 cin >> t; 1690 int c = 0; 1691 while (t--) 1692 { 1693 cout << "Case " << ++c << ":" << endl; 1694 Init(); 1695 Game(); 1696 } 1697 system("pause"); 1698 return 0; 1699 } 1700 1701 1702 void Sword::init() 1703 { 1704 //Attack = mAllSoldier[Belong]->getAttack()*0.2; 1705 } 1706 void Lion::init() 1707 { 1708 if (getCamp() == 0) { 1709 Loyalty = RedBaseInstance->Blood; 1710 } 1711 else { 1712 Loyalty = BlueBaseInstance->Blood; 1713 } 1714 } 1715 void getWeapon(Soldier * t,Weapon* &p, int code) 1716 { 1717 switch (code) 1718 { 1719 case 0: 1720 { 1721 p = new Sword(); 1722 ((Sword *)p)->init(); 1723 ((Sword *)p)->setAttack(t->getAttack()); 1724 if (p->getAttack() == 0) 1725 { 1726 delete p; 1727 p = NULL; 1728 } 1729 break; 1730 } 1731 case 1: 1732 { 1733 p = new Bomb(); 1734 ((Bomb *)p)->init(); 1735 t->hasBomb = ((Bomb *)p); 1736 break; 1737 } 1738 case 2: 1739 { 1740 p = new Arrow(); 1741 ((Arrow *)p)->init(); 1742 t->hasArrow = ((Arrow *)p); 1743 break; 1744 } 1745 } 1746 //p->setBelong(getuId());//设置属于谁的 1747 } 1748 void Dragon::init() 1749 { 1750 getWeapon(this,mWeapon, Id % 3); 1751 if(mWeapon) mWeapon->setBelong(getuId());//设置属于谁的 1752 if (Camp == 0)//3.初始化morale 1753 { 1754 Morale = (double)RedBaseInstance->Blood / (double)SoldierBlood[0]; 1755 } 1756 else 1757 { 1758 Morale = (double)BlueBaseInstance->Blood / (double)SoldierBlood[0]; 1759 } 1760 } 1761 Soldier* Base::CreatSoldier() 1762 { 1763 Soldier *tmp=NULL; 1764 switch (creatwho(SoidierNum)) 1765 { 1766 case 0://iceman 1767 tmp = new Iceman(); 1768 break; 1769 case 1://lion 1770 tmp = new Lion(); 1771 break; 1772 case 2://wolf 1773 tmp = new Wolf(); 1774 break; 1775 case 3://ninja 1776 tmp = new Ninja(); 1777 break; 1778 case 4://dragon 1779 tmp = new Dragon(); 1780 break; 1781 } 1782 if (tmp->getBlood() <= Blood)//可以制造出生命值为 m 的武士 1783 { 1784 Blood -= tmp->getBlood(); 1785 SoidierNum++; 1786 tmp->setCamp(getCamp()); 1787 tmp->setId(SoidierNum); 1788 tmp->init(); 1789 mSoldier.push_back(tmp); 1790 mAllSoldier.push_back(tmp); 1791 1792 if (getCamp() == 0) 1793 { 1794 mAllCity[0]->RedSoldier = tmp; 1795 } 1796 else 1797 { 1798 mAllCity[N]->BlueSoldier = tmp; 1799 } 1800 return tmp; 1801 } 1802 else 1803 { 1804 delete tmp;// 1805 return NULL; 1806 } 1807 } 1808 1809 void Dragon::yell(int t)//judge whether to yell 1810 { 1811 if (mAllCity[t]->whoFirst == Camp)//检测是否先攻击 1812 { 1813 if (Morale > 0.8) 1814 { 1815 string s = printTime(); 1816 if (Camp == 0) s += " red "; 1817 if (Camp == 1) s += " blue "; 1818 s += getName() + ' ' + toString(getId())+" yelled in city "+toString(t); 1819 mAllCity[t]->output.push(s); 1820 //puts(s.c_str()); 1821 } 1822 } 1823 }