forked from buckyoung/mousesim
/
Colony.java
83 lines (63 loc) · 2.05 KB
/
Colony.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
import java.util.LinkedList;
public class Colony {
//* Private Constants
private static final int MAX_MICE = MouseSim.getMaxMice();
//* Private Fields
private static LinkedList<Mouse> mice = new LinkedList<>();
private static LinkedList<Mouse> bornMice = new LinkedList<>();
private static LinkedList<Mouse> deadMice = new LinkedList<>();
//* Public Methods
public static void generateBaby(Position position, Mouse father, Mouse mother) {
if(canCreateMouse()) {
bornMice.add(new Mouse(new Position(position), father, mother));
}
}
public static void generateMice(int number, Position p, Mouse f, Mouse m) {
Stream.history("Colony.generateMice("+number+")");
if(p == null) {
p = new Position(MouseSim.rand.nextInt(MouseSim.getWorldSizeRow()), MouseSim.rand.nextInt(MouseSim.getWorldSizeCol()));
}
while(number > 0) {
generateBaby(p, f, m);
number--;
}
}
public static void update() {
Statistics.colonyReset();
for(Mouse mouse : mice) {
mouse.update();
if(!mouse.isAlive()) deadMice.add(mouse);
Statistics.colonyInclude(mouse);
}
Statistics.colonyReady();
while(!deadMice.isEmpty()) {
Mouse deadMouse = deadMice.remove();
mice.remove(deadMouse);
Stream.history("Colony Size: " + mice.size());
MouseSim.getWorld().getWorldNode(deadMouse.getPosition()).remove(deadMouse);
if(mice.size() == 1) {
Stream.update(mice.get(0) + " is the last mouse alive! x_x");
}
}
while(!bornMice.isEmpty()) {
Mouse bornMouse = bornMice.remove();
mice.add(bornMouse);
Stream.history("Colony Size: " + mice.size());
}
if(mice.isEmpty()) {
MouseSim.endGame("all the mice have died.");
}
}
private static boolean canCreateMouse() {
return mice.size() < MAX_MICE;
}
public static int getSize() {
return mice.size();
}
public static Mouse getCandidate(Mouse notThisMouse) {
for(Mouse mouse : mice) {
if(mouse.getGender() == Gender.MALE && !mouse.isBaby() && mouse.isAlive() && (mouse.getAge()/mouse.getDNA().getLifespan() < 0.5) && mouse != notThisMouse) return mouse;
}
return null;
}
}