Maplewing

1/27 C++: PVP

Jan 27th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 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.         // Constructor
  16.         Player(string n, int h, int a){
  17.             name = n;
  18.             hp = h;
  19.             atk = a;
  20.         }
  21.        
  22.         void attack(Player &p){
  23.             p.hp -= atk;
  24.            
  25.             cout << "------------------------------------" << endl;
  26.             cout << name << "攻擊了" << p.name << "!" << endl;
  27.             cout << p.name << "受到了" << atk << "點的損傷!" << endl;
  28.             cout << p.name << "剩下了" << p.hp << "點的血量!" << endl;
  29.             cout << "------------------------------------" << endl;
  30.         }
  31.        
  32.         /*
  33.             C++: bool
  34.             true, false
  35.            
  36.             bool a = true;
  37.             bool b = false;
  38.         */
  39.         bool isAlive(){
  40.             if( hp > 0 ){
  41.                 return true;
  42.             }
  43.             else {
  44.                 return false;
  45.             }
  46.         }
  47.        
  48.        
  49.         // get函式
  50.         string getName(){
  51.             return name;
  52.         }
  53.        
  54.         int getHP(){
  55.             return hp;
  56.         }
  57.    
  58. };
  59.  
  60. // Design Pattern 設計模式
  61.  
  62. int main(){
  63.     srand( time(NULL) );
  64.     Player p1("勇者", rand() % 1000 + 1, rand() % 1000 + 1);
  65.     Player p2("乂煞氣a魔王乂", rand() % 1000 + 1, rand() % 1000 + 1);
  66.    
  67.     while( p1.isAlive() && p2.isAlive() ){
  68.         p1.attack(p2);
  69.         p2.attack(p1);
  70.     }
  71.    
  72.     if( p1.isAlive() ){
  73.         cout << p1.getName() << "贏了!" << endl;
  74.     }
  75.     else if( p2.isAlive() ){
  76.         cout << p2.getName() << "贏了!" << endl;
  77.     }
  78.     else {
  79.         cout << "平手!" << endl;
  80.     }
  81.    
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment