diff --git a/.idea/misc.xml b/.idea/misc.xml
index 05e1d17..5ba6911 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -7,5 +7,5 @@
-
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index ce6df6f..eab56a7 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -1,7 +1,10 @@
-
+
+
+
+
@@ -11,12 +14,12 @@
-
+
-
-
+
+
@@ -24,63 +27,21 @@
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- JAVA
- com.zipcodeconway.ConwayGameOfLife
-
- com.zipcodeconway.ConwayGameOfLife
-
-
-
-
-
-
- Constructors
- Methods
-
- All
- private
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
-
+
-
-
+
+
@@ -100,6 +61,7 @@
end
+ north
currentGeneration
@@ -126,8 +88,8 @@
-
+
@@ -138,11 +100,18 @@
true
DEFINITION_ORDER
-
-
-
-
-
+
+
+
+
+
+
+
+
@@ -159,55 +128,57 @@
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
@@ -217,17 +188,15 @@
-
-
-
+
-
+
@@ -238,14 +207,15 @@
-
-
+
+
+
-
-
+
+
@@ -260,7 +230,7 @@
-
+
@@ -287,20 +257,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -365,6 +321,31 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -473,19 +454,19 @@
-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
@@ -503,30 +484,34 @@
1519668901598
+
+
+
+
-
+
-
+
-
+
-
+
-
-
+
+
-
-
+
+
-
-
+
+
@@ -534,41 +519,43 @@
+
+
+
-
+
-
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
+
-
-
+
+
+
@@ -584,30 +571,120 @@
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
+
+
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -629,39 +706,38 @@
-
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
+
-
-
+
+
@@ -670,22 +746,8 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
index 0d3b15b..e9ba026 100644
--- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
+++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
@@ -2,31 +2,69 @@
public class ConwayGameOfLife {
+ private int[][] currentGen;
+ private int[][] nextGen;
+ private SimpleWindow displayWindow;
+
public ConwayGameOfLife(Integer dimension) {
+
+ this.currentGen = createRandomStart(dimension);
+ this.nextGen = new int [dimension][dimension];
+ this.displayWindow = new SimpleWindow(dimension);
}
public ConwayGameOfLife(Integer dimension, int[][] startmatrix) {
+
+ this.currentGen = startmatrix;
+ this.nextGen = new int [dimension][dimension];
+ this.displayWindow = new SimpleWindow(dimension);
}
public static void main(String[] args) {
ConwayGameOfLife sim = new ConwayGameOfLife(50);
- int[][] endingWorld = sim.simulate(50);
+ int[][] endingWorld = sim.simulate(500);
}
// Contains the logic for the starting scenario.
// Which cells are alive or dead in generation 0.
// allocates and returns the starting matrix of size 'dimension'
private int[][] createRandomStart(Integer dimension) {
- return new int[1][1];
+ int[][] randStart = new int[dimension][dimension];
+ for (int i = 0; i < randStart.length; i++) {
+ for (int j = 0; j < randStart.length; j++) {
+ randStart[i][j] = (int)Math.round(Math.random());
+ }
+ } return randStart;
}
public int[][] simulate(Integer maxGenerations) {
- return new int[1][1];
+ int generations = 0;
+
+ while (generations <= maxGenerations) {
+ this.displayWindow.display(currentGen, generations);
+
+ for (int i = 0; i < currentGen.length; i++) {
+ for (int j = 0; j < currentGen.length; j++) {
+ nextGen[i][j] = isAlive(i, j, currentGen);
+ }
+ }
+ this.displayWindow.sleep(125);
+ copyAndZeroOut(nextGen, currentGen);
+ generations++;
+ }
+ return currentGen;
}
// copy the values of 'next' matrix to 'current' matrix,
// and then zero out the contents of 'next' matrix
public void copyAndZeroOut(int [][] next, int[][] current) {
+
+ for (int i = 0; i < current.length; i++) {
+ for (int j = 0; j < current.length; j++) {
+ current[i][j] = next[i][j];
+ next[i][j] = 0;
+ }
+ }
}
// Calculate if an individual cell should be alive in the next generation.
@@ -38,6 +76,39 @@ public void copyAndZeroOut(int [][] next, int[][] current) {
Any dead cell with exactly three live neighbours cells will come to life.
*/
private int isAlive(int row, int col, int[][] world) {
- return 0;
+
+ int numberOfLivingNeighbors = numberOfNeighborsAlive(row, col, world);
+
+ if (numberOfLivingNeighbors > 2 && numberOfLivingNeighbors <= 3)
+ return 1;
+ else if (numberOfLivingNeighbors < 2 || numberOfLivingNeighbors > 3)
+ return 0;
+ else return world[row][col];
+
+ }
+
+ public int numberOfNeighborsAlive(int row, int col, int[][] world) {
+
+ int north = col - 1;
+ int south = col + 1;
+ int east = row + 1;
+ int west = row -1;
+
+ if (north < 0) {
+ north = world[row].length-1;
+ }
+ if (south == world[row].length) {
+ south = 0;
+ }
+ if (east == world[col].length) {
+ east = 0;
+ }
+ if (west < 0) {
+ west = world[col].length - 1;
+ }
+
+ return world[row][north] + world[row][south] + world[east][col] + world[west][col]
+ + world[east][north] + world[east][south] + world[west][north] + world[west][south];
+
}
}
diff --git a/src/main/java/com/zipcodeconway/Screen Shot 2018-03-12 at 4.42.28 PM.png b/src/main/java/com/zipcodeconway/Screen Shot 2018-03-12 at 4.42.28 PM.png
new file mode 100644
index 0000000..2ba4e8f
Binary files /dev/null and b/src/main/java/com/zipcodeconway/Screen Shot 2018-03-12 at 4.42.28 PM.png differ
diff --git a/src/main/java/com/zipcodeconway/SimpleWindow.java b/src/main/java/com/zipcodeconway/SimpleWindow.java
index f315e00..e036fc5 100644
--- a/src/main/java/com/zipcodeconway/SimpleWindow.java
+++ b/src/main/java/com/zipcodeconway/SimpleWindow.java
@@ -39,7 +39,7 @@ public void display(int[][] array, Integer n) {
for (int j = 0; j < array[0].length; j++) {
g.drawRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
if (array[i][j] == 0) {
- g.setColor(Color.WHITE);
+ g.setColor(Color.GREEN);
g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
if (array[i][j] == 1) {