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..6448443 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -1,7 +1,18 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
@@ -11,100 +22,48 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- JAVA
- com.zipcodeconway.ConwayGameOfLife
-
- com.zipcodeconway.ConwayGameOfLife
-
-
-
-
-
-
- Constructors
- Methods
-
- All
- private
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- end
-
-
- currentGeneration
- nextGeneration
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -117,17 +76,8 @@
-
-
-
-
-
-
-
-
-
@@ -138,11 +88,14 @@
true
DEFINITION_ORDER
-
-
-
-
-
+
+
+
+
+
+
+
+
@@ -164,29 +117,29 @@
-
+
-
+
-
+
-
+
-
+
@@ -194,20 +147,20 @@
-
+
-
+
-
+
@@ -217,36 +170,17 @@
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -260,7 +194,7 @@
-
+
@@ -287,20 +221,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -340,7 +260,7 @@
-
+
@@ -352,7 +272,32 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -410,27 +355,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -471,21 +395,20 @@
-
-
-
-
-
-
-
+
+
+
+
+
+
-
-
+
+
-
-
+
+
@@ -497,71 +420,74 @@
-
- 1519668901598
+
+ 1520856104111
- 1519668901598
-
+ 1520856104111
+
-
+
-
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
+
+
+
-
-
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
@@ -578,90 +504,22 @@
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- JAVA
- com.zipcodeconway.ConwayGameOfLife
-
- com.zipcodeconway.ConwayGameOfLife
-
-
-
-
-
-
- Constructors
- Methods
-
- All
- private
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
+
-
-
+
+
@@ -670,112 +528,11 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- No facets are configured
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 1.8
-
-
-
-
-
-
-
-
-
-
-
- Game-Of-Life-Java
-
-
-
-
-
-
-
-
-
-
-
-
- 1.8
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ 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/Screen Shot 2018-03-12 at 3.11.09 PM.png b/Screen Shot 2018-03-12 at 3.11.09 PM.png
new file mode 100644
index 0000000..a195177
Binary files /dev/null and b/Screen Shot 2018-03-12 at 3.11.09 PM.png differ
diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
index 0d3b15b..103b8ec 100644
--- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
+++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
@@ -2,10 +2,24 @@
public class ConwayGameOfLife {
+ int edge;
+ int[][] currentGen;
+ int[][] nextGen;
+ SimpleWindow displayWindow;
+
+
public ConwayGameOfLife(Integer dimension) {
- }
+ edge = dimension - 1;
+ nextGen = new int[dimension][dimension];
+ displayWindow = new SimpleWindow(dimension);
+ currentGen = createRandomStart(dimension);
+ }
public ConwayGameOfLife(Integer dimension, int[][] startmatrix) {
+ edge = dimension - 1;
+ currentGen = startmatrix;
+ nextGen = new int[dimension][dimension];
+ displayWindow = new SimpleWindow(dimension);
}
public static void main(String[] args) {
@@ -17,16 +31,52 @@ 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];
+
+ currentGen = new int[dimension][dimension];
+
+ for (int i = 0; i < dimension; i++) {
+ for (int j = 0; j < dimension; j++) {
+ if ((Math.random() * 100) > 28) {
+ currentGen[i][j] = 0;
+ } else {
+ currentGen[i][j] = 1;
+ }
+ }
+ }
+
+ return currentGen;
}
public int[][] simulate(Integer maxGenerations) {
- return new int[1][1];
+
+ int generations = 0;
+
+ while (generations <= maxGenerations) {
+ this.displayWindow.display(currentGen, generations);
+ this.displayWindow.sleep(75);
+ 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++) {
+ next[i][j] = isAlive(i, j, 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.
@@ -37,7 +87,46 @@ public void copyAndZeroOut(int [][] next, int[][] current) {
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;
+ protected int isAlive(int row, int col, int[][] world) {
+
+ int iLooker;
+ int jLooker;
+ int count = 0;
+
+ for (int i = row-1; i < row+2; i++) {
+
+ if (i > edge) {
+ iLooker = 0;
+ } else if (i < 0) {
+ iLooker = edge;
+ } else {
+ iLooker = i;
+ }
+
+ for (int j = col-1; j < col+2; j++) {
+
+ if (j > edge) {
+ jLooker = 0;
+ } else if (j < 0) {
+ jLooker = edge;
+ } else {
+ jLooker = j;
+ }
+
+ if (world[iLooker][jLooker] == 1) {
+ count++;
+ }
+ }
+ }
+
+ if (count == 3) {
+ return 1;
+ } else if (count <3 || count > 4) {
+ return 0;
+ }
+
+ return world[row][col];
}
+
+
}
diff --git a/src/main/java/com/zipcodeconway/SimpleWindow.java b/src/main/java/com/zipcodeconway/SimpleWindow.java
index f315e00..51e78e6 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.BLUE);
g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
if (array[i][j] == 1) {
- g.setColor(Color.BLACK);
+ g.setColor(Color.WHITE);
g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
}
diff --git a/src/test/java/com/zipcodeconway/ConwayGameOfLifeTest.java b/src/test/java/com/zipcodeconway/ConwayGameOfLifeTest.java
index 1e5e845..3d5ec72 100644
--- a/src/test/java/com/zipcodeconway/ConwayGameOfLifeTest.java
+++ b/src/test/java/com/zipcodeconway/ConwayGameOfLifeTest.java
@@ -1,5 +1,6 @@
package com.zipcodeconway;
+import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -43,4 +44,53 @@ public void runTest2() {
int[][] results = sim.simulate(10);
assertTrue(java.util.Arrays.deepEquals(results, expected));
}
+
+ @Test
+ public void isAliveTest1() {
+ ConwayGameOfLife sim = new ConwayGameOfLife(5);
+ int row = 2;
+ int col = 2;
+ int[][] world = {
+ {0, 0, 0, 0, 0},
+ {0, 0, 0, 0, 0},
+ {0, 1, 1, 1, 0},
+ {0, 0, 0, 0, 0},
+ {0, 0, 0, 0, 0}};
+ int expected = 1;
+ int actual = sim.isAlive(row, col, world);
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void isAliveTest2() {
+ ConwayGameOfLife sim = new ConwayGameOfLife(5);
+ int row = 1;
+ int col = 4;
+ int[][] world = {
+ {1, 0, 0, 1, 0},
+ {1, 1, 1, 0, 0},
+ {0, 1, 1, 1, 0},
+ {1, 1, 0, 1, 1},
+ {1, 0, 0, 1, 1}};
+ int expected = 0;
+ int actual = sim.isAlive(row, col, world);
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void isAliveTest3() {
+ ConwayGameOfLife sim = new ConwayGameOfLife(5);
+ int row = 4;
+ int col = 2;
+ int[][] world = {
+ {1, 0, 0, 1, 0},
+ {1, 1, 1, 0, 0},
+ {0, 1, 1, 1, 0},
+ {1, 0, 0, 1, 1},
+ {1, 0, 0, 1, 1}};
+ int expected = 1;
+ int actual = sim.isAlive(row, col, world);
+ Assert.assertEquals(expected, actual);
+ }
+
}
\ No newline at end of file