-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimal.java
115 lines (101 loc) · 3.98 KB
/
Animal.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import javafx.scene.paint.Color;
import java.util.List;
import java.util.Random;
public abstract class Animal extends Organism {
protected int addToX, addToY;
Animal(MyField[][] fields, int str, int init, Color color, String name, List<Organism> organisms) {
super(fields, str, init, color, name, organisms);
addToX = 0;
addToY = 0;
}
Animal(MyField[][] fields, int str, int init, Color color, String name, List<Organism> organisms, int posX, int posY) {
super(fields, str, init, color, name, organisms, posX, posY);
addToX = 0;
addToY = 0;
}
Animal(MyField[][] fields, List<Organism> organisms, int str, int init, Color color, String name, int posX, int posY, boolean toDelete, int age, int cooldown) {
super(fields, organisms, str, init, color, name, posX, posY, toDelete, age, cooldown);
}
void collision(Organism opponent) {
if (this.getClass().isInstance(opponent)) {
reproduction(opponent);
} else {
if (opponent.reflectedAttack(this) == 0) { // attacker won
opponent.emptyField();
opponent.toDelete = true;
emptyField();
setOnField(opponent.posX, opponent.posY);
announcements += (name + " kills " + opponent.name + "\n");
} else if (opponent.reflectedAttack(this) == 1) {// attacker lost
emptyField();
toDelete = true;
announcements += (name + " is killed by " + opponent.name + "\n");
} else if (opponent.reflectedAttack(this) == 2) { // i.e antelope case
emptyField();
setOnField(opponent.posX, opponent.posY);
announcements += (opponent.name + " escapes");
} else if (opponent.reflectedAttack(this) == 3) { // i.e sonsowsky hogweeed case
opponent.emptyField();
opponent.toDelete = true;
emptyField();
toDelete = true;
announcements += (name + " eats " + opponent.name + " and dies!");
}
}
}
abstract void reproduction(Organism parent);
MyField getNearbyEmptyField(Organism parent) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (isInsideBoard(posX + (j - 1), posY + (i - 1)) &&
isFieldEmpty(posX + (j - 1), posY + (i - 1))) {
return fields[posX + (j - 1)][posY + (i - 1)];
} else if (isInsideBoard(parent.posX + (j - 1), parent.posY + (i - 1)) &&
isFieldEmpty(parent.posX + (j - 1), parent.posY + (i - 1))) {
return fields[parent.posX + (j - 1)][parent.posY + (i - 1)];
}
}
}
return null;
}
@Override
int reflectedAttack(Organism opponent) {
if (strength > opponent.strength)
return 1;
else if (strength == opponent.strength) {
if (age > opponent.age)
return 1;
}
return 0;
}
@Override
void action() {
if (!toDelete && age >= 0) {
setMovePosition();
if (addToX == 0 && addToY == 0) return;
if (isInsideBoard(posX + addToX, posY + addToY)) {
if (isFieldEmpty(posX + addToX, posY + addToY)) {
emptyField();
setOnField(posX + addToX, posY + addToY);
} else {
collision(fields[posX + addToX][posY + addToY].getOrganism());
}
}
}
}
void setMovePosition() {
addToX = 0;
addToY = 0;
if (generator.nextBoolean())
addToX = getRandomMovement();
else
addToY = getRandomMovement();
}
public int getRandomMovement() { //
Random generator = new Random();
int a = 0;
while (a == 0)
a = generator.nextInt(3) - 1;
return a;
}
}