Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- class Player{
- private:
- // 資料(變數部分)
- string name;
- int hp;
- int atk;
- public:
- // 功能;方法(函式部分)
- Player(string n, int h, int a){
- name = n;
- hp = h;
- atk = a;
- }
- void attack(Player &p2){
- p2.hp -= atk;
- cout << "----------------------" << endl;
- cout << name << "攻擊了" << p2.name << "!" << endl;
- cout << p2.name << "受到了" << atk << "點的損傷!" << endl;
- cout << p2.name << "剩下了" << p2.hp << "點的血量!" << endl;
- cout << "----------------------" << endl;
- }
- int getHP(){
- return hp;
- }
- string getName(){
- return name;
- }
- /*
- C++: bool
- bool a = true;
- bool b = false;
- */
- bool isAlive(){
- if( hp > 0 ){
- return true;
- }
- else{
- return false;
- }
- }
- };
- int main(){
- srand( time(NULL) );
- Player p1("勇者", rand() % 1000 + 1, rand() % 1000 + 1);
- Player p2("乂煞氣a魔王乂", rand() % 1000 + 1, rand() % 1000 + 1);
- while( p1.isAlive() && p2.isAlive() ){
- p1.attack(p2);
- p2.attack(p1);
- }
- if( p1.isAlive() ){
- cout << p1.getName() << "贏了!" << endl;
- }
- else if( p2.isAlive() ){
- cout << p2.getName() << "贏了!" << endl;
- }
- else {
- cout << "平手!" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement