-
-
- 1.8
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 8.09.54 PM.png b/Screen Shot 2018-03-12 at 8.09.54 PM.png
new file mode 100644
index 0000000..3ab2ef0
Binary files /dev/null and b/Screen Shot 2018-03-12 at 8.09.54 PM.png differ
diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
index 0d3b15b..cc72b08 100644
--- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
+++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java
@@ -1,11 +1,22 @@
package com.zipcodeconway;
+
public class ConwayGameOfLife {
+ private int[][] next;
+ private int[][] currentGen;
+ private SimpleWindow displayWindow;
+
public ConwayGameOfLife(Integer dimension) {
+ next = new int[dimension][dimension];
+ currentGen = createRandomStart(dimension);
+ this.displayWindow = new SimpleWindow(dimension);
}
public ConwayGameOfLife(Integer dimension, int[][] startmatrix) {
+ currentGen = startmatrix;
+ next = new int [dimension][dimension];
+ this.displayWindow = new SimpleWindow(dimension);
}
public static void main(String[] args) {
@@ -17,16 +28,44 @@ 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];
+ //creating random world[][]
+ int[][] newWorld = new int[dimension][dimension];
+ for(int row = 0; row < newWorld.length; row++){
+ for(int col = 0; col < newWorld[row].length; col++){
+ newWorld[row][col] = (int)(Math.random() * 2);
+ }
+ }
+
+ return newWorld;
}
public int[][] simulate(Integer maxGenerations) {
- return new int[1][1];
+ //updating the next from the current generation
+ //setting alive neighbors from isAlive method into next generation
+ int counter = 0;
+ while(counter <= maxGenerations){
+ displayWindow.display(currentGen, counter);
+ for(int row = 0; row < currentGen.length; row++){
+ for(int col = 0; col < currentGen[row].length; col++){
+ next[row][col] = isAlive(row, col, currentGen);
+ }
+ }
+ copyAndZeroOut(next, currentGen);
+ displayWindow.sleep(125);
+ counter++;
+ }
+ 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 +77,40 @@ 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;
+ //declaring directions to move and check for neighbors
+ int north = col - 1;
+ int south = col + 1;
+ int east= row + 1;
+ int west = row - 1;
+ int countNeighbor = 0;
+
+ //boundaries that loop move [][]
+ 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 in each direction
+ if(world[east][north] == 1) countNeighbor++;
+ if(world[east][col] == 1) countNeighbor++;
+ if(world[east][south] == 1) countNeighbor++;
+
+ if(world[west][north] == 1) countNeighbor++;
+ if(world[west][col] == 1) countNeighbor++;
+ if(world[west][south] == 1) countNeighbor++;
+
+ if(world[row][north] == 1) countNeighbor++;
+ if(world[row][south] == 1) countNeighbor++;
+
+ //determining whether the neighbor is alive or dead and returning that world
+ if(countNeighbor < 2 || countNeighbor > 3) return 0;
+ else if(countNeighbor == 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/Screenshot b/src/main/java/com/zipcodeconway/Screenshot
new file mode 100644
index 0000000..d1af8dc
--- /dev/null
+++ b/src/main/java/com/zipcodeconway/Screenshot
@@ -0,0 +1 @@
+Screen Shot 2018-03-12 at 8.09.54 PM
\ 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..ccb36fc 100644
--- a/src/main/java/com/zipcodeconway/SimpleWindow.java
+++ b/src/main/java/com/zipcodeconway/SimpleWindow.java
@@ -43,7 +43,7 @@ public void display(int[][] array, Integer n) {
g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
if (array[i][j] == 1) {
- g.setColor(Color.BLACK);
+ g.setColor(Color.BLUE);
g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
}