-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.java
126 lines (110 loc) · 2.81 KB
/
Cell.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
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
import javafx.scene.paint.Color;
/**
* A class representing the shared characteristics of all forms of life
*
* @author David J. Barnes, Michael Kölling & Jeffery Raphael
* @version 2022.01.06
*/
public abstract class Cell {
private boolean alive;
private boolean nextAlive; // The state of the cell in the next iteration
private int livingGenerations; // How many generations the cell has been alive
private Field field;
private Location location;
private Color color = Color.WHITE;
/**
* Create a new cell at location in field.
*
* @param field The field currently occupied.
* @param location The location within the field.
*/
public Cell(Field field, Location location, Color col) {
alive = true;
nextAlive = false;
livingGenerations = 0;
this.field = field;
setLocation(location);
setColor(col);
}
/**
* 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;
}
/**
* Indicate that the cell is no longer alive.
*/
protected void setDead() {
alive = false;
}
/**
* Updates number of generations the cell has been alive
*/
protected void updateGeneration(boolean state) {
if (state) {
livingGenerations++;
}
else {
livingGenerations = 0;
}
}
/**
* Return number of generations the cell has been alive
*/
protected int getLivingGenerations() {
return livingGenerations;
}
/**
* Indicate that the cell will be alive or dead in the next generation.
*/
public void setNextState(boolean value) {
nextAlive = value;
}
/**
* Changes the state of the cell
*/
public void updateState() {
alive = nextAlive;
}
/**
* Changes the color of the cell
*/
public void setColor(Color col) {
color = col;
}
/**
* Returns 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);
}
/**
* Return the cell's field.
* @return The cell's field.
*/
protected Field getField() {
return field;
}
}