-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorld.java
More file actions
267 lines (232 loc) · 6.95 KB
/
World.java
File metadata and controls
267 lines (232 loc) · 6.95 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import java.util.Random;
public class World {
private static World w;
private Random rand;
private Entity[][] grid;
public int WIDTH;
public int HEIGHT;
public int NUMCARN;
public int NUMHERB;
public int NUMPLAN;
public int PLANTSADDED;
/**
* (Private) Constructor for World Class
* @param grid_width width of grid that holds all Entities
* @param grid_height height of grid that holds all Entities
*/
private World(int grid_width, int grid_height){
WIDTH = grid_width;
HEIGHT = grid_height;
int area = WIDTH*HEIGHT;
NUMCARN = Math.max(area/40, area/120 + 2); //making sure there are never 0
NUMHERB = Math.max(area/45, area/90 + 2);
NUMPLAN = Math.max(area/30, area/60 + 1);
PLANTSADDED = Math.max(area/75, area/100 + 2);
grid = new Entity[WIDTH][HEIGHT];
rand = new Random();
int x, y;
//add NUMCARN carnivores to grid
for (int iCarn = 0; iCarn < NUMCARN; iCarn++) {
do {
x = rand.nextInt(WIDTH);
y = rand.nextInt(HEIGHT);
}
while (!isEmpty(x,y));
grid[x][y] = new Carnivore(x,y);
}
//add NUMHERB herbivores
for (int iHerb = 0; iHerb < NUMHERB; iHerb++) {
do {
x = rand.nextInt(WIDTH);
y = rand.nextInt(HEIGHT);
}
while (!isEmpty(x,y));
grid[x][y] = new Herbivore(x,y);
}
//add NUMPLAN plants
for (int iPlant = 0; iPlant < NUMPLAN; iPlant++) {
do {
x = rand.nextInt(WIDTH);
y = rand.nextInt(HEIGHT);
}
while (!isEmpty(x,y));
grid[x][y] = new Plant(x,y);
}
//Herder in the middle of the grid
x = WIDTH/2;
y = HEIGHT/2;
grid[x][y] = Herder.getInstance(x,y,1);
}
//-------------------------------------------
//GET/SET
/**
* Retrieve single World instance
* @return singleton World instance
*/
public static World getInstance() {
if(w == null)
w = new World(30, 30);
return w;
}
/**
* Retrieve single World instance
* @param width width of grid that holds all Entities if World is not yet instantiated
* @param height height of grid that holds all Entities if World is not yet instantiated
* @return singleton World instance
*/
public static World getInstance(int width, int height) {
if(w == null)
w = new World(width, height);
return w;
}
/**
* Check if a location in the grid is empty i.e. it does not have an Entity on int
* @param x X coordinate in grid (width)
* @param y Y coordinate in grid (height)
* @return boolean result of query
*/
public boolean isEmpty(int x, int y) {
return (grid[x][y] == null);
}
/**
* Returns the entity at a specific location on the grid
* @param x X coordinate in grid (width)
* @param y Y coordinate in grid (height)
* @return Entity at (x,y) or null if no Entity at that location
*/
public Entity getEntity(int x, int y) {
if(isEmpty(x,y) == true)
return null;
return grid[x][y];
}
/**
* Returns the grid that holds all Entities
* @return grid of the World which is a 2-dimensional Entity array
*/
public Entity[][] getGrid(){
return grid;
}
//-------------------------------------------
//MOVING ANIMALS
/**
* Tells every Carnivore on the grid to move
*/
public void moveCarnivores() {
Carnivore a;
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++){
if (!isEmpty(x,y) && grid[x][y] instanceof Carnivore && !((Animal)grid[x][y]).isMoving()) {
a = (Carnivore)grid[x][y];
//make sure each animal can only move once
a.startMoving();
a.move(grid);
}
}
}
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++){
if (!isEmpty(x,y) && grid[x][y] instanceof Carnivore) {
//make sure each animal can only move once
((Carnivore)grid[x][y]).stopMoving();
}
}
}
}
/**
* Tells every Herbivore on the grid to move
*/
public void moveHerbivores() {
Herbivore a;
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++){
if (!isEmpty(x,y) && grid[x][y] instanceof Herbivore && !((Animal)grid[x][y]).isMoving()) {
a = (Herbivore)grid[x][y];
//make sure each animal can only move once
a.startMoving();
a.move(grid);
}
}
}
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++){
if (!isEmpty(x,y) && grid[x][y] instanceof Herbivore) {
//make sure each animal can only move once
((Herbivore)grid[x][y]).stopMoving();
}
}
}
}
//-------------------------------------------
//AGING/DYING
/**
* Tells every Animal on the grid to grow older and possibly die
*/
public void age() {
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
if (!isEmpty(x,y)) {
Entity e = grid[x][y];
if (e instanceof Herder)
continue;
e.growOlder();
if (e.getAge() + 3*rand.nextGaussian() > e.getMaxAge()) {
//died from old age
grid[x][y] = null;
}
if (e instanceof Herbivore) {
((Herbivore)e).age(grid);
}
if (e instanceof Carnivore) {
((Animal)e).setEnergy(((Animal)e).getEnergy()-1);
if (((Animal)e).getEnergy() < 1) {
//starved
grid[x][y] = null;
}
}
}
}
}
}
//-------------------------------------------
//ADD PLANTS
/**
* Adds PLANTSADDED instances of Plant to the grid
*/
public void addPlants() {
//add PLANTSADDED plants
int x, y;
for (int iPlant = 0; iPlant < PLANTSADDED; iPlant++) {
do {
x = rand.nextInt(WIDTH);
y = rand.nextInt(HEIGHT);
}
while (!isEmpty(x,y));
grid[x][y] = new Plant(x,y);
}
}
//-------------------------------------------
//TOSTRING
/**
* Print the grid representing the World
* @return String representing the current state of the World
*/
@Override
public String toString() {
String s = "";
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
if (isEmpty(x,y)) {
s += ".";
}
else {
s += grid[x][y].toString();
}
if (y != HEIGHT-1) {
s += " ";
}
}
s += "\n";
}
return s;
}
}