===== 9月21日5時間目 RPGバトルメニューに防御が追加される ===== #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) { 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); } // mychar構造体を2つ引数にとって、第1引数のほうが、第2引数の方に1回だけ攻撃する関数をつくる! // 第1引数 struct mychar *_to // 第2引数 struct mychar *_from void beatDefence(struct mychar* _to, struct mychar* _from) { } void printBattleMenu() { cout << "どうする?" << endl; cout << "+---------------+" << endl; cout << "| 1: たたかう |" << endl; cout << "| 2: まほう |" << endl; cout << "| 3: 防御 |" << endl; cout << "| 4: ステータス |" << endl; cout << "| 5: 逃げる   |" << endl; cout << "+---------------+" << endl; } int main() { srand((unsigned int)time(nullptr)); 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); cout << mob.name << "が現れた!" << endl; bool turn = true; int command; bool isDefence = false; //turnがtrueの時はheroの攻撃ターン //turnがfalseの時はmobの攻撃ターン while(hero.hp > 0 && mob.hp >0) { if (turn)//自分のターン { do { printCharSummary(&hero); printBattleMenu(); cin >> command; std::system("cls"); } while (command < 1 || command > 5); //1~5の値によって分岐 //今のところ1を選んだ時だけ攻撃 //その他は、それはできないと表示 switch (command) { case 1: beatCharacter(&hero, &mob); turn = !turn; break; case 2: cout << "それはできない" << endl; break; case 3: isDefence = true; turn = !turn; break; case 4: //ステータス表示の処理を完成させる! //全ステータス表示する関数あったよね! printCharacterStatus(&hero); break; case 5: cout << "それはできない" << endl; break; default: cout << "エラー! それはできない" << endl; break; } cin.get(); //エンター待ち } else//敵のターン { if (isDefence) { beatDefence(&mob, &hero); isDefence = false; } else { beatCharacter(&mob, &hero); } cout << "Push Enter to Next Turn"; cin.get(); //エンター待ち std::system("cls"); //画面クリア turn = !turn; } //ターンの切り替え //turn = !turn; //cout << turn << endl; } return 0; } ==== 相手の攻撃を防御してみる ==== // 第1引数 struct mychar *_to 攻撃する側 // 第2引数 struct mychar *_from 攻撃される側 void beatDefence(struct mychar* _to, struct mychar* _from) { float ratio;//防御力にかける倍率の係数 //ここで乱数使ってごにょごにょして cout << _from->name << "は防御の姿勢をとった!" << endl; cout << _to->name << "の攻撃" << endl; int dam = int(_to->atk + _to->str * 0.5) - int(_from->def * 0.25); //↑この式にごにょごにょする! //防御を選んだ時に、通常防御力は0.25までしか効果がないが、 //ランダムで防御力の0.3~0.7まで効果が出るように関数を書き換える! //ダメージがマイナスになったらHP増えちゃうから、dam = 0にする cout << _from->name << "の受けたダメージ:" << dam << endl; _from->hp = _from->hp - dam; printCharSummary(_from); } \\ \\ ==== 防御用関数を実装してみる(サンプル) ==== // mychar構造体を2つ引数にとって、第1引数のほうが、第2引数の方に1回だけ攻撃する関数をつくる! // 第1引数 struct mychar *_to // 第2引数 struct mychar *_from void beatDefence(struct mychar* _to, struct mychar* _from) { float ratio = (rand() % 10) / 100.0 + 0.25; //なんか大体0になっちゃうから0.25~0.35ぐらいに調整 cout << _from->name << "は防御の姿勢をとった" << endl; cout << _to->name << "の攻撃" << endl; int dam = int(_to->atk + _to->str * 0.5) - int(_from->def * ratio); //def*ratioが防御の値として、攻撃値から減算される if(dam < 0)dam = 0; //ダメージがマイナスの時は0に補正 cout << _from->name << "の受けたダメージ:" << dam << endl; _from->hp = _from->hp - dam;//ダメージ分HPを減算 printCharSummary(_from); }