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..72564a0 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,24 +64,11 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
+
@@ -126,9 +114,9 @@
-
+
@@ -138,7 +126,7 @@
true
DEFINITION_ORDER
-
+
@@ -159,68 +147,72 @@
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
+
+
+
+
-
-
-
+
@@ -260,7 +252,7 @@
-
+
@@ -481,8 +473,8 @@
-
-
+
+
@@ -503,6 +495,7 @@
1519668901598
+
@@ -525,6 +518,9 @@
+
+
+
@@ -536,39 +532,38 @@
-
+
-
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
+
-
-
+
+
@@ -584,27 +579,21 @@
-
-
-
-
-
-
-
-
-
+
-
-
+
+
-
+
-
-
-
+
+
+
+
+
@@ -614,12 +603,14 @@
JAVA
com.zipcodeconway.ConwayGameOfLife
- com.zipcodeconway.ConwayGameOfLife
+ com.zipcodeconway.ConwayGameOfLife
-
-
+
+
+ com.zipcodeconway.ConwayGameOfLife
+
Constructors
Methods
@@ -629,63 +620,73 @@
-
-
-
-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
+
+
+
+
+
+
+
-
-
+
+
-
+
-
-
-
-
-
-
-
-
-
+
+
+
-
+
+
+
+ JAVA
+ com.zipcodeconway.ConwayGameOfLife
+
+ com.zipcodeconway.ConwayGameOfLife
+
+
+
+
+
+ com.zipcodeconway.ConwayGameOfLife
+
+
+ Constructors
+ Methods
+
+ All
+ private
+
+
+
+
-
-
+
+
diff --git a/Screen Shot 2018-03-12 at 6.15.30 PM.png b/Screen Shot 2018-03-12 at 6.15.30 PM.png
new file mode 100644
index 0000000..2566dbf
Binary files /dev/null and b/Screen Shot 2018-03-12 at 6.15.30 PM.png differ
diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
index 0d3b15b..83acb26 100644
--- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
+++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
@@ -1,11 +1,21 @@
package com.zipcodeconway;
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 +27,43 @@ 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++){
+ //this is taking our rows and cols and generating a random int into them
+ newArray[row][col]= (int)Math.round(Math.random());
+ }
+ }
+ //its returning the whole array not just one cell
+ 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++){
+ for(int col = 0; col < currentGen.length; col++) {
+ nextGen[row][col]=isAlive(row, col, currentGen);
+ }
+ }
+ copyAndZeroOut(nextGen, currentGen);
+ this.displayWindow.sleep(125);
+ //killing the while loop
+ 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 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 +75,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 south = col + 1;
+ int east = row + 1;
+ int west = row -1;
+
+ int countNeighbor = 0;
+
+
+ //these are the boundaries that loop around
+ if(north<0)
+ north = world.length -1;
+ if(west<0)
+ west = world.length-1;
+ //katrice, draw it out on some paper...this spot stands for the top position in the col
+ if(south==world.length)
+ south = 0;
+ if(east==world.length)
+ east = 0;
+
+ //checking how many neighbors are alive
+
+ if(world[east][north]==1) countNeighbor++;
+ if(world[east][south]==1) countNeighbor++;
+ if(world[east][col]==1) countNeighbor++;
+
+ if(world[west][north]==1) countNeighbor++;
+ if(world[west][south]==1) countNeighbor++;
+ if(world[west][col]==1) countNeighbor++;
+
+ if(world[row][north]==1) countNeighbor++;
+ if(world[row][south]==1) countNeighbor++;
+
+ /*
+ 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.
+ */
+ if(countNeighbor<2 || countNeighbor > 3) {
+ return 0;
+ }else if(countNeighbor == 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..1defa45 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.GREEN);
g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
}