-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCell.java
More file actions
265 lines (203 loc) · 7.54 KB
/
Cell.java
File metadata and controls
265 lines (203 loc) · 7.54 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// =============================================================================
/**
* A <code>Cell</code> keeps track of its own liveness. It also can determine
* its own evolution by examining its neighbors and applying its survival rules.
**/
// =============================================================================
// =============================================================================
public class Cell {
// =============================================================================
// =========================================================================
/**
* The specialized constructor. Create a new, initially-dead cell.
*
* @param grid The <code>Grid</code> that contains this cell.
* @param row The row coordinate of this cell within its <code>Grid</code>.
* @param column The column coordinate of this cell within its
* <code>Grid</code>.
**/
public Cell (Grid grid, int row, int column) {
// Set the initial state to be dead.
_isAlive = false;
_wasEverAlive = false;
// Store the grid and the coordinates within that grid.
_grid = grid;
_row = row;
_column = column;
} // Cell()
// =========================================================================
// =========================================================================
/**
* Indicate whether this cell is currently dead or alive.
*
* @return <code>true</code> if the cell is alive; <code>false</code> if it
* is dead.
**/
public boolean isAlive () {
return _isAlive;
} // isAlive()
// =========================================================================
// =========================================================================
/**
* Set the cell to be alive.
**/
public void makeAlive () {
_isAlive = true;
_wasEverAlive=true;
} // makeAlive ()
// =========================================================================
// =========================================================================
/**
* Set the cell to be dead.
**/
public void makeDead () {
_isAlive = false;
} // makeDead ()
// =========================================================================
// =========================================================================
/**
* Provide the row coordinate of this cell in its <code>Grid</code>.
*
* @return The row coordinate of this cell.
**/
public int getRow () {
return _row;
} // getRow ()
// =========================================================================
// =========================================================================
/**
* Provide the column coordinate of this cell in its <code>Grid</code>.
*
* @return The column coordinate of this cell.
**/
public int getColumn () {
return _column;
} // getColumn ()
// =========================================================================
// =========================================================================
/**
* Provide a textual representation of the cell's state.
*
* @return One particular character to represent liveness, another to
* represent deadness. The characters chosen depend on the type of
* cell, and thus are determined by the subclasses.
**/
public String toString () {
if (_isAlive) {
return "+";
} else {
return "-";
}
}
// =========================================================================
// =========================================================================
/**
* Traverse the standard neighborhood (the surrounding 8 <code>Cell</code>s
* in the <code>Grid</code>) and count the number of neighbors that are
* alive.
*
* @return The number of live neighbors in the standard neighborhood.
**/
//change it to private!!!!!!
private int countLiveNeighbors () {
int numOfNeighbors = 0;
// WRITE ME.
// iterating through the nine cells and skipping central one
// as well as the ones beyond matrix
for(int iteratingRow = this.getRow() - 1; iteratingRow < this.getRow() + 2; iteratingRow++) {
for (int iteratingCol = this.getColumn() - 1; iteratingCol < this.getColumn() + 2; iteratingCol++) {
if( iteratingRow < 0 || iteratingCol < 0 || iteratingRow >= this._grid.getRows() || iteratingCol >= this._grid.getColumns() || ( iteratingRow == this.getRow() && iteratingCol == this.getColumn() )) {
continue; // skip the unnecessary cells
}
else {
if ( this._grid.getCell(iteratingRow, iteratingCol).isAlive() ) {
numOfNeighbors++; //taking the alive cell into account
}
}
}
}
return numOfNeighbors;
}
// =========================================================================
// =========================================================================
/**
* Based on its neighbors' states, calculate what this cell's state will be
* in the <i>next</i> generation. Here, the Conway rules are that <i>a live
* cell with 2 or 3 live neighbors remains alive, a dead cell with 3 live
* neighbors becomes alive, and all other cells will die</i>.
**/
public void evolve ()
{
// WRITE ME.
//making a dead cell with 3 neighbors alive
// System.out.println("cell's evolved");
if( this.isAlive() == false && this.countLiveNeighbors() == 3)
{
this._willBeAlive = true;
this._willChange = true;
}
//all cells with less than 2 or more than 3 neighbors
else if (this.countLiveNeighbors() < 2 || this.countLiveNeighbors() > 3)
{
this._willBeAlive = false;
if(this.isAlive()) {
this._willChange = true;
}
else {
this._willChange = false;
}
}
//the cells that have 2 or 3 neighbors and are alive
else {
this._willChange = false;
}
} // evolve ()
// =========================================================================
// =========================================================================
/**
* Advance to the next generation. That is, adopt as the current
* liveness whatever was calculated by <code>evolve()</code>.
**/
public void advance () {
// System.out.println("cell's advanced");
// WRITE ME.
if (this._willChange) {
if(this._willBeAlive == true)
{
this.makeAlive();
}
else
{
this.makeDead();
}
}
}
// =========================================================================
// =========================================================================
// DATA MEMBERS
/**
* The current liveness.
**/
boolean _isAlive;
/**
* The liveness in the next generation.
**/
public boolean _willBeAlive;
public boolean _wasEverAlive;
public boolean _willChange;
/**
* The <code>Grid</code> that contains this cell.
**/
Grid _grid;
/**
* The cell's row coordinate within its <code>Grid</code>.
**/
int _row;
/**
* The cell's column coordinate within its <code>Grid</code>.
**/
int _column;
// =========================================================================
// =============================================================================
} // class Cell
// =============================================================================