繼承練習0321


放個程式碼在這,這樣去學校就不用重打ㄌ

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <bits/stdc++.h>
using namespace std;

class Object{
protected:
int HP;
int ATK;
int hitRate;
public:
Object(int hp, int atk, int rate):HP(hp), ATK(atk),hitRate(rate) {}
void state(){
cout << "HP: " << HP << "\n";
cout << "ATK: " << ATK << "\n";
cout << "hitRate: " << hitRate << "\n";
}
void Attack(Object *obj){
if(rand()%100+1 <= hitRate){
cout << "Attack sucess!!\n";
obj->UnderAttack(ATK);
}
else cout << "miss...\n";
}
void UnderAttack(int damage){
HP -= damage;
if(HP < 0) HP = 0;
cout << "目前血量: " << HP << "\n";
}
int GetHP(){
return HP;
}
bool die(){
if(HP <= 0){
return true;
}
return false;
}

};
class Player:public Object{
private:
int Level = 1;
public:
Player():Object(100, 50, 80){}
void LevelUP(){
++Level;
HP += 10;
if(Level % 3 == 0) ++ATK;
}
};
class Monster:public Object{
public:
Monster():Object(200, 10, 50){}
};
class NPC:public Object{
public:
NPC():Object(150, 5, 60){}
};

int main(){
srand(time(NULL));
Player *player = new Player;
Monster *monster = new Monster;
NPC *npc = new NPC;

player->state();
monster->state();
npc->state();

while(true){
cout << "player對monster發動攻擊!!\n";
player->Attack(monster);
cout << "npc對monster發動攻擊!!\n";
npc->Attack(monster);
cout << "monster對player發動攻擊!!\n";
monster->Attack(player);

if(player->die()){
cout << "你已經死了!!\n";
delete player;
delete monster;
delete npc;
break;
}
if(monster->die()){
player->LevelUP();
delete monster;
Monster *monster = new Monster;
}
if(npc->die()){
delete npc;
NPC *npc = new NPC;
}
}
return 0;
}