==== バトルもどきをしてみる ==== #include #include using std::string; using std::cin; using std::cout; using std::endl; //ドラ〇エのキャラクターを考える //name 名前:キャラクターの名前 //job 職業番号 0:戦士 1:魔法使い 2:僧侶 3:盗賊 // HP  生命力:これが尽きると死す // MP  魔法力:これが尽きると魔法が使えない //STR  強さ:力の強さ→攻撃力に影響 //ATK  攻撃力:攻撃能力の高さ //DEF  防御力:防御能力の高さ //MGK 魔法能力:魔法を操る能力の高さ //ING かしこさ:インテリジェンスの高さ //LUK  運:運の強さ //僕のヒーローのパラメータ struct mychar { string name;//名前 int job;//職業番号 int hp; int mp; int str; float atk; float def; float mgk; int ing; int luk; }; string jobs[] = { "戦士","魔法使い","僧侶", "盗賊" }; //jobs[0] 戦士, jobs[1] 魔法使い。。。 void setCharacterStatus(mychar* _mc, string _name, int _job, int _hp, int _mp, int _str, float _atk, float _def, float _mgk, int _ing, int _luk) { //-> アロー演算子:アドレスで渡された構造体のメンバ変数を参照する _mc->name = _name; _mc->job = _job; _mc->hp = _hp; _mc->mp = _mp; _mc->str = _str; _mc->atk = _atk; _mc->def = _def; _mc->mgk = _mgk; _mc->ing = _ing; _mc->luk = _luk; } //素敵なフォーマットでキャラのパラメータを表示する関数 //引数で、キャラクターを表す構造体を渡す(ポインタでアドレス渡し) void printCharacterStatus(struct mychar* _mychar) { cout << "==================" << endl; cout << " 名前:" << _mychar->name << endl; cout << " 職業:" << jobs[_mychar->job] << endl; cout << " HP:" << _mychar->hp << endl; cout << " MP:" << _mychar->mp << endl; cout << " STR:" << _mychar->str << endl; cout << " ATK:" << _mychar->atk << endl; cout << " DEF:" << _mychar->def << endl; cout << " MGK:" << _mychar->mgk << endl; cout << " ING:" << _mychar->ing << endl; cout << " LUK:" << _mychar->luk << endl; cout << "==================" << endl; } //素敵なフォーマットでキャラのパラメータを表示する関数 //引数で、キャラクターを表す構造体を渡す(ポインタでアドレス渡し) void printCharSummary(struct mychar* _mychar) { cout << "------------------" << endl; cout << " 名前:" << _mychar->name << endl; cout << " HP:" << _mychar->hp << endl; cout << " MP:" << _mychar->mp << endl; cout << "------------------" << endl; } // mychar構造体を2つ引数にとって、第1引数のほうが、第2引数の方に1回だけ攻撃する関数をつくる! // 第1引数 struct mychar *_to // 第2引数 struct mychar *_from void beatCharacter(struct mychar* _to, struct mychar* _from) { } int main() { mychar hero; //作っただけでは初期化されないのでメンバー変数は何も入ってない setCharacterStatus(&hero, "オルテガ", 0, 100, 10, 50, 100.0, 80.0, 10.0, 5, 20); // 構造体, 名前, job, hp, mp,str, atk, def, mgk,ing, luk //printCharacterStatus(&hero); printCharSummary(&hero); mychar mob; setCharacterStatus(&mob, "モブ夫", 3, 500, 80, 10, 20.0, 15.0, 5.0, 10, 10); // 構造体, 名前, job, hp, mp,str, atk, def, mgk, ing, luk //printCharacterStatus(&mob); //printCharSummary(&mob); //攻撃のルーチンを考える! // 攻撃の時に与えられるダメージ int(atk + str * 0.5) - 相手のdef*0.25 //モブ夫→オルテガ // // hero.hp = hero.hp -  int(モブ夫.atk + モブ夫.str * 0.5) - int(オルテガ.def*0.25) (マイナスになったらダメージ0) // mychar構造体を2つ引数にとって、第1引数のほうが、第2引数の方に1回だけ攻撃する関数をつくる! cout << mob.name << "の攻撃" << endl; int dam = int(mob.atk + mob.str * 0.5) - int(hero.def * 0.25); cout << hero.name << "の受けたダメージ:" << dam << endl; hero.hp = hero.hp - dam; printCharSummary(&hero); cout << endl; cout << hero.name << "の攻撃" << endl; dam = int(hero.atk + hero.str * 0.5) - int(mob.def * 0.25); cout << mob.name << "の受けたダメージ:" << dam << endl; mob.hp = mob.hp - dam; //値がマイナスの時は0になる。それ以外はhpの値になる mob.hp = std::max(mob.hp, 0); printCharSummary(&mob); return 0; } ==== キャラクターを2匹引数にとって、バトルを1回する関数 ==== // mychar構造体を2つ引数にとって、第1引数のほうが、第2引数の方に1回だけ攻撃する関数をつくる! // 第1引数 struct mychar *_to // 第2引数 struct mychar *_from void beatCharacter(struct mychar* _to, struct mychar* _from) { cout << _to->name << "の攻撃" << endl; int dam = int(_to->atk + _to->str * 0.5) - int(_from->def * 0.25); cout << _from->name << "の受けたダメージ:" << dam << endl; _from->hp = _from->hp - dam; printCharSummary(_from); }