-
Notifications
You must be signed in to change notification settings - Fork 0
/
Living.java
171 lines (160 loc) · 6.39 KB
/
Living.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
*
* @author Stephanie Engelhardt
*
*/
/**
*
* Living refers to the life form occupying a square in a world grid. It is a
* superclass of Empty, Grass, and Animal, the latter of which is in turn a superclass
* of Badger, Fox, and Rabbit. Living has two abstract methods awaiting implementation.
*
*/
public abstract class Living {
protected World world; // the world in which the life form resides
protected int row; // location of the square on which
protected int column; // the life form resides
// constants to be used as indices.
protected static final int BADGER = 0;
protected static final int EMPTY = 1;
protected static final int FOX = 2;
protected static final int GRASS = 3;
protected static final int RABBIT = 4;
public static final int NUM_LIFE_FORMS = 5;
// life expectancies
public static final int BADGER_MAX_AGE = 4;
public static final int FOX_MAX_AGE = 6;
public static final int RABBIT_MAX_AGE = 3;
/**
* Censuses all life forms in the 3 X 3 neighborhood in a world.
* @param population counts of all life forms
*/
protected void census(int population[]){
//count what the living is
if(this.world.grid[this.row][this.column].who()==State.RABBIT)
population[RABBIT]++;
else if(this.world.grid[this.row][this.column].who()==State.GRASS)
population[GRASS]++;
else if(this.world.grid[this.row][this.column].who()==State.FOX)
population[FOX]++;
else if(this.world.grid[this.row][this.column].who()==State.EMPTY)
population[EMPTY]++;
else if(this.world.grid[this.row][this.column].who()==State.BADGER)
population[BADGER]++;
//if possible, check to the left
if(this.column-1>=0){
if(this.world.grid[this.row][this.column-1].who()==State.RABBIT)
population[RABBIT]++;
else if(this.world.grid[this.row][this.column-1].who()==State.GRASS)
population[GRASS]++;
else if(this.world.grid[this.row][this.column-1].who()==State.FOX)
population[FOX]++;
else if(this.world.grid[this.row][this.column-1].who()==State.EMPTY)
population[EMPTY]++;
else if(this.world.grid[this.row][this.column-1].who()==State.BADGER)
population[BADGER]++;
}
//if possible, check to the right
if(this.column+1<this.world.grid.length){
if(this.world.grid[this.row][this.column+1].who()==State.RABBIT)
population[RABBIT]++;
else if(this.world.grid[this.row][this.column+1].who()==State.GRASS)
population[GRASS]++;
else if(this.world.grid[this.row][this.column+1].who()==State.FOX)
population[FOX]++;
else if(this.world.grid[this.row][this.column+1].who()==State.EMPTY)
population[EMPTY]++;
else if(this.world.grid[this.row][this.column+1].who()==State.BADGER)
population[BADGER]++;
}
//if possible, count living above
if(this.row-1>=0){
if(this.world.grid[this.row-1][this.column].who()==State.RABBIT)
population[RABBIT]++;
else if(this.world.grid[this.row-1][this.column].who()==State.GRASS)
population[GRASS]++;
else if(this.world.grid[this.row-1][this.column].who()==State.FOX)
population[FOX]++;
else if(this.world.grid[this.row-1][this.column].who()==State.EMPTY)
population[EMPTY]++;
else if(this.world.grid[this.row-1][this.column].who()==State.BADGER)
population[BADGER]++;
//if possible, count living above and to the left
if(this.column-1>=0){
if(this.world.grid[this.row-1][this.column-1].who()==State.RABBIT)
population[RABBIT]++;
else if(this.world.grid[this.row-1][this.column-1].who()==State.GRASS)
population[GRASS]++;
else if(this.world.grid[this.row-1][this.column-1].who()==State.FOX)
population[FOX]++;
else if(this.world.grid[this.row-1][this.column-1].who()==State.EMPTY)
population[EMPTY]++;
else if(this.world.grid[this.row-1][this.column-1].who()==State.BADGER)
population[BADGER]++;
}
//if possible, count living above and to the right
if(this.column+1<this.world.grid.length){
if(this.world.grid[this.row-1][this.column+1].who()==State.RABBIT)
population[RABBIT]++;
else if(this.world.grid[this.row-1][this.column+1].who()==State.GRASS)
population[GRASS]++;
else if(this.world.grid[this.row-1][this.column+1].who()==State.FOX)
population[FOX]++;
else if(this.world.grid[this.row-1][this.column+1].who()==State.EMPTY)
population[EMPTY]++;
else if(this.world.grid[this.row-1][this.column+1].who()==State.BADGER)
population[BADGER]++;
}
}
//if possible, count living below
if(this.row+1<this.world.grid.length){
if(this.column-1>=0){
//if possible, count living below and to the left
if(this.world.grid[this.row+1][this.column-1].who()==State.RABBIT)
population[RABBIT]++;
else if(this.world.grid[this.row+1][this.column-1].who()==State.GRASS)
population[GRASS]++;
else if(this.world.grid[this.row+1][this.column-1].who()==State.FOX)
population[FOX]++;
else if(this.world.grid[this.row+1][this.column-1].who()==State.EMPTY)
population[EMPTY]++;
else if(this.world.grid[this.row+1][this.column-1].who()==State.BADGER)
population[BADGER]++;
}
//if possible, count living below and to the right
if(this.column+1<this.world.grid.length){
if(this.world.grid[this.row+1][this.column+1].who()==State.RABBIT)
population[RABBIT]++;
else if(this.world.grid[this.row+1][this.column+1].who()==State.GRASS)
population[GRASS]++;
else if(this.world.grid[this.row+1][this.column+1].who()==State.FOX)
population[FOX]++;
else if(this.world.grid[this.row+1][this.column+1].who()==State.EMPTY)
population[EMPTY]++;
else if(this.world.grid[this.row+1][this.column+1].who()==State.BADGER)
population[BADGER]++;
}
if(this.world.grid[this.row+1][this.column].who()==State.RABBIT)
population[RABBIT]++;
else if(this.world.grid[this.row+1][this.column].who()==State.GRASS)
population[GRASS]++;
else if(this.world.grid[this.row+1][this.column].who()==State.FOX)
population[FOX]++;
else if(this.world.grid[this.row+1][this.column].who()==State.EMPTY)
population[EMPTY]++;
else if(this.world.grid[this.row+1][this.column].who()==State.BADGER)
population[BADGER]++;
}
}
/**
* Gets the identity of the life form on the square.
* @return State
*/
public abstract State who();
/**
* Determines the life form on the square in the next cycle.
* @param wNew world of the next cycle
* @return Living
*/
public abstract Living next(World wNew);
}