Guest User

Untitled

a guest
Mar 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. class Mao{
  8. int energy; //エネルギー
  9. int pos; //位置情報
  10. public:
  11. Mao():energy(100),pos(rand()%10){}//コンストラクタ
  12. void Attacked(int n);//ダメージ受ける計算の関数
  13. int GetEnergy()const{ return energy; }//残りエネルギー関数を返す
  14. };
  15.  
  16. void Mao::Attacked(int n){
  17. if(n==pos){
  18. cout << "ぎゃー。命中だ!" << endl;
  19. energy -= 50;
  20. pos += rand()%3-1;
  21. //cout << "魔王の残りのエネルギー " << energy;
  22. }else if(n==pos-1 || n==pos+1){
  23. cout << "おっと危ない! だがはずれだ。" << endl;
  24. energy -= 10;
  25. //cout << "魔王の残りのエネルギー " << energy;
  26. }else{
  27. cout << "どこをねらっている? まったくはずれだ。" << endl;
  28. //cout << "魔王の残りのエネルギー " << energy;
  29. }
  30. if (energy <= 0){
  31. cout << "ああ、やられた! 君は英雄だよ。" << endl;
  32. }
  33. }
  34.  
  35. class Hero{
  36. int energy;
  37. public:
  38. Hero():energy(50){}
  39. int Attack();
  40. int GetEnergy() const{ return energy; }
  41. };
  42.  
  43. int Hero::Attack(){
  44. energy -= 10;
  45. int a;
  46. cout << endl;
  47. cout << "正義の力を受けてみよ!" << endl;
  48. cout << "(0〜9の半角数字で、攻撃位置を指示してください。)" << endl;
  49. cin >> a;
  50. return a;
  51. }
  52.  
  53.  
  54.  
  55. int main(){
  56. srand((unsigned)time(NULL));
  57. Mao m;
  58. Hero h;
  59. cout << "これから魔王と戦います。がんばれ!" << endl;
  60. cout << "魔王の残りのエネルギー" << m.GetEnergy();
  61.  
  62. while(h.GetEnergy()>0){
  63. if(m.GetEnergy()<=0){
  64. break;
  65. }
  66. int x;
  67. x = h.Attack();
  68. m.Attacked(x);
  69. }
  70. if(m.GetEnergy()>0){
  71. cout << "・・・魔王は逃げてしまった。" << endl;
  72. }
  73.  
  74. return 0;
  75. }
Add Comment
Please, Sign In to add comment