Repository navigation
Expand file tree
/
Copy pathCell.java
More file actions
89 lines (76 loc) · 2.13 KB
/
Copy pathCell.java
File metadata and controls
89 lines (76 loc) · 2.13 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
public class Cell {
private State _state = State.DEAD;
private int _xLoc = 0;
private int _yLoc = 0;
/**
* Public getter for the cell
* @return Current state of the cell
*/
public State state() {
return _state;
}
/**
* Returns whether or not the cell is alive.
* @return True if cell alive, false if dead
*/
public boolean isAlive() {
if (_state == State.ALIVE) {
return true;
} else {
return false;
}
}
/**
* Any live cell with fewer than two live neighbors dies, as if caused by under-population.
* Any live cell with two or three live neighbors lives on to the next generation.
* Any live cell with more than three live neighbors dies, as if by overcrowding.
* Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
* @param int numNeighbors - the current number of neighbors
* @return State - the new state of the cell (alive or dead)
*/
public State iterate(int numNeighbors) {
State toReturn = null;
if (_state == State.ALIVE) {
if (numNeighbors < 2 || numNeighbors > 3) {
toReturn = State.DEAD;
} else {
toReturn = State.ALIVE;
}
} else if (_state == State.DEAD) {
if (numNeighbors == 3) {
toReturn = State.ALIVE;
} else {
toReturn = State.DEAD;
}
}
return toReturn;
}
/**
* Get the character to display for the current state.
* Modifying this code will allow you to change the terminal display
* of when a cell is alive or dead.
* @return Character representation of the cell's state
*/
public char getStateRep() {
char toReturn = ' ';
if (_state == State.DEAD) {
toReturn = '.';
} else if (_state == State.ALIVE) {
toReturn = 'X';
} else {
toReturn = '?';
}
return toReturn;
}
/**
* Constructor for a new cell.
* @param initialState - Initial state of a cell, dead or alive
* @param xLoc - x location
* @param yLoc - y location
*/
public Cell(State initialState, int xLoc, int yLoc) {
_state = initialState;
_xLoc = xLoc;
_yLoc = yLoc;
}
}