-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulate.java
107 lines (98 loc) · 4.02 KB
/
Simulate.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
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
import java.util.List;
import java.util.Scanner;
import java.awt.event.KeyEvent;
public class Simulate
{
private Grid old_board;
private Grid new_board;
public Simulate(int rows, int cols, String fileName, boolean generate_cells){
this.run(rows, cols, fileName, generate_cells);
}
public void updateNewBoard(){
//fewer than 2 (under population), more than 3 (over population), dead cell with EXACTLY 3 live neighbors is alive due to reproduction
/*
* [c0][c1][c2]
* [c7][CV][c3]
* [c6][c5][c4]
*/
boolean[][] old_boardGridValues = old_board.getGridValues();
int iter = 0;
for(int i = 0; i < old_board.rows; i++){
for(int j = 0; j < old_board.cols; j++){
if(i == 0 || i == old_board.cols - 1 || j == 0 || j == old_board.rows - 1){
} else {
boolean[] neighboringCells = new boolean[8];
boolean CV = old_boardGridValues[i][j];
neighboringCells[0] = old_boardGridValues[i - 1][j + 1];
neighboringCells[1] = old_boardGridValues[i][j + 1];
neighboringCells[2] = old_boardGridValues[i + 1][j + 1];
neighboringCells[3] = old_boardGridValues[i + 1][j];
neighboringCells[4] = old_boardGridValues[i + 1][j - 1];
neighboringCells[5] = old_boardGridValues[i][j - 1];
neighboringCells[6] = old_boardGridValues[i - 1][j - 1];
neighboringCells[7] = old_boardGridValues[i - 1][j];
int count = 0;
for(boolean cellValue : neighboringCells){
if(cellValue){
count++;
}
}
System.out.println("iteration: " + iter + " " + CV + " x: " + i + " y: " + j + " neighbor_count: " + count);
if(CV){
System.out.println("x: " + i + " y: " + j + " neighbor_count: " + count);
if(count < 2 || count > 3){
System.out.println("DEAD: " + "x: " + i + " y: " + j);
new_board.dead(new Coordinate(i, j));
}
if(count == 2 || count == 3){
new_board.alive(new Coordinate(i, j));
}
} else {
if(count == 3){
System.out.println("ALIVE: " + "x: " + i + " y: " + j);
new_board.alive(new Coordinate(i, j));
}
}
}
}
iter++;
}
}
public Grid getOld(){
return old_board;
}
public void tick(){
this.updateNewBoard();
new_board.draw();
old_board = new_board;
}
private void run(int rows, int cols, String fileName, boolean generateCells){
Grid g;
Reader r;
if(fileName != null && generateCells){
r = new Reader(fileName);
Scanner sc = new Scanner(System.in);
System.out.println(">> Number of cells: ");
int num_generated_cells = sc.nextInt();
CellGenerator cg = new CellGenerator(fileName, num_generated_cells, rows, cols);
g = new Grid(rows, cols, r.getLiveCells());
} else if(fileName != null && !generateCells) {
r = new Reader(fileName);
g = new Grid(rows, cols, r.getLiveCells());
} else {
g = new Grid(rows, cols);
}
old_board = g;
new_board = old_board;
old_board.draw();
}
public static void main(String fileName) throws InterruptedException{
Simulate s = new Simulate(50,50, fileName, true);
while(s.old_board.cellsOnGrid > 0){
if(StdDraw.isKeyPressed(KeyEvent.VK_ENTER)){
s.tick();
Thread.sleep(60);
}
}
}
}