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 6.19.12 PM.png b/Screen Shot 2018-03-12 at 6.19.12 PM.png
new file mode 100644
index 0000000..c3d9871
Binary files /dev/null and b/Screen Shot 2018-03-12 at 6.19.12 PM.png differ
diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
index 0d3b15b..adda1d0 100644
--- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
+++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
@@ -1,11 +1,22 @@
package com.zipcodeconway;
+import sun.java2d.pipe.SpanShapeRenderer;
+
public class ConwayGameOfLife {
+ private SimpleWindow displayWindow;
+ private int[][] currentGen;
+ private int[][] nextGen;
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);
+ this.currentGen = startmatrix;
+ this.nextGen = new int[dimension][dimension];
}
public static void main(String[] args) {
@@ -17,16 +28,40 @@ 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[][] newArray = new int[dimension][dimension];
+ for (int row = 0; row < newArray.length; row++) {
+ for (int col = 0; col < newArray.length; col++) {
+ newArray[row][col] = (int) Math.round(Math.random());
+ }
+ }
+ return newArray;
}
public int[][] simulate(Integer maxGenerations) {
- return new int[1][1];
+ int generations = 0;
+ while (generations <= maxGenerations) {
+ this.displayWindow.display(currentGen, generations);
+ for (int row = 0; row < currentGen.length; row++) { //upating currentGen to nextGeneration
+ for (int col = 0; col < currentGen.length; col++) {
+ nextGen[row][col] = isAlive(row, col, currentGen);
+ }
+ }
+ copyAndZeroOut(nextGen, currentGen);
+ this.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 < next.length; row++) {
+ for (int col = 0; col < current.length; col++) {
+ current[row][col] = next[row][col];
+ next[row][col] = 0;
+ }
+ }
}
// Calculate if an individual cell should be alive in the next generation.
@@ -38,6 +73,50 @@ 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 east = row + 1;
+ int south = col + 1;
+ int west = row - 1;
+ int countNeighborsAlive = 0;
+
+ //these will wrap around the world in order to handle edge cases
+ if (north < 0) {
+ north = world.length - 1;
+ }
+ if (west < 0) {
+ west = world.length - 1;
+ }
+ if (south == world.length) {
+ south = 0;
+ }
+ if (east == world.length) {
+ east = 0;
+ }
+
+ //checking how many neighbors are alive
+
+ if (world[east][north] == 1) countNeighborsAlive++;
+ if (world[east][south] == 1) countNeighborsAlive++;
+ if (world[east][col] == 1) countNeighborsAlive++;
+
+ if (world[west][north] == 1) countNeighborsAlive++;
+ if (world[west][south] == 1) countNeighborsAlive++;
+ if (world[west][col] == 1) countNeighborsAlive++;
+
+ if (world[row][north] == 1) countNeighborsAlive++;
+ if (world[row][south] == 1) countNeighborsAlive++;
+
+ if (countNeighborsAlive < 2 || countNeighborsAlive > 3) {
+ return 0;
+ } else if (countNeighborsAlive == 3) {
+ return 1;
+ } else
+ return world[row][col];
}
}
+ /*
+ Any live cell with fewer than two live neighbours dies, as if by needs caused by underpopulation.
+ Any live cell with more than three live neighbours dies, as if by overcrowding.
+ 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.
+ */
\ No newline at end of file
diff --git a/src/main/java/com/zipcodeconway/SimpleWindow.java b/src/main/java/com/zipcodeconway/SimpleWindow.java
index f315e00..eec9ec8 100644
--- a/src/main/java/com/zipcodeconway/SimpleWindow.java
+++ b/src/main/java/com/zipcodeconway/SimpleWindow.java
@@ -39,15 +39,14 @@ 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.GREEN);
g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
}
}
-
}
-}
+}
\ No newline at end of file