-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrc_FieldStats.java
More file actions
96 lines (77 loc) · 2.14 KB
/
src_FieldStats.java
File metadata and controls
96 lines (77 loc) · 2.14 KB
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
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import processing.core.PApplet;
public class FieldStats {
private HashMap<Class, Counter> counts;
private boolean countsValid = false;
public FieldStats(){
counts = new HashMap<Class, Counter>();
}
public String getPopulationDetails(Field field)
{
StringBuffer buffer = new StringBuffer();
if(!countsValid) {
generateCounts(field);
}
for(Class key : counts.keySet()) {
Counter info = counts.get(key);
buffer.append(info.getName());
buffer.append(": ");
buffer.append(info.getCount());
buffer.append(' ');
}
return buffer.toString();
}
public void reset()
{
countsValid = false;
for(Class key : counts.keySet()) {
Counter count = counts.get(key);
count.reset();
}
}
public void incrementCount(Class organismClass)
{
Counter count = counts.get(organismClass);
if(count == null) {
count = new Counter(organismClass);
counts.put(organismClass, count);
}
count.increment();
}
public void countFinished()
{
countsValid = true;
}
public boolean isViable(Field field)
{
int nonZero = 0;
if(!countsValid) {
generateCounts(field);
}
for(Class key : counts.keySet()) {
Counter info = counts.get(key);
if(info.getCount() > 0) {
nonZero++;
}
}
return nonZero > 1;
}
public void generateCounts(Field field)
{
reset();
for(int row = 0; row < field.getHeight(); row++) {
for(int col = 0; col < field.getWidth(); col++) {
Object organism = field.getObjectAt(col, row);
if(organism != null) {
incrementCount(organism.getClass());
}
}
}
countsValid = true;
}
public Collection<Counter> getCounts() {
return this.counts.values();
}
}