Maplewing

10/17 C++: PVP

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