Advertisement
Maplewing

5/28 C++: PVP

May 27th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. class Player{
  7.     private:
  8.         // 資料(變數部分)
  9.         string name;
  10.         int hp;
  11.         int atk;
  12.        
  13.     public:
  14.         // 功能;方法(函式部分)
  15.         Player(string n, int h, int a){
  16.             name = n;
  17.             hp = h;
  18.             atk = a;
  19.         }
  20.        
  21.         void attack(Player &p2){
  22.             p2.hp -= atk;
  23.            
  24.             cout << "----------------------" << endl;
  25.             cout << name << "攻擊了" << p2.name << "!" << endl;
  26.             cout << p2.name << "受到了" << atk << "點的損傷!" << endl;
  27.             cout << p2.name << "剩下了" << p2.hp << "點的血量!" << endl;
  28.             cout << "----------------------" << endl;
  29.         }
  30.        
  31.         int getHP(){
  32.             return hp;
  33.         }
  34.        
  35.         string getName(){
  36.             return name;
  37.         }
  38.        
  39.         /*
  40.             C++: bool
  41.             bool a = true;
  42.             bool b = false;
  43.         */
  44.         bool isAlive(){
  45.             if( hp > 0 ){
  46.                 return true;
  47.             }
  48.             else{
  49.                 return false;
  50.             }
  51.         }
  52.    
  53. };
  54.  
  55.  
  56. int main(){
  57.     srand( time(NULL) );
  58.    
  59.     Player p1("勇者", rand() % 1000 + 1, rand() % 1000 + 1);
  60.     Player p2("乂煞氣a魔王乂", rand() % 1000 + 1, rand() % 1000 + 1);
  61.    
  62.     while( p1.isAlive() && p2.isAlive() ){
  63.         p1.attack(p2);
  64.         p2.attack(p1);
  65.     }
  66.    
  67.     if( p1.isAlive() ){
  68.         cout << p1.getName() << "贏了!" << endl;
  69.     }
  70.     else if( p2.isAlive() ){
  71.         cout << p2.getName() << "贏了!" << endl;
  72.     }
  73.     else {
  74.         cout << "平手!" << endl;
  75.     }
  76.        
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement