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..d0f55ed 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -1,7 +1,12 @@
-
+
+
+
+
+
+
@@ -11,12 +16,12 @@
-
+
-
-
+
+
@@ -24,18 +29,12 @@
-
+
-
-
-
-
-
-
-
-
-
+
+
+
@@ -47,12 +46,14 @@
JAVA
com.zipcodeconway.ConwayGameOfLife
- com.zipcodeconway.ConwayGameOfLife
+ com.zipcodeconway.ConwayGameOfLife
-
-
+
+
+ com.zipcodeconway.ConwayGameOfLife
+
Constructors
Methods
@@ -63,12 +64,22 @@
+
+
+
+
+
+
+
+
+
+
-
+
-
-
+
+
@@ -76,16 +87,6 @@
-
-
-
-
-
-
-
-
-
-
@@ -126,8 +127,8 @@
-
+
@@ -138,7 +139,7 @@
true
DEFINITION_ORDER
-
+
@@ -159,55 +160,57 @@
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
@@ -218,16 +221,14 @@
-
-
-
+
@@ -260,7 +261,7 @@
-
+
@@ -481,8 +482,8 @@
-
-
+
+
@@ -503,13 +504,11 @@
1519668901598
+
-
-
-
@@ -525,6 +524,12 @@
+
+
+
+
+
+
@@ -536,39 +541,39 @@
-
+
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
+
-
-
+
+
@@ -584,42 +589,83 @@
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+ JAVA
+ com.zipcodeconway.ConwayGameOfLife
+
+ com.zipcodeconway.ConwayGameOfLife
+
+
+
+
+
+ com.zipcodeconway.ConwayGameOfLife
+
+
+ Constructors
+ Methods
+
+ All
+ private
+
+
+
+
+
+
+
-
+
-
-
+
+
-
+
-
-
+
+
+
+
+
+
+
+
JAVA
com.zipcodeconway.ConwayGameOfLife
- com.zipcodeconway.ConwayGameOfLife
+ com.zipcodeconway.ConwayGameOfLife
-
-
+
+
+ com.zipcodeconway.ConwayGameOfLife
+
Constructors
Methods
@@ -629,39 +675,29 @@
-
+
+
+
+
+
+
+
+
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
@@ -670,22 +706,8 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
diff --git a/Screen Shot 2018-03-12 at 10.26.42 PM.png b/Screen Shot 2018-03-12 at 10.26.42 PM.png
new file mode 100644
index 0000000..dd0fd56
Binary files /dev/null and b/Screen Shot 2018-03-12 at 10.26.42 PM.png differ
diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
index 0d3b15b..8557388 100644
--- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
+++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
@@ -2,33 +2,73 @@
public class ConwayGameOfLife {
+ private int[][] currentGen;
+ private int[][] nextGen;
+ private SimpleWindow displayWindow;
+
public ConwayGameOfLife(Integer dimension) {
- }
+
+ 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) {
+
ConwayGameOfLife sim = new ConwayGameOfLife(50);
int[][] endingWorld = sim.simulate(50);
}
- // 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];
+ int[][] newRandomArray = new int[dimension][dimension];
+ for (int row = 0; row < newRandomArray.length; row++) {
+ for (int column = 0; column < newRandomArray[row].length; column++) {
+ newRandomArray[row][column] = (int) (Math.random() * 2);
+ }
+ }
+ return newRandomArray;
}
public int[][] simulate(Integer maxGenerations) {
- return new int[1][1];
+ int generations = 0;
+ while (generations <= maxGenerations) {
+ displayWindow.display(currentGen, generations);
+ 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(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) {
+ public void copyAndZeroOut(int[][] next, int[][] current) {
+
+ for (int row = 0; row < current.length; row++)
+ for (int column = 0; column < current[row].length; column++) {
+ current[row][column] = next[row][column];
+ next[row][column] = 0;
+ }
}
+
// Calculate if an individual cell should be alive in the next generation.
// Based on the game logic:
/*
@@ -38,6 +78,52 @@ 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 north = col - 1;
+ int south = col + 1;
+ int east = row + 1;
+ int west = row - 1;
+ int countNeighborAlive = 0;
+
+
+ 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;
+ }
+
+
+ //check how many neighbors are alive
+ if (world[east][north] == 1) countNeighborAlive++;
+ if (world[east][south] == 1) countNeighborAlive++;
+ if (world[east][col] == 1) countNeighborAlive++;
+
+ if (world[west][north] == 1) countNeighborAlive++;
+ if (world[west][south] == 1) countNeighborAlive++;
+ if (world[west][col] == 1) countNeighborAlive++;
+
+ if (world[row][north] == 1) countNeighborAlive++;
+ if (world[row][south] == 1) countNeighborAlive++;
+
+
+
+ if (countNeighborAlive < 2 || countNeighborAlive > 3) {
+ return 0;
+ } else if (countNeighborAlive == 3) {
+ return 1;
+ } else
+ return world[row][col];
}
}
+
+
+
+
+
diff --git a/src/main/java/com/zipcodeconway/SimpleWindow.java b/src/main/java/com/zipcodeconway/SimpleWindow.java
index f315e00..2d04f16 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.gray);
g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
if (array[i][j] == 1) {
- g.setColor(Color.BLACK);
+ g.setColor(Color.pink);
g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
}