-
Notifications
You must be signed in to change notification settings - Fork 0
/
Season.java
98 lines (70 loc) · 2.17 KB
/
Season.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
package sim.app.birds;
import sim.engine.*;
import sim.field.continuous.*;
import sim.util.*;
import java.util.*;
public class Season implements Steppable {
private LinkedList<Mating> matings;
public Season() { matings = new LinkedList<Mating>(); }
public void addMating(Mating m) {
matings.add(m);
}
public void step(SimState state) {
System.out.println("Season stepping: " + state.schedule.getTimestamp("", "") + " Num Matings: " + matings.size());
Continuous2D world = ((BirdsModel)state).getWorld();
Bag birds = world.clear();
// Cull the dead
LinkedList<Bird> survivors = cull(birds);
// Create the list of offspring
LinkedList<Bird> young = doBirths();
System.out.println("Num Survivors: " + survivors.size() + " Num Young: " + young.size());
// Put the next generation of birds into the scheduler and the world
//((BirdsModel)state).reschedule(new LinkedList<Bird>(survivors, young));
// Nasty, because it makes use of side-effects, but this list is only visible in this
// function.
young.addAll(survivors);
((BirdsModel)state).reschedule(young);
// Create a new list of matings for the next season.
matings = new LinkedList<Mating>();
}
private LinkedList<Bird> doBirths() {
Iterator iter = matings.iterator();
LinkedList<Bird> young = new LinkedList<Bird>();
try {
Mating m = (Mating)iter.next();
while( m != null ) {
Bird mom = m.getFemale();
Bird dad = m.getMale();
// Two offspring per mating
Bird b1 = new Bird(mom, dad);
b1.setAge(0);
b1.setPos(m.getPosition());
Bird b2 = new Bird(mom, dad);
b2.setAge(0);
b2.setPos(m.getPosition());
young.add(b1); young.add(b2);
mom.setPregnant(false);
m = (Mating)iter.next();
}
} catch( NoSuchElementException e ) {
}
return young;
}
private LinkedList<Bird> cull(Bag birds) {
Iterator iter = birds.iterator();
LinkedList<Bird> retval = new LinkedList<Bird>();
try {
Bird b = (Bird)iter.next();
while( b != null ) {
int a = b.getAge();
if( a < 2 ) {
b.setAge(1 + a);
retval.add(b);
}
b = (Bird)iter.next();
}
} catch( NoSuchElementException e ) {
}
return retval;
}
}