diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index ce6df6f..6caa37f 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -11,15 +11,13 @@
-
-
-
+
+
+
-
-
-
-
-
+
+
+
@@ -27,61 +25,21 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- JAVA
- com.zipcodeconway.ConwayGameOfLife
-
- com.zipcodeconway.ConwayGameOfLife
-
-
-
-
-
-
- Constructors
- Methods
-
- All
- private
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
-
-
+
+
-
-
+
+
+
+
@@ -92,8 +50,8 @@
@@ -126,9 +84,12 @@
-
+
+
+
+
@@ -138,11 +99,11 @@
true
DEFINITION_ORDER
-
-
-
-
-
+
+
+
+
+
@@ -159,55 +120,57 @@
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
@@ -217,10 +180,8 @@
-
-
-
+
@@ -238,14 +199,17 @@
-
-
+
+
+
+
+
-
-
+
+
@@ -260,7 +224,7 @@
-
+
@@ -287,20 +251,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -365,6 +315,31 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -473,19 +448,19 @@
-
-
-
-
-
+
+
+
+
+
-
-
+
+
-
-
+
+
@@ -503,71 +478,85 @@
1519668901598
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
+
+
-
-
+
+
-
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
+
-
-
+
@@ -579,35 +568,63 @@
-
+
-
+
-
-
+
+
-
+
-
-
+
+
-
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -629,64 +646,62 @@
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
+
-
+
-
+
-
-
+
+
-
+
-
-
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
+
+
+
-
+
-
-
+
+
+
+
diff --git a/Screen Shot 2018-03-12 at 8.42.34 PM.png b/Screen Shot 2018-03-12 at 8.42.34 PM.png
new file mode 100644
index 0000000..db2c40f
Binary files /dev/null and b/Screen Shot 2018-03-12 at 8.42.34 PM.png differ
diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
index 0d3b15b..a337e29 100644
--- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
+++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
@@ -1,11 +1,24 @@
package com.zipcodeconway;
public class ConwayGameOfLife {
+ int[][] currentGeneration;
+ int[][] nextGeneration;
+
+ public SimpleWindow displayWindow;
public ConwayGameOfLife(Integer dimension) {
- }
+ currentGeneration = createRandomStart(dimension);
+ nextGeneration = new int[dimension][dimension];
+ displayWindow = new SimpleWindow(dimension);
+
+ }
public ConwayGameOfLife(Integer dimension, int[][] startmatrix) {
+ currentGeneration = startmatrix;
+ nextGeneration = new int[dimension][dimension];
+ displayWindow = new SimpleWindow(dimension);
+
+
}
public static void main(String[] args) {
@@ -17,16 +30,43 @@ public static void main(String[] args) {
// 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[][] randomStart = new int[dimension][dimension];
+ for (int row = 0; row < dimension; row++) {
+ for (int col = 0; col < dimension; col++) {
+ randomStart[row][col] = (int) (Math.random() * 2);
+ }
+ }
+ return randomStart;
}
public int[][] simulate(Integer maxGenerations) {
- return new int[1][1];
+ int generations = 0;
+ while (generations <= maxGenerations) {
+ this.displayWindow.display(currentGeneration, generations);
+
+ for (int row = 0; row < currentGeneration.length; row++) {
+ for (int col = 0; col < currentGeneration.length; col++) {
+ nextGeneration[row][col] = isAlive(row, col, currentGeneration);
+ }
+ }
+
+ copyAndZeroOut(nextGeneration, currentGeneration);
+ this.displayWindow.sleep(125);
+ generations++;
+ }
+
+ return currentGeneration;
}
// 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) {
+ public void copyAndZeroOut(int[][] next, int[][] current) {
+ for (int row = 0; row < current.length; row++) {
+ for (int col = 0; col < current.length; col++) {
+ current[row][col] = next[row][col];
+ next[row][col] = 0;
+ }
+ }
}
// Calculate if an individual cell should be alive in the next generation.
@@ -38,6 +78,46 @@ 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 up = row -1;
+ int down = row + 1;
+ int right = col + 1;
+ int left = col -1;
+
+ int amountOfNeighborsAlive = 0;
+// Cell cell = new Cell(row, col);
+ int lifeDeathValue = world[row][col];
+
+ if (up < 0){
+ up = world[row].length -1;
+ }
+ if (down == world[row].length){
+ down = 0;
+ }
+ if (right == world[col].length){
+ right = 0;
+ }
+ if (left < 0){
+ left = world[col].length -1;
+ }
+
+
+ if (world[down][right] > 0) amountOfNeighborsAlive++;
+ if (world[down][left] > 0) amountOfNeighborsAlive++;
+ if (world[up][left] > 0) amountOfNeighborsAlive++;
+ if (world[up][right] > 0) amountOfNeighborsAlive++;
+ if (world[row][left] > 0) amountOfNeighborsAlive++;
+ if (world[row][right] > 0) amountOfNeighborsAlive++;
+ if (world[down][col] > 0) amountOfNeighborsAlive++;
+ if (world[up][col] > 0) amountOfNeighborsAlive++;
+
+ if (amountOfNeighborsAlive> 3 || amountOfNeighborsAlive < 2) {
+ lifeDeathValue = 0;
+ } else if (lifeDeathValue == 0 && amountOfNeighborsAlive == 3) {
+ lifeDeathValue = 1;
+ }
+ return lifeDeathValue; // only returns a one a zero
}
+
}
+
+
diff --git a/src/main/java/com/zipcodeconway/SimpleWindow.java b/src/main/java/com/zipcodeconway/SimpleWindow.java
index f315e00..7c00b2e 100644
--- a/src/main/java/com/zipcodeconway/SimpleWindow.java
+++ b/src/main/java/com/zipcodeconway/SimpleWindow.java
@@ -39,11 +39,11 @@ 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.BLACK);
g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
if (array[i][j] == 1) {
- g.setColor(Color.BLACK);
+ g.setColor(Color.PINK);
g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
}