-
Notifications
You must be signed in to change notification settings - Fork 0
/
Empty.java
47 lines (43 loc) · 1.25 KB
/
Empty.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
/**
*
* @author Stephanie Engelhardt
*
*/
/**
* Empty squares are competed by various forms of life.
*/
public class Empty extends Living {
public Empty (World w, int r, int c){
super.world=w;
super.row=r;
super.column=c;
}
public State who(){
return State.EMPTY;
}
/**
* An empty square will be occupied by a neighboring Badger, Fox, Rabbit, or Grass, or
* remain empty.
* @param wNew world of the next life cycle.
* @return Living life form in the next cycle.
*/
public Living next(World wNew){
int[] neighborhood= new int[NUM_LIFE_FORMS];
this.census(neighborhood);
//a) Rabbit, if more than one neighboring Rabbit;
if(neighborhood[RABBIT]>1)
return new Rabbit(wNew, this.row, this.column, 0);
//b) otherwise, Fox, if more than one neighboring Fox;
else if(neighborhood[FOX]>1)
return new Fox(wNew, this.row, this.column, 0);
//c) otherwise, Badger, if more than one neighboring Badger;
else if(neighborhood[BADGER]>1)
return new Badger(wNew, this.row, this.column, 0);
//d) otherwise, Grass, if at least one neighboring Grass;
else if(neighborhood[GRASS]>0)
return new Grass(wNew, this.row, this.column);
//e) otherwise, Empty.
else
return new Empty(wNew, this.row, this.column);
}
}