-
Notifications
You must be signed in to change notification settings - Fork 0
/
LifeController.java
40 lines (32 loc) · 1.1 KB
/
LifeController.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
package lifegame;
/**
* @author ifedko
*/
public class LifeController {
LifeController() {
}
public LifeArea fillArea(LifeArea area) throws Exception {
area.randomFillArea();
return area;
}
public LifeArea updateArea(LifeArea currentArea) throws Exception {
LifeArea nextArea = new LifeArea(currentArea.getAreaRows(), currentArea.getAreaColumns());
int iMax = currentArea.getAreaRows();
int jMax = currentArea.getAreaColumns();
LifeCell[][] currentCells = currentArea.getCells();
for (int i = 0; i < iMax; i++) {
for(int j = 0; j < jMax; j++) {
LifeCell cell = currentCells[i][j];
int countAliveNeighbors = currentArea.getCountNeighbors(cell);
if (cell.isDead() && countAliveNeighbors == 3) {
cell.born();
}
if (cell.isAlive() && (countAliveNeighbors < 2 || countAliveNeighbors > 3)) {
cell.die();
}
nextArea.addCell(cell);
}
}
return nextArea;
}
}