Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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;
- //cout << "魔王の残りのエネルギー " << energy;
- }else if(n==pos-1 || n==pos+1){
- cout << "おっと危ない! だがはずれだ。" << endl;
- energy -= 10;
- //cout << "魔王の残りのエネルギー " << energy;
- }else{
- cout << "どこをねらっている? まったくはずれだ。" << endl;
- //cout << "魔王の残りのエネルギー " << energy;
- }
- if (energy <= 0){
- cout << "ああ、やられた! 君は英雄だよ。" << endl;
- }
- }
- class Hero{
- int energy;
- public:
- Hero():energy(50){}
- int Attack();
- int GetEnergy() const{ return energy; }
- };
- int Hero::Attack(){
- energy -= 10;
- int a;
- cout << endl;
- cout << "正義の力を受けてみよ!" << endl;
- cout << "(0〜9の半角数字で、攻撃位置を指示してください。)" << endl;
- cin >> a;
- return a;
- }
- int main(){
- srand((unsigned)time(NULL));
- Mao m;
- Hero h;
- cout << "これから魔王と戦います。がんばれ!" << endl;
- cout << "魔王の残りのエネルギー" << m.GetEnergy();
- while(h.GetEnergy()>0){
- if(m.GetEnergy()<=0){
- break;
- }
- int x;
- x = h.Attack();
- m.Attacked(x);
- }
- if(m.GetEnergy()>0){
- cout << "・・・魔王は逃げてしまった。" << endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment