-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeedsCAL.java
78 lines (77 loc) · 2.08 KB
/
SeedsCAL.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
import java.util.ArrayList;
import java.util.Random;
public class SeedsCAL {
public ArrayList<Cell> cells = new ArrayList<Cell>();
public SeedsCAL() {
for (int i = 0; i < gridgx; i++)
for (int j = 0; j < gridgy; j++) {
cells.add(new Cell(i, j));
}
for (Cell cell : cells) {
if (cell.x > 0)
cell.addNeighbors(getNeighbors(cell.x - 1, cell.y));
if (cell.y > 0)
cell.addNeighbors(getNeighbors(cell.x, cell.y - 1));
if (cell.x < gridgx - 1)
cell.addNeighbors(getNeighbors(cell.x + 1, cell.y));
if (cell.y < gridgy - 1)
cell.addNeighbors(getNeighbors(cell.x, cell.y + 1));
if (cell.x > 0 && cell.y > 0)
cell.addNeighbors(getNeighbors(cell.x - 1, cell.y - 1));
if (cell.x < gridgx - 1 && cell.y > 0)
cell.addNeighbors(getNeighbors(cell.x + 1, cell.y - 1));
if (cell.x > 0 && cell.y < gridgy - 1)
cell.addNeighbors(getNeighbors(cell.x - 1, cell.y + 1));
if (cell.x < gridgx - 1 && cell.y < gridgy - 1)
cell.addNeighbors(getNeighbors(cell.x + 1, cell.y + 1));
}
}
public Cell getNeighbors(int x, int y) {
for (int i = 0; i < cells.size(); i++)
if (cells.get(i).x == x && cells.get(i).y == y) {
System.out.println("Neighbor: " + cells.get(i).x + ", " + cells.get(i).y);
return (Cell) cells.get(i);
}
return null; // Shouldn't be reachable
}
public int gridgx = 50;
public int gridgy = 50;
public boolean life;
public int num_live_neighbors(Cell cell){
int live = 0;
for(Cell neighbor : cell.neighbors){
if(neighbor.life == true){
live++;}
}
return live;
}
public void cal_it(){
int liveneighbors = 0;
for(Cell cell : cells){
liveneighbors=num_live_neighbors(cell);
if(cell.life == false && liveneighbors == 2){
cell.life = true;
}
else{
cell.life = false;
}
}
}
public class Cell {
/* The below are standard attributes of all Cells: x, y, life, and neighbors */
public int x;
public int y;
public boolean life;
public ArrayList<Cell> neighbors = new ArrayList<Cell>();
public Cell(int x, int y) {
this.x = x;
this.y = y;
/* Random function inserted according to what is needed */
Random r = new Random();
life = r.nextBoolean();
}
// standard function in all Cell classes
public void addNeighbors(Cell c) {
this.neighbors.add(c);
}
}}