魔兽世界之一:备战 / 魔兽世界之二:装备
魔兽世界之一:备战
单看这题很简单,考虑到可扩展性,把司令部,武士,游戏进程设计成三个类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
#include <map> #include <vector> #include <iostream> using namespace std; int T, M; map<string, int> HP; class Samurai{ int hp; int id; public: Samurai(int _id, string type){ id = _id; hp = HP[type]; } }; class Headquarter{ public: bool stoped; int hp; int summon_id; int Stot; string color; vector<Samurai> Sam; map<string, int> Snum; Headquarter(){} Headquarter(string _color, int _hp):color(_color), hp(_hp){ stoped = 0; summon_id = -1; Stot = 0; Sam.clear(); Snum.clear(); } bool summon(string type); bool summon_pre(); void stop(); }RED, BLUE; class Game{ public: int t; void reset(){ t = 0; } void modify(){ t++; } string show(){ string p = ""; int x = t; while(x) { p = char((x % 10) + '0') + p; x /= 10; } while(p.length() < 3) p = '0' + p; return p; } void new_turn(){ for(int i = 0; i < 6; i++) { if(i == 5)RED.stop(); else if(RED.summon_pre())break; } for(int i = 0; i < 6; i++) { if(i == 5)BLUE.stop(); else if(BLUE.summon_pre())break; } modify(); } }game; void Headquarter::stop() { if(!stoped)cout << game.show() <<' '<< color <<" headquarter stops making warriors\n"; stoped = 1; } void printinfo(string color, string type, int num, int tot) { cout << game.show() <<' '<< color <<' '<< type <<' '<< tot <<" born with strength "<< HP[type] <<','<< num <<' '<< type <<" in " << color <<" headquarter\n"; } bool Headquarter::summon(string type) { if(HP[type] <= hp) { Samurai S = Samurai(summon_id, type); Sam.push_back(S); Snum[type]++; Stot++; printinfo(color, type, Snum[type], Stot); hp -= HP[type]; return true; } else return false; } bool Headquarter::summon_pre() { summon_id++; if(color == "red") { switch(summon_id % 5) { case 0: return summon("iceman"); case 1: return summon("lion"); case 2: return summon("wolf"); case 3: return summon("ninja"); case 4: return summon("dragon"); } } else { switch(summon_id % 5) { case 0: return summon("lion"); case 1: return summon("dragon"); case 2: return summon("ninja"); case 3: return summon("iceman"); case 4: return summon("wolf"); } } } int main() { cin >> T; for(int cas = 1; cas <= T; cas++) { game.reset(); cout << "Case:" << cas << endl; cin >> M; RED = Headquarter("red", M); BLUE = Headquarter("blue", M); cin >> HP["dragon"] >> HP["ninja"] >> HP["iceman"] >> HP["lion"] >> HP["wolf"]; while(game.t < 10000) game.new_turn(); } return 0; } |
魔兽世界之二:装备
增加了一个武器类,但我指针用的不是很顺手,希望在司令部中能有个武士的vector和map,所以没有把各个武士设计成子类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
#include <map> #include <cstdio> #include <vector> #include <iomanip> #include <iostream> using namespace std; int T, M; string WTYPE[3] = {"sword", "bomb", "arrow"}; map<string, int> SHP; class Weapon{ public: int atk; string type; Weapon(string _type, int _atk){ atk = _atk; type = _type; } }; class Samurai{ public: int hp; int id; int loyalty; string type; double morale; vector<Weapon> Wea; Samurai(int _id, string _type, int hhp):type(_type){ id = _id; hp = SHP[type]; morale = loyalty = 0; if(type == "dragon") { Wea.push_back(Weapon(WTYPE[id % 3], 0)); morale = (double)hhp / SHP["dragon"]; } if(type == "ninja") { Wea.push_back(Weapon(WTYPE[id % 3], 0)); Wea.push_back(Weapon(WTYPE[(id + 1) % 3], 0)); } if(type == "iceman") { Wea.push_back(Weapon(WTYPE[id % 3], 0)); } if(type == "lion") { loyalty = hhp; } } }; class Headquarter{ public: bool stoped; int hp; int summon_type; int Stot; string color; vector<Samurai> Sam; map<string, int> Snum; Headquarter(){} Headquarter(string _color, int _hp):color(_color), hp(_hp){ stoped = summon_type = Stot = 0; Sam.clear(); Snum.clear(); } bool summon(string type); bool summon_pre(); void stop(); }RED, BLUE; class Game{ public: int t; void reset(int cas); void modify(); string show(); void nextturn(); void printinfo(Samurai S, string color, int num, int tot); }game; //==========游戏类函数 void Game::reset(int cas) { cout << "Case:" << cas << endl; t = 0; } void Game::modify() { t++; } string Game::show() { string p = ""; int x = t; while(x) { p = char((x % 10) + '0') + p; x /= 10; } while(p.length() < 3)p = '0' + p; return p; } void Game::nextturn(){ for(int i = 0; i < 6; i++) { if(i == 5)RED.stop(); else if(RED.summon_pre())break; } for(int i = 0; i < 6; i++) { if(i == 5)BLUE.stop(); else if(BLUE.summon_pre())break; } modify(); } void Game::printinfo(Samurai S, string color, int num, int tot){ cout << show() <<' '<< color <<' '<< S.type <<' '<< tot <<" born with strength "<< SHP[S.type] <<','<< num <<' '<< S.type <<" in " << color <<" headquarter\n"; if(S.type == "dragon") cout << "It has a " << S.Wea[0].type << ",and it's morale is " << fixed << setprecision(2) << S.morale << endl; if(S.type == "ninja") cout << "It has a " << S.Wea[0].type << " and a " << S.Wea[1].type << endl; if(S.type == "iceman") cout << "It has a " << S.Wea[0].type << endl; if(S.type == "lion") cout << "It's loyalty is " << S.loyalty << endl; } //========== //==========司令部类函数 void Headquarter::stop() { if(!stoped)cout << game.show() <<' '<< color <<" headquarter stops making warriors\n"; stoped = 1; } bool Headquarter::summon(string type) { if(SHP[type] <= hp) { hp -= SHP[type]; Snum[type]++; Stot++; Samurai S = Samurai(Stot, type, hp); Sam.push_back(S); game.printinfo(S, color, Snum[type], Stot); return true; } else return false; } string RSam[] = {"iceman", "lion", "wolf", "ninja", "dragon"}; string BSam[] = {"lion", "dragon", "ninja", "iceman", "wolf"}; bool Headquarter::summon_pre() { summon_type++; if(color == "red")return summon(RSam[(summon_type - 1) % 5]); else return summon(BSam[(summon_type - 1) % 5]); } //========== int main() { cin >> T; for(int cas = 1; cas <= T; cas++) { game.reset(cas); cin >> M; RED = Headquarter("red", M); BLUE = Headquarter("blue", M); cin >> SHP["dragon"] >> SHP["ninja"] >> SHP["iceman"] >> SHP["lion"] >> SHP["wolf"]; while(game.t < 10000) game.nextturn(); } return 0; } |
Subscribe