-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGrid.java
More file actions
223 lines (199 loc) · 6.36 KB
/
Grid.java
File metadata and controls
223 lines (199 loc) · 6.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package denguesimulation;
import java.util.ArrayList;
public class Grid {
Person[][] grid;
int maxRows;
int maxColumns;
public Grid(int population) {
int[] dimensions = getDimensions(population);
maxRows = dimensions[0];
maxColumns = dimensions[1];
grid = new Person[maxRows][maxColumns];
initialize(population);
}
private int[] getDimensions(int population) {
double squareRoot = Math.sqrt(population);
int[] dimensions = new int[2];
if (squareRoot == Math.floor(squareRoot)) {
dimensions[0] = (int)squareRoot;
dimensions[1] = (int)squareRoot;
} else {
dimensions[0] = (int)Math.ceil(squareRoot);
dimensions[1] = (int)Math.ceil(squareRoot);
}
return dimensions;
}
public int getMaxRows() {
return maxRows;
}
public int getMaxColumns() {
return maxColumns;
}
//fill grid up with persons
public void initialize(int population){
int counter = 0;
for (int i = 0; i < maxRows; i++) {
for (int j = 0; j < maxColumns; j++) {
Person p = new Person(i, j);
grid[i][j] = p;
if(counter >= population) {
p.setState(0);
}
counter++;
}
}
}
//returns a person's neighbors by looking at the 8 persons surrounding it in the grid
public ArrayList<Person> getNeighbors(Person person) {
ArrayList<Person> neighbors = new ArrayList();
ArrayList<Coordinates> neighborCoordinates = person.getNeightbors();
for(int i=0; i<neighborCoordinates.size(); i++) {
Coordinates tempCoordinates = neighborCoordinates.get(i);
if(isValidCoordinates(neighborCoordinates.get(i))) {
Person neighbor = grid[tempCoordinates.getX()][tempCoordinates.getY()];
neighbors.add(neighbor);
}
}
return neighbors;
}
//checks if the coordinates for its neighbors don't go beyond the grid's capacity
private boolean isValidCoordinates(Coordinates coordinates) {
int x = coordinates.getX();
int y = coordinates.getY();
if((x >= maxRows) || (x < 0)) {
return false;
} else if ((y >= maxColumns) || (y < 0)) {
return false;
} else {
return true;
}
}
//change state to susceptible(1), infected(2) or resistant(3)
public void changeState(int x, int y, int state) {
grid[x][y].setState(state);
}
//returns person at coordinate x, y
public Person getPerson(int x, int y) {
return grid[x][y];
}
//returns number of susceptible persons
public int getSusceptibleCount() {
int susceptibleCount = 0;
for (int i = 0; i < maxRows; i++) {
for (int j = 0; j < maxColumns; j++) {
Person currPerson = grid[i][j];
if(currPerson.isSusceptible()) {
susceptibleCount++;
}
}
}
return susceptibleCount;
}
//returns the coordinates of persons who are infected
public ArrayList<Coordinates> getInfectedCoordinates() {
ArrayList<Coordinates> infected = new ArrayList();
for (int i = 0; i < maxRows; i++) {
for (int j = 0; j < maxColumns; j++) {
Person currPerson = grid[i][j];
if(currPerson.getState() == 2) {
infected.add(currPerson.getCoordinates());
}
}
}
return infected;
}
//sets state of persons in given coordinates to infected(2)
public void setInfectedCoordinates(ArrayList<Coordinates> infectedCoordinates) {
for(int i=0; i<infectedCoordinates.size(); i++) {
Coordinates currCoordinates = infectedCoordinates.get(i);
int x = currCoordinates.getX();
int y = currCoordinates.getY();
this.changeState(x, y, 2);
}
}
//returns number of infected persons
public int getInfectedCount() {
int infectedCount = 0;
for (int i = 0; i < maxRows; i++) {
for (int j = 0; j < maxColumns; j++) {
Person currPerson = grid[i][j];
if(currPerson.isInfected()) {
infectedCount++;
}
}
}
return infectedCount;
}
//returns the coordinates of persons who are resistant
public ArrayList<Coordinates> getResistantCoordinates() {
ArrayList<Coordinates> infected = new ArrayList();
for (int i = 0; i < maxRows; i++) {
for (int j = 0; j < maxColumns; j++) {
Person currPerson = grid[i][j];
if(currPerson.getState() == 3) {
infected.add(currPerson.getCoordinates());
}
}
}
return infected;
}
//sets state of persons in given coordinates to resistant(3)
public void setResistantCoordinates(ArrayList<Coordinates> resistantCoordinates) {
for(int i=0; i<resistantCoordinates.size(); i++) {
Coordinates currCoordinates = resistantCoordinates.get(i);
int x = currCoordinates.getX();
int y = currCoordinates.getY();
this.changeState(x, y, 3);
}
}
//returns number of resistant persons
public int getResistantCount() {
int resistantCount = 0;
for (int i = 0; i < maxRows; i++) {
for (int j = 0; j < maxColumns; j++) {
Person currPerson = grid[i][j];
if(currPerson.isResistant()) {
resistantCount++;
}
}
}
return resistantCount;
}
//increases each person's currStateTimeStep by 1, except for those who have changed states (becomes 0)
public void updateTimes(ArrayList<Coordinates> changedCoordinates) {
for (int i = 0; i < maxRows; i++) {
for (int j = 0; j < maxColumns; j++) {
Person currPerson = grid[i][j];
if(changedCoordinates.contains(currPerson.getCoordinates())) {
currPerson.resetCurrStateTimeStep();
} else {
currPerson.incCurrStateTimeStep();
}
}
}
}
//displays the grid with each persons's state: 1-susceptible, 2-infected, 3-resistant
public String display() {
String display = "";
for (int i = 0; i < maxRows; i++) {
display += "[ ";
for (int j = 0; j < maxColumns; j++) {
display += grid[i][j].getState() + " ";
}
display += "] \n";
}
return display;
}
//displays the grid containing how many time steps each individual has been at its current state
public String displayTimes() {
String display = "";
for (int i = 0; i < maxRows; i++) {
display += "[ ";
for (int j = 0; j < maxColumns; j++) {
display += grid[i][j].getCurrStateTimeStep() + " ";
}
display += "] \n";
}
return display;
}
}