-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPredator.java
157 lines (153 loc) · 2.79 KB
/
Predator.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package Projects;
public class Predator {
public int xAxis;
public int yAxis;
public int xSize;
public int ySize;
public int nuts;
public boolean life;
public Life lifeClass;
CircleComparer c = new CircleComparer();
public Predator(Life lifeClass) {
this.lifeClass = lifeClass;
xAxis = (int)(Math.random() * 485);
yAxis = (int)(Math.random() * 485);
xSize = (int)((Math.random() * 2) + 5);
ySize = xSize;
nuts = 1000;
life = true;
}
public Predator(int x, int y, int a, int b, Life life1) {
xAxis = x;
yAxis = y;
xSize = a;
ySize = b;
nuts = 1000;
lifeClass = life1;
life = true;
}
public void behavior() {
if(life == true) {
if(nuts > 25) {
if(reproduce()) {
}
else{
grow();
movement();
}
}
else {
die();
}
}
}
private void die() {
life = false;
}
private void movement() {
double xTest = Math.random();
double yTest = Math.random();
if (xAxis <= 0) {
xAxis+=1;
}
else if (xAxis >= 500) {
xAxis-=1;
}
else {
if (xTest > 0.5) {
xAxis +=1;
}
else {
xAxis-=1;
}
}
if (yAxis <= 0) {
yAxis+=1;
}
else if (yAxis >= 500) {
yAxis-=1;
}
else {
if (yTest > 0.5) {
yAxis +=1;
}
else {
yAxis-=1;
}
}
for(int j = 0; j < lifeClass.herbStore.size(); j++) {
if(c.compare(xAxis, yAxis, xSize, lifeClass.herbStore.get(j).getxAxis(), lifeClass.herbStore.get(j).getyAxis(), lifeClass.herbStore.get(j).getxSize())) {
if(lifeClass.herbStore.get(j).isLife());
eat(lifeClass.herbStore.get(j), j);
}
}
nuts -= 1;
}
private void eat(Herbivore h, int j) {
nuts += (h.getNuts());
lifeClass.herbStore.remove(j);
}
private void grow() {
if (nuts > 1000 && xSize < 10) {
xSize += 1;
ySize += 1;
nuts -= 200;
}
}
private boolean reproduce() {
if (nuts > 2000) {
int a = xSize/2;
int b = ySize/2;
Predator p = new Predator(xAxis, yAxis, a, b, lifeClass);
p.populate(p);
xSize = xSize/2;
ySize = ySize/2;
nuts = 1000;
return true;
}
return false;
}
public void populate(Predator p) {
lifeClass.predStore.add(p);
// print();
}
public void print(){
System.out.println("x: " + xAxis + " " + "y: " + yAxis);
}
public int getxAxis() {
return xAxis;
}
public void setxAxis(int xAxis) {
this.xAxis = xAxis;
}
public int getyAxis() {
return yAxis;
}
public void setyAxis(int yAxis) {
this.yAxis = yAxis;
}
public void setxSize(int xSize) {
this.xSize = xSize;
}
public int getySize() {
return ySize;
}
public void setySize(int ySize) {
this.ySize = ySize;
}
public int getNuts() {
return nuts;
}
public void setNuts(int nuts) {
this.nuts = nuts;
}
public boolean isLife() {
return life;
}
public void setLife(boolean life) {
this.life = life;
}
public int getxSize() {
return xSize;
}
}