diff --git a/.DS_Store b/.DS_Store
new file mode 100644
index 0000000..38cd806
Binary files /dev/null and b/.DS_Store differ
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..2208471 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -1,7 +1,11 @@
-
+
+
+
+
+
@@ -11,100 +15,46 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- JAVA
- com.zipcodeconway.ConwayGameOfLife
-
- com.zipcodeconway.ConwayGameOfLife
-
-
-
-
-
-
- Constructors
- Methods
-
- All
- private
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- end
-
-
- currentGeneration
- nextGeneration
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -117,17 +67,8 @@
-
-
-
-
-
-
-
-
-
@@ -138,12 +79,11 @@
true
DEFINITION_ORDER
-
-
-
-
-
+
+
+
+
@@ -159,94 +99,93 @@
+
+
+
+
-
+
-
+
-
+
-
+
-
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
-
+
+
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -260,7 +199,7 @@
-
+
@@ -287,9 +226,9 @@
-
+
-
+
@@ -298,24 +237,35 @@
-
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
-
+
+
+
+
-
+
@@ -327,8 +277,8 @@
-
-
+
+
@@ -340,7 +290,7 @@
-
+
@@ -352,7 +302,7 @@
-
+
@@ -410,27 +360,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -471,21 +400,20 @@
-
-
-
-
-
-
-
+
+
+
+
+
+
-
-
+
+
-
-
+
+
@@ -497,78 +425,85 @@
-
- 1519668901598
+
+ 1520694948576
- 1519668901598
-
+ 1520694948576
+
+
+
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
+
+
+
+
-
+
-
+
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
-
-
+
+
@@ -579,203 +514,114 @@
-
+
-
+
-
-
+
+
-
+
-
-
+
+
-
+
-
-
+
+
-
-
-
- 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 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.19.57 PM.png b/Screen Shot 2018-03-12 at 3.19.57 PM.png
new file mode 100644
index 0000000..da1b705
Binary files /dev/null and b/Screen Shot 2018-03-12 at 3.19.57 PM.png differ
diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
index 0d3b15b..a8928be 100644
--- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
+++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
@@ -1,11 +1,27 @@
package com.zipcodeconway;
+import java.util.Random;
+
public class ConwayGameOfLife {
+ private SimpleWindow displayWindow;
+ private Integer edge;
+ int[][] currentGen;
+ int[][] nextGen;
+
public ConwayGameOfLife(Integer dimension) {
+ this.displayWindow = new SimpleWindow(dimension);
+ this.edge = dimension -1;
+ this.nextGen = new int [dimension][dimension];
+ currentGen = createRandomStart(dimension);
}
public ConwayGameOfLife(Integer dimension, int[][] startmatrix) {
+ this.displayWindow = new SimpleWindow(dimension);
+ this.edge = dimension - 1;
+ this.currentGen = startmatrix;
+ this.nextGen = new int [dimension][dimension];
+
}
public static void main(String[] args) {
@@ -17,16 +33,47 @@ 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 > 50){
+ currentGen[i][j] = 1;
+ } else {
+ currentGen[i][j] = 0;
+ }
+ }
+ }
+
+ return currentGen;
}
public int[][] simulate(Integer maxGenerations) {
- return new int[1][1];
- }
+ for(int i = 0; i <= maxGenerations; i++){
+ this.displayWindow.display(currentGen, i);
+ this.displayWindow.sleep(125);
+ copyAndZeroOut(nextGen, currentGen);
+ }
+
+ 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 +84,39 @@ 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) {
+ //is alive in next generation//
+
+ int nextGenLives;
+ int counter = 0;
+
+ for(int i = row - 1; i <= row + 1; i++) {
+ int iSeeker = i;
+ if(i < 0){
+ iSeeker = edge;
+ } else if(i > edge){
+ iSeeker = 0;
+ }
+
+ for(int j = col - 1; j <= col + 1; j++){
+ int jSeeker = j;
+ if(j < 0){
+ jSeeker = edge;
+ } else if(j > edge){
+ jSeeker = 0;
+ }
+ if(world[iSeeker][jSeeker] == 1) counter++;
+ }
+ }
+
+ if (counter == 3){
+ nextGenLives = 1;
+ } else if (counter == 4){
+ nextGenLives = world[row][col];
+ } else {
+ nextGenLives = 0;
+ }
+
+ return nextGenLives;
}
}
diff --git a/src/main/java/com/zipcodeconway/SimpleWindow.java b/src/main/java/com/zipcodeconway/SimpleWindow.java
index f315e00..7521a5b 100644
--- a/src/main/java/com/zipcodeconway/SimpleWindow.java
+++ b/src/main/java/com/zipcodeconway/SimpleWindow.java
@@ -37,14 +37,14 @@ public void display(int[][] array, Integer n) {
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 * BOX_DIM + 1, j * BOX_DIM + 1, 10, 10);
if (array[i][j] == 0) {
- g.setColor(Color.WHITE);
- g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
+ g.setColor(Color.BLACK);
+ g.fillRect(i * BOX_DIM, j * BOX_DIM + 1, 10, 10);
}
if (array[i][j] == 1) {
- g.setColor(Color.BLACK);
- g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
+ g.setColor(Color.ORANGE);
+ g.fillRect(i * BOX_DIM, j * BOX_DIM + 1, 10, 10);
}
}
}
diff --git a/src/test/java/com/zipcodeconway/ConwayGameOfLifeTest.java b/src/test/java/com/zipcodeconway/ConwayGameOfLifeTest.java
index 1e5e845..c4d589f 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,48 @@ public void runTest2() {
int[][] results = sim.simulate(10);
assertTrue(java.util.Arrays.deepEquals(results, expected));
}
+
+ @Test
+ public void isAliveTest(){
+ ConwayGameOfLife sim = new ConwayGameOfLife(5);
+
+ //Given
+ int row = 2;
+ int col = 2;
+ int [][] world = {
+ {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}};
+ //When
+ // 1 means alive
+ int expected = 1;
+ int actual = sim.isAlive(row, col, world);
+
+ //Then
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void isAliveEdgeTest(){
+ ConwayGameOfLife sim = new ConwayGameOfLife(5);
+
+ //Given
+ int row = 0;
+ int col = 2;
+ int [][] world = {
+ {0, 0, 1, 0, 0},
+ {0, 0, 0, 1, 0},
+ {0, 0, 0, 0, 0},
+ {0, 0, 0, 0, 0},
+ {0, 1, 1, 0, 0}};
+ //When
+ // 1 means alive
+ int expected = 1;
+ int actual = sim.isAlive(row, col, world);
+
+ //Then
+ Assert.assertEquals(expected, actual);
+ }
}
\ No newline at end of file