Holey_yan

20150514_KUAS_VisualC++_Exam09_Question

May 15th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. /***************************************/
  2. /** 20150514 Exam 09 (Question)       **/
  3. /** Maker  : Yan                      **/
  4. /** E-Mail : [email protected] **/
  5. /** Date   : 2015/05/14 am.11:00      **/
  6. /***************************************/
  7.  
  8. #include <iostream>
  9. #include <cstdlib>
  10. #include <ctime>
  11. #include <windows.h>
  12. using namespace std;
  13.  
  14. #define MAXCAP 100
  15. #define MAXRABBIT 10
  16. #define MAXLENGTH 1000
  17. #define PERLEN 20
  18.  
  19. class Animal {
  20. private:
  21.     char name [10];
  22.     int HP;
  23.     int attack;
  24.     int defense;
  25.     static int counter;
  26. public:
  27.     Animal(char *str, int myhp, int myatt, int mydef){
  28.         strcpy (name, str);
  29.         HP = myhp;
  30.         attack = myatt;
  31.         defense = mydef;
  32.         counter++;
  33.     }
  34.     static int getcounter (){return counter;}
  35.     int getattack (){return attack;}
  36.     int getdefense (){return defense;}
  37.     int getHP (){return HP;}
  38.     char *getname (){return name;}
  39.     void injured (int points){HP -= points;}
  40.     bool isdead (){return (HP <= 0 ? true : false);}
  41.     void fightwith (Animal &pk){
  42.         int result = rand () % attack - rand () % pk.getdefense ();
  43.         if (result > 0){
  44.             pk.injured (result);
  45.             cout << name << "使出必殺絕技攻向 " << pk.getname () << "造成" << pk.getname () << result << "點損失, " << endl;
  46.             cout << pk.getname () << "剩下 " << pk.getHP () << endl;
  47.         } else
  48.             cout << pk.getname () << "使出無敵防衛,因此沒有造成任何傷害" << endl;
  49.     }
  50.     ~Animal (){counter--;}
  51. };
  52.  
  53. int Animal::counter = 0;
  54.  
  55. int main(){
  56.     srand(time(NULL));
  57.  
  58.     Animal *monster [100];
  59.     char rname [11];
  60.     for (int i = 0; i < 100; i++){
  61.         sprintf (rname, "小兔兔%d號", i);
  62.         monster [i] = new Animal (rname, 100, 50, 10);
  63.     }
  64.  
  65.     cout << "現有" << Animal::getcounter () << "小動物" << endl;
  66.  
  67.     Animal tur ("小龜龜", 1000, 80, 50);
  68.  
  69.     int num = 0;
  70.     Animal *enemy;
  71.     while (!tur.isdead() && Animal::getcounter () > 1){
  72.         enemy = monster [num];
  73.         enemy->fightwith (tur);
  74.         tur.fightwith (*enemy);
  75.         Sleep (1000);
  76.     }
  77.     system("pause");
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment