-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractCell.java
More file actions
87 lines (65 loc) · 1.79 KB
/
Copy pathAbstractCell.java
File metadata and controls
87 lines (65 loc) · 1.79 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
public abstract class AbstractCell {
private boolean isAlive; // is the cell considered alive
private int row; // row position in world
private int column; // column position in world
private int cellAge; // age of cell (increment by 1 each turn)
private boolean isBorg; // a hack to test if a cell contains any type of borg
protected ConwayWorld world; // contains grid of all cells
public AbstractCell(int r, int c, ConwayWorld w) {
row = r;
column = c;
world = w;
isAlive = false;
cellAge = 0;
isBorg = false;
}
public int getAge() {
return cellAge;
}
public void setAge(int x) {
cellAge = x;
}
public boolean isBorg() {
return isBorg;
}
public void setBorg(boolean flag) {
isBorg = flag;
}
public char displayCharacter() {
return isAlive ? 'O' : '.';
}
public void setIsAlive(boolean value) {
isAlive = value;
}
public boolean getIsAlive() {
return isAlive;
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
}
public int neighborCount() {
int count = 0;
int r = getRow();
int c = getColumn();
// the row above
if (world.isAlive(r - 1, c - 1)) count++;
if (world.isAlive(r - 1, c)) count++;
if (world.isAlive(r - 1, c + 1)) count++;
// Same row as this cell
if (world.isAlive(r, c - 1)) count++;
if (world.isAlive(r, c + 1)) count++;
// The row below
if (world.isAlive(r + 1, c - 1)) count++;
if (world.isAlive(r + 1, c)) count++;
if (world.isAlive(r + 1, c + 1)) count++;
return count;
}
public AbstractCell BorgForNextGeneration() {
return this;
}
public abstract AbstractCell cellForNextGeneration();
public abstract boolean willBeAliveInNextGeneration();
}