Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************************/
- /** 20150514 Exam 09 (Question) **/
- /** Maker : Yan **/
- /** E-Mail : [email protected] **/
- /** Date : 2015/05/14 am.11:00 **/
- /***************************************/
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <windows.h>
- using namespace std;
- #define MAXCAP 100
- #define MAXRABBIT 10
- #define MAXLENGTH 1000
- #define PERLEN 20
- class Animal {
- private:
- char name [10];
- int HP;
- int attack;
- int defense;
- static int counter;
- public:
- Animal(char *str, int myhp, int myatt, int mydef){
- strcpy (name, str);
- HP = myhp;
- attack = myatt;
- defense = mydef;
- counter++;
- }
- static int getcounter (){return counter;}
- int getattack (){return attack;}
- int getdefense (){return defense;}
- int getHP (){return HP;}
- char *getname (){return name;}
- void injured (int points){HP -= points;}
- bool isdead (){return (HP <= 0 ? true : false);}
- void fightwith (Animal &pk){
- int result = rand () % attack - rand () % pk.getdefense ();
- if (result > 0){
- pk.injured (result);
- cout << name << "使出必殺絕技攻向 " << pk.getname () << "造成" << pk.getname () << result << "點損失, " << endl;
- cout << pk.getname () << "剩下 " << pk.getHP () << endl;
- } else
- cout << pk.getname () << "使出無敵防衛,因此沒有造成任何傷害" << endl;
- }
- ~Animal (){counter--;}
- };
- int Animal::counter = 0;
- int main(){
- srand(time(NULL));
- Animal *monster [100];
- char rname [11];
- for (int i = 0; i < 100; i++){
- sprintf (rname, "小兔兔%d號", i);
- monster [i] = new Animal (rname, 100, 50, 10);
- }
- cout << "現有" << Animal::getcounter () << "小動物" << endl;
- Animal tur ("小龜龜", 1000, 80, 50);
- int num = 0;
- Animal *enemy;
- while (!tur.isdead() && Animal::getcounter () > 1){
- enemy = monster [num];
- enemy->fightwith (tur);
- tur.fightwith (*enemy);
- Sleep (1000);
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment