-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppMain.java
More file actions
99 lines (86 loc) · 3.17 KB
/
AppMain.java
File metadata and controls
99 lines (86 loc) · 3.17 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
/**
* Based on John Conway's rules from his Game of Life.
* <p>
* <ol>
* <li>Any live cell with fewer than two live neighbours dies, as if caused by under-population.</li>
* <li>Any live cell with two or three live neighbours lives on to the next generation.</li>
* <li>Any live cell with more than three live neighbours dies, as if by overcrowding.</li>
* <li>Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.</li>
*</ol>
*
* @author PFFFT Productions
* @version 20140110
*/
public class AppMain implements Runnable
{
private boolean[][] array;
private boolean[][] arrayStep2;
public int updatesPerSecond = 1;
public boolean running = true;
public AppMain(int size)
{
array = new boolean [size][size];
arrayStep2 = new boolean [size][size];
//Spawns a square
array[12][12] = true;
array[12][13] = true;
array[13][12] = true;
array[13][13] = true;
//makes sure they despawn
array[5][5] = true;
//Should create a blinker
array[7][7] =true;
array[8][7] = true;
array[9][7] = true;
}
/**
* To be used to set the flagged array.
*/
private void checkLocation(int x, int y)
{
int localCount = 0;
if (y-1>=0 && array[x][y-1]) localCount++; //Top
if (y-1>=0 && x+1< array.length && array[x+1][y-1]) localCount++; //Top Right
if (x+1<array.length && array[x+1][y]) localCount++; //Right
if (x+1<array.length && y+1<array.length && array[x+1][y+1])localCount++; //Botton Right
if (y+1<array.length && array[x][y+1]) localCount++; //Bottom
if (y+1<array.length && x-1>=0 && array[x-1][y+1]) localCount++; //Bottom Left
if (x-1>=0 && array[x-1][y]) localCount++; //Left
if (x-1>=0 && y-1>=0 && array[x-1][y-1]) localCount++; //Top Left
if (localCount == 3) arrayStep2[x][y] = true;
else if (array[x][y] && localCount == 2) arrayStep2[x][y] = true;
else arrayStep2[x][y] = false;
}
public void update()
{
for (int x = 0; x < array.length; x++){
for (int y = 0; y < array.length; y++){
checkLocation(x,y);
}
}
array = arrayStep2;
arrayStep2 = new boolean[array.length][array.length];
}
public String toString()
{
String ret = "";
for (int i = 0; i < array.length; i++){
for (int j = 0; j < array[0].length; j++){
ret += array[i][j] + "\t";
}
ret += "\n";
}
return ret;
}
public boolean[][] getArray(){return array;}
public void run() {
while (true) {
if (running) {
update();
} else {
System.out.println("Simulation updated");
}
try { Thread.sleep(1000/updatesPerSecond); } catch (Exception E) {E.printStackTrace();}
}
}
}