-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCell.java
More file actions
207 lines (177 loc) · 4.32 KB
/
Cell.java
File metadata and controls
207 lines (177 loc) · 4.32 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
import java.awt.Color;
import java.util.List;
import java.util.HashMap;
/**
* A superclass representing a cell in the simulation.
* Contains methods for getting and setting the cell's location, field, and
* state.
*
* @author Rayan Popat (K21056367) & James Coward (K22004743)
* @version 2023.02.23
*/
public abstract class Cell {
// fields
// Whether the cell is alive or not.
private boolean alive;
// The cell's field.
private Field field;
// The cell's position in the field.
private Location location;
// The cell's color
private Color color = Color.white;
// The cell's next type
private CellType nextCellType;
// The cell's type
private CellType type;
// The cell's age
private int age;
// chance of neighbour to be infected by cancer
protected final double spreadChance = 0.05;
/**
* Constructor for objects of class Cell
*
* @param field The field currently occupied.
* @param location The location within the field.
* @param type The type of cell.
*/
public Cell(Field field, Location location, CellType type) {
alive = true;
this.field = field;
setLocation(location);
this.type = type;
setAge(0);
}
// mutator methods
/**
* Make this cell act - that is: the cell decides it's status in the
* next generation.
*/
abstract public void act();
/**
* Check whether the cell is alive or not.
*
* @return true if the cell is still alive.
*/
protected boolean isAlive() {
return alive;
}
/**
* Changes the type of cell.
*
* @param nextType The cell's next type.
*/
public void updateState(CellType nextType) {
nextCellType = nextType;
}
/**
* Mutates the color of the cell
*/
public void setColor(Color col) {
color = col;
}
/**
* @return The cell's color.
*/
public Color getColor() {
return color;
}
/**
* Return the cell's location.
*
* @return The cell's location.
*/
protected Location getLocation() {
return location;
}
/**
* Place the cell at the new location in the given field.
*
* @param location The cell's location.
*/
protected void setLocation(Location location) {
this.location = location;
field.place(this, location);
}
/*
* Increments the cell's age.
*/
protected void addAge() {
this.age = this.age + 1;
}
/*
* Sets the cell's age.
*/
protected void setAge(int age) {
this.age = age;
}
// accessor methods
/**
* Return the cell's field.
*
* @return The cell's field.
*/
protected Field getField() {
return field;
}
/*
* Returns the cell's type.
*
* @return The cell's type.
*/
protected CellType getType() {
return type;
}
/*
* Returns the cell's next type.
*
* @return The cell's next type.
*/
protected CellType getNextType() {
return nextCellType;
}
/*
* Returns a list of the cell's neighbours.
*
* @return A list of the cell's neighbours.
*/
protected List<Cell> getNeighbours() {
return getField().getLivingNeighbours(getLocation());
}
/*
* Returns a hashmap of the cell's neighbours.
* Key is the cell type, value is the number of neighbours of that type.
*
* @return A hashmap of the cell's neighbours.
*/
protected HashMap<CellType, Integer> getNeighbourTypes() {
HashMap<CellType, Integer> neighbourTypes = new HashMap<>();
List<Cell> neighbours = getNeighbours();
for (Cell neighbour : neighbours) {
neighbourTypes.merge(neighbour.getType(), 1, Integer::sum);
}
return neighbourTypes;
}
/*
* Checks if the cell has a cancerous neighbour.
*
* @return true if the cell has a cancerous neighbour.
*/
protected boolean checkMalignant() {
List<Cell> neighbours = getNeighbours();
for (Cell neighbour : neighbours) {
if (neighbour.getType() == CellType.CANCER) {
Cancer currentCancer = (Cancer) neighbour;
return currentCancer.isMalignant();
}
}
return false;
}
/*
* Returns the cell's age.
*
* @return The cell's age.
*/
protected int getAge() {
return this.age;
}
}