diff --git a/.idea/.name b/.idea/.name
new file mode 100644
index 0000000..d920b20
--- /dev/null
+++ b/.idea/.name
@@ -0,0 +1 @@
+Game-Of-Life-Java
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 05e1d17..d30d09e 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -7,5 +7,7 @@
-
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
index f98eea2..6604c74 100644
--- a/.idea/modules.xml
+++ b/.idea/modules.xml
@@ -2,7 +2,7 @@
-
+
\ No newline at end of file
diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml
deleted file mode 100644
index e96534f..0000000
--- a/.idea/uiDesigner.xml
+++ /dev/null
@@ -1,124 +0,0 @@
-
-
-
-
- -
-
-
- -
-
-
- -
-
-
- -
-
-
- -
-
-
-
-
-
- -
-
-
-
-
-
- -
-
-
-
-
-
- -
-
-
-
-
-
- -
-
-
-
-
- -
-
-
-
-
- -
-
-
-
-
- -
-
-
-
-
- -
-
-
-
-
- -
-
-
-
-
- -
-
-
- -
-
-
-
-
- -
-
-
-
-
- -
-
-
-
-
- -
-
-
-
-
- -
-
-
-
-
- -
-
-
- -
-
-
- -
-
-
- -
-
-
- -
-
-
-
-
- -
-
-
- -
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
index 35eb1dd..94a25f7 100644
--- a/.idea/vcs.xml
+++ b/.idea/vcs.xml
@@ -1,6 +1,6 @@
-
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index ce6df6f..df0729f 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -1,7 +1,10 @@
-
+
+
+
+
@@ -12,75 +15,11 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- JAVA
- com.zipcodeconway.ConwayGameOfLife
-
- com.zipcodeconway.ConwayGameOfLife
-
-
-
-
-
-
- Constructors
- Methods
-
- All
- private
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
@@ -88,24 +27,6 @@
-
-
-
-
-
- end
-
-
- currentGeneration
- nextGeneration
-
-
@@ -114,24 +35,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -139,11 +42,11 @@
DEFINITION_ORDER
-
-
-
-
+
+
+
+
@@ -164,89 +67,59 @@
-
+
-
+
-
+
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
-
+
+
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -260,7 +133,7 @@
-
+
@@ -287,20 +160,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -315,81 +174,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -410,27 +194,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -471,21 +234,9 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
@@ -497,62 +248,33 @@
-
- 1519668901598
+
+ 1520893230066
- 1519668901598
-
+ 1520893230066
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
-
+
-
+
-
+
@@ -578,114 +300,14 @@
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- JAVA
- com.zipcodeconway.ConwayGameOfLife
-
- com.zipcodeconway.ConwayGameOfLife
-
-
-
-
-
-
- Constructors
- Methods
-
- All
- private
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
@@ -693,66 +315,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- No facets are configured
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 1.8
-
-
-
-
-
-
-
-
-
-
-
- Game-Of-Life-Java
-
-
-
-
-
-
-
-
-
-
1.8
@@ -765,17 +327,6 @@
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/Game-Of-Life-Java.iml b/Game-Of-Life-Java.iml
similarity index 91%
rename from .idea/Game-Of-Life-Java.iml
rename to Game-Of-Life-Java.iml
index 9c26698..9f1c55c 100644
--- a/.idea/Game-Of-Life-Java.iml
+++ b/Game-Of-Life-Java.iml
@@ -5,7 +5,6 @@
-
diff --git a/src/main/Screen Shot 2018-03-12 at 6.20.56 PM.png b/src/main/Screen Shot 2018-03-12 at 6.20.56 PM.png
new file mode 100644
index 0000000..361a847
Binary files /dev/null and b/src/main/Screen Shot 2018-03-12 at 6.20.56 PM.png differ
diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
index 0d3b15b..b0750fb 100644
--- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
+++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
@@ -1,11 +1,24 @@
package com.zipcodeconway;
+import java.util.Arrays;
+
public class ConwayGameOfLife {
+ private int[][] currentGen;
+ private int[][] nextGen;
+ private SimpleWindow displayWindow;
+
public ConwayGameOfLife(Integer dimension) {
- }
+ this.displayWindow = new SimpleWindow(dimension);
+ this.currentGen = createRandomStart(dimension);
+ this.nextGen = new int[dimension][dimension];
+ }
public ConwayGameOfLife(Integer dimension, int[][] startmatrix) {
+ this.displayWindow = new SimpleWindow(dimension);
+ currentGen = startmatrix;
+ nextGen = new int[dimension][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 i = 0; i < randomStart.length; i++) {
+ for (int j = 0; j < randomStart.length; j++) {
+ randomStart[i][j] = (int) Math.round(Math.random());
+ }
+ }
+ return randomStart;
}
public int[][] simulate(Integer maxGenerations) {
- return new int[1][1];
+ int count = 0;
+ //count has to be less than OR equal to maxGen because it has to be equal to the number of times it has to run
+ while (count <= maxGenerations) {
+ displayWindow.display(currentGen, count);
+ //update nextGen from currentGen
+ for (int row = 0; row < currentGen.length; row++) {
+ for (int column = 0; column < currentGen[row].length; column++) {
+ nextGen[row][column] = isAlive(row, column, currentGen);
+ }
+ }
+ copyAndZeroOut(nextGen, currentGen);
+ displayWindow.sleep(500);
+ count++;
+ }
+ 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) {
+ public void copyAndZeroOut(int[][] next, int[][] current) {
+ for (int i = 0; i < next.length; i++) {
+ for (int j = 0; j < next.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 +78,34 @@ 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 numOfLiveNeighbors = numberOfLivingNeighbors(row,col,world);
+ if (numOfLiveNeighbors < 2 || numOfLiveNeighbors > 3) {
+ return 0;
+ } else if (numOfLiveNeighbors == 3)
+ return 1;
+ else {
+ return world[row][col];
+ }
}
+
+ public int numberOfLivingNeighbors(int row, int col, int[][] world) {
+ int north = col - 1;
+ int south = col + 1;
+ int west = row - 1;
+ int east = row + 1;
+
+ if (north == -1)
+ north = world[row].length - 1;
+ if (south == world[row].length)
+ south = 0;
+ if (west == -1)
+ west = world.length - 1;
+ if (east == world.length)
+ east = 0;
+
+ return world[row][north] + world[east][north] + world[west][north] +
+ world[row][south] + world[east][south] + world[west][south]
+ + world[east][col] + world[west][col];
+ }
+
}
diff --git a/src/main/java/com/zipcodeconway/SimpleWindow.java b/src/main/java/com/zipcodeconway/SimpleWindow.java
index f315e00..0c9a3e6 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.RED);
g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
if (array[i][j] == 1) {
- g.setColor(Color.BLACK);
+ g.setColor(Color.BLUE);
g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
}