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..898674d 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -1,7 +1,12 @@
-
+
+
+
+
+
+
@@ -15,8 +20,8 @@
-
-
+
+
@@ -27,59 +32,24 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- JAVA
- com.zipcodeconway.ConwayGameOfLife
-
- com.zipcodeconway.ConwayGameOfLife
-
-
-
-
-
-
- Constructors
- Methods
-
- All
- private
+
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
-
+
-
+
@@ -126,8 +96,8 @@
-
+
@@ -139,10 +109,10 @@
DEFINITION_ORDER
-
-
+
+
-
+
@@ -159,55 +129,56 @@
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
@@ -217,10 +188,9 @@
-
-
-
+
+
@@ -260,7 +230,7 @@
-
+
@@ -481,8 +451,8 @@
-
-
+
+
@@ -503,26 +473,31 @@
1519668901598
+
+
-
+
-
+
-
+
-
+
-
+
-
+
+
+
+
@@ -536,39 +511,39 @@
-
+
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
+
-
-
+
+
@@ -584,30 +559,68 @@
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
+
+
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -629,39 +642,33 @@
-
+
+
+
+
+
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
+
-
-
+
+
@@ -670,24 +677,15 @@
-
-
-
-
-
-
-
-
-
+
+
+
-
-
-
-
-
-
+
+
+
diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
index 0d3b15b..2387fef 100644
--- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
+++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
@@ -1,11 +1,22 @@
package com.zipcodeconway;
public class ConwayGameOfLife {
+ private int[][] currentGen;
+ private int[][] nextGen;
+ private SimpleWindow displayWindow;
+
public ConwayGameOfLife(Integer dimension) {
+ this.currentGen = createRandomStart(dimension);
+ nextGen = new int[dimension][dimension];
+ this.displayWindow = new SimpleWindow(dimension);
+
}
public ConwayGameOfLife(Integer dimension, int[][] startmatrix) {
+ currentGen = startmatrix;
+ nextGen = new int[dimension][dimension];
+ this.displayWindow = new SimpleWindow(dimension);
}
public static void main(String[] args) {
@@ -17,27 +28,103 @@ 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];
+ // make new array and define with dimension
+ int[][] randomArray = new int[dimension][dimension];
+ for(int row = 0; row < dimension; row++) {
+ for(int col = 0; col < dimension; col++) {
+ randomArray[row][col] = (int) (Math.random() * 2);
+ }
+ }
+ return randomArray;
}
public int[][] simulate(Integer maxGenerations) {
- return new int[1][1];
+ int generations = 0;
+
+ while(generations <= maxGenerations) {
+ this.displayWindow.display(currentGen, generations);
+
+ for(int row = 0; row < currentGen.length; row++ ) {
+ for(int col = 0; col < currentGen[row].length; col++) {
+ nextGen[row][col] = isAlive(row, col, currentGen);
+ }
+ }
+ copyAndZeroOut(nextGen, currentGen);
+ this.displayWindow.sleep(125);
+ 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 row = 0; row < current.length; row++) {
+ for(int col = 0; col < current.length; col++) {
+ // set next = current
+ current[row][col] = next[row][col];
+ // zero out next
+ next[row][col] = 0;
+
+ }
+ }
+
+
}
// Calculate if an individual cell should be alive in the next generation.
// Based on the game logic:
- /*
+
+ private int isAlive(int row, int col, int[][] world) {
+ int numNeighbors = 0;
+ 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;
+ }
+
+
+ if(world[east][north] == 1) numNeighbors++;
+ if(world[east][col] == 1) numNeighbors++;
+ if(world[east][south] == 1) numNeighbors++;
+
+ if(world[west][north] ==1) numNeighbors++;
+ if(world[west][south] == 1) numNeighbors++;
+ if(world[west][col] == 1) numNeighbors++;
+
+ if(world[row][north] ==1) numNeighbors++;
+ if(world[row][south] ==1) numNeighbors++;
+
+ /*
Any live cell with fewer than two live neighbours dies, as if by needs caused by underpopulation.
Any live cell with more than three live neighbours dies, as if by overcrowding.
Any live cell with two or three live neighbours lives, unchanged, to the next generation.
Any dead cell with exactly three live neighbours cells will come to life.
*/
- private int isAlive(int row, int col, int[][] world) {
- return 0;
+ if(numNeighbors < 2 || numNeighbors > 3 ) {
+ return 0;
+ }else if(numNeighbors == 3){
+ return 1;
+ } else {
+ // return it unchanged
+ return world[row][col];
+ }
+
}
}
diff --git a/src/main/java/com/zipcodeconway/SimpleWindow.java b/src/main/java/com/zipcodeconway/SimpleWindow.java
index f315e00..971af4d 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.GREEN);
g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
if (array[i][j] == 1) {
- g.setColor(Color.BLACK);
+ g.setColor(Color.CYAN);
g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
}