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/compiler.xml b/.idea/compiler.xml
index bcc81dd..2c85a31 100644
--- a/.idea/compiler.xml
+++ b/.idea/compiler.xml
@@ -10,7 +10,7 @@
-
+
\ No newline at end of file
diff --git a/.idea/libraries/Maven__junit_junit_4_12.xml b/.idea/libraries/Maven__junit_junit_4_12.xml
deleted file mode 100644
index d411041..0000000
--- a/.idea/libraries/Maven__junit_junit_4_12.xml
+++ /dev/null
@@ -1,13 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/Maven__junit_junit_4_13_rc_1.xml b/.idea/libraries/Maven__junit_junit_4_13_rc_1.xml
new file mode 100644
index 0000000..4c31d2a
--- /dev/null
+++ b/.idea/libraries/Maven__junit_junit_4_13_rc_1.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 05e1d17..b11383b 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/workspace.xml b/.idea/workspace.xml
index ce6df6f..26a5425 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -1,252 +1,41 @@
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- JAVA
- com.zipcodeconway.ConwayGameOfLife
-
- com.zipcodeconway.ConwayGameOfLife
-
-
-
-
-
-
- Constructors
- Methods
-
- All
- private
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- end
-
-
- currentGeneration
- nextGeneration
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- true
- DEFINITION_ORDER
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
@@ -260,522 +49,119 @@
-
-
-
-
-
-
-
-
+
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
-
+
-
-
-
-
-
-
-
-
-
-
+
+
+
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
-
-
-
-
- 1519668901598
+
+ 1574883421821
- 1519668901598
-
+ 1574883421821
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- JAVA
- com.zipcodeconway.ConwayGameOfLife
-
- com.zipcodeconway.ConwayGameOfLife
-
-
-
-
-
-
- Constructors
- Methods
-
- All
- private
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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 76%
rename from .idea/Game-Of-Life-Java.iml
rename to Game-Of-Life-Java.iml
index 9c26698..37a99f4 100644
--- a/.idea/Game-Of-Life-Java.iml
+++ b/Game-Of-Life-Java.iml
@@ -1,17 +1,16 @@
-
+
-
-
+
\ No newline at end of file
diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
index 0d3b15b..2fbbf20 100644
--- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
+++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
@@ -1,11 +1,29 @@
package com.zipcodeconway;
+import java.util.Arrays;
+import java.util.Random;
+import java.util.stream.IntStream;
+
public class ConwayGameOfLife {
+ private int dimension;
+ private int[][] world;
+ private SimpleWindow displayWindow;
+
+ public ConwayGameOfLife() {
+
+ }
+
public ConwayGameOfLife(Integer dimension) {
+ this.displayWindow = new SimpleWindow(dimension);
+ this.dimension = dimension;
+ this.world = createRandomStart(this.dimension);
}
public ConwayGameOfLife(Integer dimension, int[][] startmatrix) {
+ this.displayWindow = new SimpleWindow(dimension);
+ this.dimension = dimension;
+ this.world = startmatrix;
}
public static void main(String[] args) {
@@ -16,17 +34,39 @@ public static void main(String[] args) {
// 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];
+ public int[][] createRandomStart(Integer dimension) {
+ int[][] array = new int[dimension][dimension];
+ IntStream.range(0,dimension)
+ .forEach(row -> IntStream.range(0,dimension)
+ .forEach(col -> array[col][row] = new Random().nextInt(2)));
+ return array;
}
public int[][] simulate(Integer maxGenerations) {
- return new int[1][1];
+ int[][] current = this.world;
+ int[][] next = new int[this.dimension][this.dimension];
+ for (int gen = 1; gen < maxGenerations; gen++) {
+ this.displayWindow.display(current, gen);
+
+ for (int i = 0; i < this.dimension; i++) {
+ for (int j = 0; j < this.dimension; j++) {
+ next[i][j] = isAlive(i,j,current);
+ }
+ }
+
+ copyAndZeroOut(next, current);
+ this.displayWindow.sleep(125);
+ }
+ return current;
}
// 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[0].length; i++) {
+ current[i] = Arrays.copyOf(next[i],next[i].length);
+ Arrays.fill(next[i],0);
+ }
}
// Calculate if an individual cell should be alive in the next generation.
@@ -37,7 +77,35 @@ 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;
+ public int isAlive(int row, int col, int[][] world) {
+ int dim = world[0].length;
+ int numNeighbors =
+ world[row][(col+1)%dim] + world[row][(dim+col-1)%dim] +
+ world[(row+1)%dim][col] + world[(dim+row-1)%dim][col] +
+ world[(row+1)%dim][(col+1)%dim] + world[(dim+row-1)%dim][(col+1)%dim] +
+ world[(row+1)%dim][(dim+col-1)%dim] + world[(dim+row-1)%dim][(dim+col-1)%dim];
+
+ if (world[row][col] == 1) {
+ if (numNeighbors == 3 || numNeighbors == 2) {
+ return 1;
+ } else {
+ return 0;
+ }
+ } else {
+ if (numNeighbors == 3) {
+ return 1;
+ }
+ }
+ return world[row][col];
+ }
+
+
+ public void printMatrix(int[][] array) {
+ for (int[] row : array) {
+ for (int val : row) {
+ System.out.print(val + " ");
+ }
+ System.out.println("");
+ }
}
}
diff --git a/src/main/java/com/zipcodeconway/SimpleWindow.java b/src/main/java/com/zipcodeconway/SimpleWindow.java
index f315e00..52f5229 100644
--- a/src/main/java/com/zipcodeconway/SimpleWindow.java
+++ b/src/main/java/com/zipcodeconway/SimpleWindow.java
@@ -7,9 +7,10 @@ public class SimpleWindow {
static JPanel panel;
static JFrame frame;
private Integer dim = 0;
+ private Integer boxDim = 15;
public SimpleWindow(Integer dimension) {
- this.dim = dimension * 10;
+ this.dim = dimension * this.boxDim;
panel = new JPanel();
Dimension dim = new Dimension(this.dim, this.dim);
panel.setPreferredSize(dim);
@@ -34,18 +35,15 @@ public void sleep(Integer millisecs) {
public void display(int[][] array, Integer n) {
frame.setTitle(String.format("Generation: %6d", n));
Graphics g = panel.getGraphics();
- int BOX_DIM = 10;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
- g.drawRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
+ g.drawRect(i * this.boxDim, j * this.boxDim, this.boxDim, this.boxDim);
if (array[i][j] == 0) {
g.setColor(Color.WHITE);
- g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
- }
- if (array[i][j] == 1) {
+ } else if (array[i][j] == 1) {
g.setColor(Color.BLACK);
- g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
+ g.fillRect(i * this.boxDim-1, j * this.boxDim, this.boxDim+1, this.boxDim);
}
}
diff --git a/src/test/java/com/zipcodeconway/ConwayGameOfLifeTest.java b/src/test/java/com/zipcodeconway/ConwayGameOfLifeTest.java
index 1e5e845..36fd6a3 100644
--- a/src/test/java/com/zipcodeconway/ConwayGameOfLifeTest.java
+++ b/src/test/java/com/zipcodeconway/ConwayGameOfLifeTest.java
@@ -1,7 +1,10 @@
package com.zipcodeconway;
+import org.junit.Assert;
import org.junit.Test;
+import java.util.Arrays;
+
import static org.junit.Assert.*;
public class ConwayGameOfLifeTest {
@@ -22,6 +25,13 @@ public void runTest1() {
{0, 0, 0, 0, 0}};
ConwayGameOfLife sim = new ConwayGameOfLife(5, start);
int[][] results = sim.simulate(9);
+
+ System.out.println("");
+ sim.printMatrix(results);
+ System.out.println("");
+ sim.printMatrix(expected);
+ System.out.println("");
+
assertTrue(java.util.Arrays.deepEquals(results, expected));
}
@@ -43,4 +53,170 @@ public void runTest2() {
int[][] results = sim.simulate(10);
assertTrue(java.util.Arrays.deepEquals(results, expected));
}
+
+ @Test
+ public void createRandom() {
+ ConwayGameOfLife sim = new ConwayGameOfLife();
+ int[][] array = sim.createRandomStart(20);
+
+ sim.printMatrix(array);
+
+ int count = Arrays.stream(array).flatMapToInt(row -> Arrays.stream(row)).sum();
+ Assert.assertTrue(count <= 400);
+ Arrays.stream(array).flatMapToInt(row -> Arrays.stream(row)).forEach(x-> Assert.assertTrue(x == 0 || x == 1));
+ }
+
+ @Test
+ public void copyAndZeroTest() {
+ ConwayGameOfLife sim = new ConwayGameOfLife();
+ int[][] array = sim.createRandomStart(20);
+
+ int[][] current = sim.createRandomStart(20);
+ int[][] next = new int [20][20];
+ for (int i = 0; i < next[0].length; i++) {
+ next[i] = Arrays.copyOf(array[i],array[i].length);
+ }
+ sim.copyAndZeroOut(next,current);
+
+ sim.printMatrix(array);
+ System.out.println("");
+ sim.printMatrix(current);
+ System.out.println("");
+ sim.printMatrix(next);
+
+ Arrays.stream(next).flatMapToInt(row -> Arrays.stream(row)).forEach(x-> Assert.assertTrue(x == 0));
+ assertTrue(java.util.Arrays.deepEquals(array,current));
+ }
+
+ @Test
+ public void isAliveTest() {
+ ConwayGameOfLife sim = new ConwayGameOfLife();
+ int[][] world = {
+ {0, 1, 0, 0, 0},
+ {0, 0, 0, 0, 0},
+ {0, 1, 1, 1, 0},
+ {1, 1, 0, 1, 1},
+ {0, 0, 0, 1, 0}};
+
+ int[][] results = new int[5][5];
+
+ for (int i = 0; i < 5; i++) {
+ for (int j = 0; j < 5; j++) {
+ results[i][j] = sim.isAlive(i,j,world);
+ }
+ }
+ int[][] expected = {
+ {0, 0, 0, 0, 0},
+ {0, 1, 0, 0, 0},
+ {0, 1, 0, 1, 0},
+ {1, 1, 0, 0, 0},
+ {0, 1, 0, 1, 0}};
+
+ System.out.println("");
+ sim.printMatrix(results);
+ System.out.println("");
+ sim.printMatrix(expected);
+ System.out.println("");
+
+ assertTrue(java.util.Arrays.deepEquals(expected, results));
+ }
+
+ @Test
+ public void isAliveTest2() {
+ ConwayGameOfLife sim = new ConwayGameOfLife();
+ 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[][] results = new int[5][5];
+
+ for (int i = 0; i < 5; i++) {
+ for (int j = 0; j < 5; j++) {
+ results[i][j] = sim.isAlive(i,j,world);
+ }
+ }
+ int[][] expected = {
+ {0, 0, 0, 0, 0},
+ {0, 0, 1, 0, 0},
+ {0, 0, 1, 0, 0},
+ {0, 0, 1, 0, 0},
+ {0, 0, 0, 0, 0}};
+
+ System.out.println("");
+ sim.printMatrix(results);
+ System.out.println("");
+ sim.printMatrix(expected);
+ System.out.println("");
+
+ assertTrue(java.util.Arrays.deepEquals(expected, results));
+ }
+
+ @Test
+ public void isAliveTest3() {
+ ConwayGameOfLife sim = new ConwayGameOfLife();
+ int[][] world = {
+ {0, 1, 1, 1, 0},
+ {0, 0, 0, 0, 0},
+ {0, 0, 0, 0, 0},
+ {0, 0, 0, 0, 0},
+ {0, 0, 0, 0, 0}};
+
+ int[][] results = new int[5][5];
+
+ for (int i = 0; i < 5; i++) {
+ for (int j = 0; j < 5; j++) {
+ results[i][j] = sim.isAlive(i,j,world);
+ }
+ }
+ int[][] expected = {
+ {0, 0, 1, 0, 0},
+ {0, 0, 1, 0, 0},
+ {0, 0, 0, 0, 0},
+ {0, 0, 0, 0, 0},
+ {0, 0, 1, 0, 0}};
+
+ System.out.println("");
+ sim.printMatrix(results);
+ System.out.println("");
+ sim.printMatrix(expected);
+ System.out.println("");
+
+ assertTrue(java.util.Arrays.deepEquals(expected, results));
+ }
+
+ @Test
+ public void isAliveTest4() {
+ ConwayGameOfLife sim = new ConwayGameOfLife();
+ int[][] world = {
+ {0, 0, 0, 0, 0},
+ {0, 1, 1, 0, 0},
+ {0, 1, 1, 0, 0},
+ {0, 1, 1, 0, 0},
+ {0, 0, 0, 0, 0}};
+
+ int[][] results = new int[5][5];
+
+ for (int i = 0; i < 5; i++) {
+ for (int j = 0; j < 5; j++) {
+ results[i][j] = sim.isAlive(i,j,world);
+ }
+ }
+ int[][] expected = {
+ {0, 0, 0, 0, 0},
+ {0, 1, 1, 0, 0},
+ {1, 0, 0, 1, 0},
+ {0, 1, 1, 0, 0},
+ {0, 0, 0, 0, 0}};
+
+ System.out.println("");
+ sim.printMatrix(results);
+ System.out.println("");
+ sim.printMatrix(expected);
+ System.out.println("");
+
+ assertTrue(java.util.Arrays.deepEquals(expected, results));
+ }
}
\ No newline at end of file