Skip to content

Instantly share code, notes, and snippets.

@ohmiyaji
Created March 13, 2018 06:01
Show Gist options
  • Save ohmiyaji/dd5f8ac2d186c972b63245f5454aeac5 to your computer and use it in GitHub Desktop.
Save ohmiyaji/dd5f8ac2d186c972b63245f5454aeac5 to your computer and use it in GitHub Desktop.
class.mao
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Mao{
int energy; //エネルギー
int pos; //位置情報
public:
Mao():energy(100),pos(rand()%10){}//コンストラクタ
void Attacked(int n);//ダメージ受ける計算の関数
int GetEnergy()const{ return energy; }//残りエネルギー関数を返す
};
void Mao::Attacked(int n){
if(n==pos){
cout << "ぎゃー。命中だ!" << endl;
energy -= 50;
pos += rand()%3-1;
}else if(n==pos-1 || n==pos+1){
cout << "おっと危ない! だがはずれだ。" << endl;
energy -= 10;
}else{
cout << "どこをねらっている? まったくはずれだ。" << endl;
}
if (energy <= 0){
cout << "ああ、やられた! 君は英雄だよ。" << endl;
}
}
int main(){
srand((unsigned)time(NULL));
Mao m;
int i,a;
for (i=0;i<5;i++){
if(m.GetEnergy()<=0){
break;
}
cout << "魔王に攻撃を加えます。" << endl;
cout << "攻撃位置を入力してください。(0〜9の半角数字)" << endl;
cout << "魔王の残りエネルギー:" << m.GetEnergy() << endl;
cout << "攻撃はあと" << 5-i << "回" << endl;
cin >> a;
m.Attacked(a);
}
if(m.GetEnergy()>0){
cout << "・・・魔王は逃げてしまった。" << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment