Holey_yan

20150507_KUAS_VisualC++_Exam08_Question

May 7th, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. /***************************************/
  2. /** 20150507 Exam 08 (Question)       **/
  3. /** Maker  : Yan                      **/
  4. /** E-Mail : [email protected] **/
  5. /** Date   : 2015/05/07 am.11:25      **/
  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. public:
  26.     Animal(char *str, int myhp, int myatt, int mydef){
  27.         strcpy (name, str);
  28.         HP = myhp;
  29.         attack = myatt;
  30.         defense = mydef;
  31.     }
  32.     int getattack (){return attack;}
  33.     int getdefense (){return defense;}
  34.     int getHP (){return HP;}
  35.     char *getname (){return name;}
  36.     void injured (int points){HP -= points;}
  37.     bool isdead (){return (HP <= 0 ? true : false);}
  38.     void fightwith (Animal &pk){
  39.         int result = rand () % attack - rand () % pk.getdefense ();
  40.         if (result > 0) {
  41.             pk.injured (result);
  42.             cout << name << "使出必殺絕技攻向 " << pk.getname () << "造成對方 " << result << "點損失, " << endl;
  43.             cout << pk.getname () << "剩下 " << pk.getHP () << endl;
  44.         }
  45.     }
  46. };
  47.  
  48.  
  49. int main(){
  50.     srand(time(NULL));
  51.  
  52.     Animal rab ("小兔兔", 100, 50, 10), tur ("小龜龜", 100, 30, 30);
  53.  
  54.     while (!rab.isdead () && !tur.isdead()){
  55.         rab.fightwith (tur);
  56.         tur.fightwith (rab);
  57.     }
  58.     system("pause"); return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment