-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBadger.java
54 lines (50 loc) · 1.47 KB
/
Badger.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
/**
*
* @author Stephanie Engelhardt
*
*/
/**
* A badger eats a rabbit and competes against a fox.
*/
public class Badger extends Animal{
/**
* Constructor
* @param w: world
* @param r: row position
* @param c: column position
* @param a: age
*/
public Badger (World w, int r, int c, int a){
super.age=a;
super.world=w;
super.row=r;
super.column=c;
}
/**
* A badger occupies the square.
*/
public State who(){
return State.BADGER;
}
/**
* A badger dies of old age or hunger, from isolation and attack by a group of foxes.
* @param wNew world of the next cycle
* @return Living life form occupying the square in the next cycle.
*/
public Living next(World wNew){
int[] neighborhood= new int[NUM_LIFE_FORMS];
this.census(neighborhood);
//a) Empty if the Badger is currently at age 4;
if(this.myAge()>=BADGER_MAX_AGE)
return new Empty(wNew, this.row, this.column);
//b) otherwise, Fox, if there is only one Badger but there are more than one Fox in the neighborhood
else if(neighborhood[FOX]>1 && neighborhood[BADGER]==1)
return new Fox(wNew, this.row, this.column, 0);
//c) otherwise, Empty, if Badgers and Foxes together outnumber Rabbits in the neighborhood;
else if((neighborhood[BADGER]+neighborhood[FOX])>neighborhood[RABBIT])
return new Empty(wNew, this.row, this.column);
//d) otherwise, Badger (the badger will live on).
else
return (new Badger(wNew, this.row, this.column, this.age+1));
}
}