C++_dos command _ Cat and Dog War games (first knowledge of QT mini-exercise)

Source: Internet
Author: User

Recently we are going to learn QT, here to review some basic knowledge of C + +. Namely: encapsulation, inheritance, polymorphism. There is also the use of keywords such as this,new,delete,namespace.

IDE:QT Creator 2.8.0

Operating system: Windows XP

Game introduction: Game only with turn system, mutual "damage", the blood volume is 0 or negative for the party defeated, the game is over.

when the blood volume is >=%80, its combat effectiveness coefficient is 1 defensive force coefficient is 1

When the blood volume is >=%60, its combat effectiveness coefficient is 0.75 defensive force coefficient is 0.7

When the blood volume is >=%40, its combat effectiveness coefficient is 0.5 defensive force coefficient is 0.6

When the blood volume <%40, its combat effectiveness coefficient is 3! (as the saying goes: Dog nasty jumps over is this truth!) )

Each of the two sides of the battle has a property: Name, blood volume, defensive power, combat effectiveness.

The Monster class is established below:

Class monster{/   * Basic Properties */   string   m_szname;  Name   int      M_DHP;     blood volume   int      m_dap;     Defensive Force   int      M_DDP;     Attack   int      flag;public:   /* with parameter constructor *   /Monster (string m_szname,int m_dhp,int m_dap,int M_DDP);   /* Implement calculation Damage method *   /virtual int attack (Monster &m) =0;   /* Turn-based attack *   /void Fight (Monster &m);   /* Show Properties *   /Void Show ();   /* Get method *   /String GetName () const   {       return m_szname;   }   Double gethp () const   {       return m_dhp;   }   Double Getap () const   {       return m_dap;   }   Double GETDP () const   {       return M_DDP;   }   /* Set method *   /void sethp (int HP)   {       M_DHP = hp;   }   void Setap (int ap)   {       M_dap = ap;   }   void setdp (int DP)   {       M_DDP = DP;   }   void Setflag (int flag)   {       flag = flag;   }   Double Getflag () const   {       return flag;   }};

The following are the most important aspects of writing attack and fight methods

We have all played games, in turn-based games, some people will explode the damage, there are multiple damage, the following in the attack function to achieve a multiplier of the random damage.

int Cat::attack (Monster &m) {    //Finish function:    //1. Calculated damage    int ihurt = 0;    int min = 2,max = 8;    Srand ((unsigned) time (NULL));    int num = rand ()% (max-min) + min;    Ihurt = num * GETAP ()-M.GETDP () + 5.0;    if (Ihurt < 1)        Ihurt = 1;    2. Reduce the amount of    m.sethp (M.GETHP ()-Ihurt) of the attacked monsters;    3. Describe the process of monster attack    cout << "Monsters:" << getName () << "  catch Monsters with Claws:" << m.getname () << Endl;    cout << "Monsters:" << m.getname () << "  Blood volume Reduction:" << ihurt << "HP" << Endl;    return 0;}

Implementation principle: Use the Srand,rand function to randomly 2-8 of a number, as a multiplier of the hit damage, so that you can achieve the effect of double strike damage.

The cat attack way is to catch each other with claws, the dog's attack way is similar, only it attacks the way is with the mouth, in order to facilitate, the attack way with cout output text introduction.

With the loss of blood, attack and defense will also be reduced, but in the amount of blood below%40, will be furious, like DNF's Berserker, the low blood volume when the attack explosion table, the following to achieve this function.

Fight Method:

int Cat::fight (Monster &m) {cout << m.getflag () << Endl; int num = 0;        Record number of battle rounds while (1) {//Turn-based attack//First attack b,b blood volume decreased, check B for Blood volume Sleep (1000);        num++;        cout << "section" << num << "turn" << Endl;        Attack (m);            if (M.GETHP () < 0) {cout << m.getname () << "death, end of battle" << Endl;            cout << "Two sides Fighting:" << num << "turn" << Endl;        Break            } if (M.GETHP () >= M.getflag () *0.8) {M.setap (M.getap () *);        M.SETDP (M.GETDP ()); } else if (M.GETHP () <= m.getflag () *0.8 && m.gethp () >= M.getflag () *0.6) {M.setap (            M.getap () *0.7);        M.SETDP (M.GETDP () *0.75); } else if (M.GETHP () <= m.getflag () *0.6 && m.gethp () >= M.getflag () *0.4) {M.setap (M            . Getap () *0.6);        M.SETDP (M.GETDP () *0.5);   } else     {M.setap (M.getap ());        M.SETDP (M.GETDP () *0.4);        }//second B attack a,a blood volume decreased, check a blood volume m.attack (*this);            if (GETHP () < 0) {cout << getName () << "death, end of battle" << Endl;            cout << "Two sides Fighting:" << num << "turn" << Endl;        Break            } if (THIS-&GT;GETHP () >= This->getflag () *0.8) {This->setap (M.getap () *);        THIS-&GT;SETDP (M.GETDP ());        } else if (THIS-&GT;GETHP () <= this->getflag () *0.8 && this->gethp () >= This->getflag () *0.6)            {This->setap (This->getap () *0.7);        THIS-&GT;SETDP (THIS-&GT;GETDP () *0.75);        } else if (THIS-&GT;GETHP () <= this->getflag () *0.6 && this->gethp () >= This->getflag () *0.4)            {This->setap (This->getap () *0.6);        THIS-&GT;SETDP (THIS-&GT;GETDP () *0.5);     } else {       This->setap (This->getap ());        THIS-&GT;SETDP (THIS-&GT;GETDP () *0.4);        } cout << "Cat's attack:" << This->getap () << Endl;    cout << "Dog's attack:" << M.getap () << Endl; } return 0;}

For simplicity, the project uses the default parametric constructor to generate two objects directly "Doraemon", "Wang Choi" to fight.

    Cat C ("Tinker Cat", 10000,400,600);    Dog d ("Wang Choi", 8000,500,800);    C.fight (d);

Test:

In this, the small game is basically completed, it needs to use <time.h> <windows.h> and other header files in some of the functions.

Through this small project to review the characteristics of C + +, gain a lot, consolidate their own C + + language Foundation, I believe in the future of QT learning, there will be greater progress.

C++_dos command _ Cat and Dog War games (first knowledge of QT mini-exercise)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.