-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldStats.java
More file actions
162 lines (148 loc) · 5.36 KB
/
FieldStats.java
File metadata and controls
162 lines (148 loc) · 5.36 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
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
import java.util.HashMap;
/** This class collects and provides some statistical data on the state
* of a field. It is flexible: it will create and maintain a counter
* for any class of object that is found within the field */
public class FieldStats
{
// Counters for each type of entity (human etc.) in the simulation.
private HashMap<Class, Counter> counters;
// Whether the counters are currently up to date.
private boolean countsValid;
//counts the dead people.
public int count;
/** Construct a FieldStats object */
public FieldStats() {
// Set up a collection for counters
counters = new HashMap<>();
countsValid = true;
count = 0;
}
/** Get details of what is in the field.
* @return A string describing what is in the field */
public String getPopulationDetails(Field field) {
StringBuffer buffer = new StringBuffer();
if(!countsValid) {
generateCounts(field);
}
for(Class key : counters.keySet()) {
Counter info = counters.get(key);
buffer.append(info.getCount());
buffer.append(' ');
}
return buffer.toString();
}
/** Invalidate the current set of statistics; reset all counts to zero */
public void reset() {
countsValid = false;
for(Class key : counters.keySet()) {
Counter count = counters.get(key);
count.reset();
}
}
/** Increment the count for class of human.
* @param human The class to increment */
public void incrementCount(Class human) {
Counter count = counters.get(human);
if(count == null) {
// We do not have a counter for this species yet.
// Create one.
count = new Counter(human.getName());
counters.put(human, count);
}
count.increment();
}
/** Indicate that a human count has been completed */
public void countFinished() {
countsValid = true;
}
/** Determine whether the simulation is still viable. I.e., should it continue to run.
* @return true If there is more than one species alive */
public boolean isViable(Field field) {
// How many counts are non-zero.
int nonZero = 0;
if(!countsValid) {
generateCounts(field);
}
for(Class key : counters.keySet()) {
Counter info = counters.get(key);
if(info.getCount() > 0) {
nonZero++;
}
}
return nonZero > 1;
}
/** Generate counts of the number of human. These are not kept up to date as human
* are placed in the field, but only when a request is made for the information.
* @param field the field to generate the stats for */
private void generateCounts(Field field) {
reset();
for(int row = 0; row < field.getDepth(); row++) {
for(int col = 0; col < field.getWidth(); col++) {
Object human = field.getObjectAt(row, col);
if(human != null) {
incrementCount(human.getClass());
}
}
}
countsValid = true;
}
/** Count the number of infected people shown in the field
* @param field the field to generate the stats for
* @return the number of the infected in the field */
public int infectedCount(Field field){
int count = 0;
reset();
for(int row = 0; row < field.getDepth(); row++) {
for(int col = 0; col < field.getWidth(); col++) {
Human human = field.getObjectAt(row, col);
if(human != null) {
if(human.isInfected()){
count++;
}
}
}
}
countsValid = true;
return count;
}
/** Count the number of vaccinated people shown in the field
* @param field the field to generate the stats for
* @return the number of the infected in the field */
public int vaccinatedCount(Field field){
int count = 0;
reset();
for(int row = 0; row < field.getDepth(); row++) {
for(int col = 0; col < field.getWidth(); col++) {
Human human = field.getObjectAt(row, col);
if(human != null) {
if(human.isVaccinated()){
count++;
}
}
}
}
countsValid = true;
return count;
}
/** Count the number of dead people until this moment of the simulation
* @param field the field to generate the stats for
* @return the number of the dead until this moment */
public int deadCount(Field field){
reset();
if(vaccinatedCount(field) == 0){
return 0;
}
for(int row = 0; row < field.getDepth(); row++) {
for(int col = 0; col < field.getWidth(); col++) {
Human human = field.getObjectAt(row, col);
if(human != null) {
if(human.getAge() == 80){
count++;
}
}
}
}
countsValid = true;
return count;
}
}