-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlien.java
117 lines (68 loc) · 1.55 KB
/
Alien.java
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
public class Alien {
private int health, damage;
public Alien(int h, int d) {
health = h;
damage = d;
}
public Alien(Alien a) {
health = a.health;
damage = a.damage;
}
public boolean isAlive() {
return health > 0;
}
public void takeDamage(int d) {
health -= d;
}
public int getDamage() {return damage;}
public int getHealth() {return health;}
public String toString() {
return "Alien. Health: " + health + ", Damage: " + damage + ".";
}
}
class AlienPack {
private Alien [] aliens;
private int next;
public Alien al;
public AlienPack(int num) {
aliens = new Alien[num];
}
public AlienPack(AlienPack a) {
aliens = new Alien[a.aliens.length];
next = a.next;
for(int i = 0; i < next; i++) {
aliens[i] = new Alien(a.aliens[i]);
}
}
public void addAlien(Alien a) {
if(next < aliens.length) {
aliens[next] = a;
next++;
}
}
public int totalDamage() {
int total = 0;
for(int i = 0; i < next; i++) {
total += aliens[i].getDamage();
}
return total;
}
}
class AlienTester {
public static void main(String [] args) {
Alien a1 = new Alien(100, 3);
Alien a2 = new Alien(300, 5);
Alien a3 = new Alien(200, 2);
Alien a4 = new Alien(a1);
AlienPack ap1 = new AlienPack(10);
ap1.addAlien(a1);
ap1.addAlien(a4);
ap1.addAlien(a2);
ap1.addAlien(a3);
ap1.addAlien(new Alien(a1));
AlienPack ap2 = new AlienPack(ap1);
ap2.addAlien(new Alien(200, 6));
System.out.println(ap1.totalDamage());
System.out.println(ap2.totalDamage());
}
}