diff --git a/.idea/Game-Of-Life-Java.iml b/.idea/Game-Of-Life-Java.iml index 1e06a11..25096a2 100644 --- a/.idea/Game-Of-Life-Java.iml +++ b/.idea/Game-Of-Life-Java.iml @@ -1,6 +1,6 @@ - + @@ -11,7 +11,7 @@ - + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml index bcc81dd..f2cd775 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -10,7 +10,7 @@ - + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__junit_junit_4_13.xml b/.idea/libraries/Maven__junit_junit_4_13.xml deleted file mode 100644 index 59fc5c4..0000000 --- a/.idea/libraries/Maven__junit_junit_4_13.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/libraries/Maven__junit_junit_4_13_2.xml b/.idea/libraries/Maven__junit_junit_4_13_2.xml new file mode 100644 index 0000000..606c352 --- /dev/null +++ b/.idea/libraries/Maven__junit_junit_4_13_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index ad135b2..e88138d 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,93 +1,18 @@ + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - JAVA - com.zipcodeconway.ConwayGameOfLife - - com.zipcodeconway.ConwayGameOfLife - - - - - - - Constructors - Methods - - All - private - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + @@ -223,19 +152,17 @@ - - - - - - - - - - - - + + + { + "keyToString": { + "SHARE_PROJECT_CONFIGURATION_FILES": "true", + "WebServerToolWindowFactoryState": "false", + "nodejs_package_manager_path": "npm" + } +} @@ -250,18 +177,6 @@ - - - - - + + + + + + + @@ -396,8 +320,8 @@ - + @@ -406,13 +330,14 @@ + - + @@ -424,6 +349,14 @@ @@ -493,16 +426,18 @@ - - - - - - - + + diff --git a/pom.xml b/pom.xml index 1bc152b..e73e4f2 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,10 @@ RELEASE + + 18 + 18 + \ No newline at end of file diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java index 0d3b15b..8c4e79d 100644 --- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java +++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java @@ -1,11 +1,26 @@ package com.zipcodeconway; +import java.util.Random; + public class ConwayGameOfLife { + private int[][] current; + private int[][] next; + private int dimension; + private SimpleWindow window; + public ConwayGameOfLife(Integer dimension) { - } + this.dimension = dimension; + this.current = createRandomStart(dimension); + this.next = new int[dimension][dimension]; + this.window = new SimpleWindow(dimension); + } public ConwayGameOfLife(Integer dimension, int[][] startmatrix) { + this.dimension = dimension; + this.current = startmatrix; + this.next = new int[dimension][dimension]; + this.window = new SimpleWindow(dimension); } public static void main(String[] args) { @@ -17,16 +32,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[][] randomBoard = new int[dimension][dimension]; + Random rand = new Random(); + for ( int i = 0; i < dimension; i++ ) { + for ( int j = 0; j < dimension; j++ ) { + randomBoard[i][j] = rand.nextInt(2); + } + } + return randomBoard; } public int[][] simulate(Integer maxGenerations) { - return new int[1][1]; + for ( int i = 0; i <= maxGenerations; i++ ) { + this.window.display(this.current, i); + for ( int row = 0; row < this.dimension; row++ ) { + for ( int col = 0; col < this.dimension; col++ ) { + this.next[row][col] = isAlive(row, col, this.current); + } + } + copyAndZeroOut(this.next, this.current); + this.window.sleep(2000); + } + return this.current; } // 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 < this.dimension; row++ ) { + for ( int col = 0; col < this.dimension; 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,28 @@ 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 liveCount = 0; + int status = world[row][col]; + + + for ( int i = row - 1; i <= row + 1; i++ ) { + for ( int j = col - 1; j <= col + 1; j++ ) { + if ( i != row || j != col ) { + int newRow = i; + int newCol = j; + if ( newRow < 0 ) newRow = dimension - 1; + if ( newRow == dimension ) newRow = 0; + if ( newCol < 0 ) newCol = dimension - 1; + if ( newCol == dimension ) newCol = 0; + liveCount += world[newRow][newCol]; + } + } + } + + if ( liveCount < 2 || liveCount > 3 ) status = 0; + else if ( status == 1 && liveCount == 2 ) status = 1; + else if ( liveCount == 3 ) status = 1; + + return status; } -} +} \ No newline at end of file