From 28464e054eb2e6400dee4937d4ab07dcbe597c91 Mon Sep 17 00:00:00 2001 From: "Meilin Li (Linda)" Date: Sun, 10 Jul 2022 23:33:07 -0400 Subject: [PATCH 01/11] test2 --- .idea/Game-Of-Life-Java.iml | 4 +- .idea/compiler.xml | 2 +- .idea/jarRepositories.xml | 20 +++ .idea/libraries/Maven__junit_junit_4_13.xml | 13 -- .idea/libraries/Maven__junit_junit_4_13_2.xml | 13 ++ .idea/workspace.xml | 160 +++++------------- pom.xml | 4 + .../com/zipcodeconway/ConwayGameOfLife.java | 11 ++ 8 files changed, 94 insertions(+), 133 deletions(-) create mode 100644 .idea/jarRepositories.xml delete mode 100644 .idea/libraries/Maven__junit_junit_4_13.xml create mode 100644 .idea/libraries/Maven__junit_junit_4_13_2.xml 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..d789aab 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,93 +1,24 @@ + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - JAVA - com.zipcodeconway.ConwayGameOfLife - - com.zipcodeconway.ConwayGameOfLife - - - - - - - Constructors - Methods - - All - private - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + @@ -223,19 +158,17 @@ - - - - - - - - - - - - + + + { + "keyToString": { + "SHARE_PROJECT_CONFIGURATION_FILES": "true", + "WebServerToolWindowFactoryState": "false", + "nodejs_package_manager_path": "npm" + } +} @@ -250,19 +183,7 @@ - - - - + + @@ -424,6 +346,8 @@ @@ -493,16 +417,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..dcc3061 100644 --- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java +++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java @@ -2,10 +2,17 @@ public class ConwayGameOfLife { + private SimpleWindow displayWindow; + private Integer dimension; + public ConwayGameOfLife(Integer dimension) { + this.dimension = dimension; + this.displayWindow = new SimpleWindow(dimension); + } public ConwayGameOfLife(Integer dimension, int[][] startmatrix) { + } public static void main(String[] args) { @@ -38,6 +45,10 @@ 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) { + int length = world.length; + int width = world[0].length; + int aliveCount = 0; + for (int i = 0; i < length; ) return 0; } } From da41c6a9c10ccfdeac3d2a3ab982c4b9efd884c9 Mon Sep 17 00:00:00 2001 From: "Meilin Li (Linda)" Date: Mon, 11 Jul 2022 00:40:40 -0400 Subject: [PATCH 02/11] randomboard method ok --- .idea/workspace.xml | 8 +---- .../com/zipcodeconway/ConwayGameOfLife.java | 32 ++++++++++++++----- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d789aab..18ba04f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,13 +5,7 @@ - - - - - - diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java index dcc3061..c8f470f 100644 --- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java +++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java @@ -1,21 +1,28 @@ package com.zipcodeconway; +import java.util.Random; + public class ConwayGameOfLife { private SimpleWindow displayWindow; private Integer dimension; + private int[][] next; + private int[][] current; public ConwayGameOfLife(Integer dimension) { this.dimension = dimension; this.displayWindow = new SimpleWindow(dimension); - + this.current = createRandomStart(dimension); + this.next = new int[dimension][dimension]; } public ConwayGameOfLife(Integer dimension, int[][] startmatrix) { - + this.dimension = dimension; + this.displayWindow = new SimpleWindow(dimension); } public static void main(String[] args) { + SimpleWindow displayWindow = new SimpleWindow(50); ConwayGameOfLife sim = new ConwayGameOfLife(50); int[][] endingWorld = sim.simulate(50); } @@ -24,16 +31,23 @@ 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]; + Random rand = new Random(); + int[][] randomBoard = new int[dimension][dimension]; + for (int i = 0; i < dimension; i++) + for (int j = 0; j < dimension; j++) { + randomBoard[i][j] = rand.nextInt(0,2); + } + return randomBoard; } public int[][] simulate(Integer maxGenerations) { + return new int[1][1]; } // 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) { } // Calculate if an individual cell should be alive in the next generation. @@ -45,10 +59,12 @@ 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) { - int length = world.length; - int width = world[0].length; - int aliveCount = 0; - for (int i = 0; i < length; ) + for (int i = 0; i < row; i++) { + for (int j = 0; j < col; j++) { + int liveCount = 0; + + } + } return 0; } } From 0511f6a40acce72ca12869060c2619cd80a25bf6 Mon Sep 17 00:00:00 2001 From: "Meilin Li (Linda)" Date: Mon, 11 Jul 2022 19:18:48 -0400 Subject: [PATCH 03/11] randomStart method done --- .idea/workspace.xml | 36 ++++++++++++------- .../com/zipcodeconway/ConwayGameOfLife.java | 3 +- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 18ba04f..f94b72e 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -177,7 +177,7 @@ - + - - + + + + + + + @@ -311,8 +320,8 @@ - + @@ -320,11 +329,11 @@ - + + - @@ -341,7 +350,8 @@ 1519668901598 - + + diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java index c8f470f..868ce49 100644 --- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java +++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java @@ -19,10 +19,11 @@ public ConwayGameOfLife(Integer dimension) { public ConwayGameOfLife(Integer dimension, int[][] startmatrix) { this.dimension = dimension; this.displayWindow = new SimpleWindow(dimension); + this.current = startmatrix; + this.next = new int[dimension][dimension]; } public static void main(String[] args) { - SimpleWindow displayWindow = new SimpleWindow(50); ConwayGameOfLife sim = new ConwayGameOfLife(50); int[][] endingWorld = sim.simulate(50); } From 80200dc1648fb3163a245fb2a4240a83462cf425 Mon Sep 17 00:00:00 2001 From: "Meilin Li (Linda)" Date: Mon, 11 Jul 2022 20:30:13 -0400 Subject: [PATCH 04/11] testing... --- .idea/workspace.xml | 1 + .../com/zipcodeconway/ConwayGameOfLife.java | 23 +++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f94b72e..cb7d8c3 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -352,6 +352,7 @@ + diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java index 868ce49..966dc5a 100644 --- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java +++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java @@ -43,6 +43,7 @@ private int[][] createRandomStart(Integer dimension) { public int[][] simulate(Integer maxGenerations) { + return new int[1][1]; } @@ -60,12 +61,24 @@ 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) { - for (int i = 0; i < row; i++) { - for (int j = 0; j < col; j++) { - int liveCount = 0; - + int count = 0; + for (int i = row - 1; i < row + 2; i++) { + for (int j = col - 1; j < col + 2; j++) { + if (!(i == row && j == col) && isLive(row, col, world)) { + count++; + } } } - return 0; + return count; + } + + private boolean isLive(int row, int col, int[][] world) { + if (row < 0 || col < 0 || row >= world.length || col >= world[0].length) { + return false; + } + if (world[row][col] >= 1) { + return true; + } + return false; } } From b185d8108c0b59ba9c3ad15fa4853cb0b6e67dac Mon Sep 17 00:00:00 2001 From: "Meilin Li (Linda)" Date: Mon, 11 Jul 2022 20:36:14 -0400 Subject: [PATCH 05/11] copyandzeroout --- .idea/workspace.xml | 3 +-- src/main/java/com/zipcodeconway/ConwayGameOfLife.java | 8 ++++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index cb7d8c3..123ebb1 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,7 +5,6 @@ - diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java index 966dc5a..e6164b0 100644 --- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java +++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java @@ -42,14 +42,18 @@ private int[][] createRandomStart(Integer dimension) { } public int[][] simulate(Integer maxGenerations) { - - return new int[1][1]; } // 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 < 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. From 38a875bede99d7ac62aa4fcc9f634f9daea693b1 Mon Sep 17 00:00:00 2001 From: "Meilin Li (Linda)" Date: Mon, 11 Jul 2022 20:50:23 -0400 Subject: [PATCH 06/11] tests failed --- .idea/workspace.xml | 3 ++- src/main/java/com/zipcodeconway/ConwayGameOfLife.java | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 123ebb1..c60ed93 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,6 +5,7 @@ + diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java index e6164b0..02c7c42 100644 --- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java +++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java @@ -42,6 +42,11 @@ private int[][] createRandomStart(Integer dimension) { } public int[][] simulate(Integer maxGenerations) { + for (int row = 0; row < this.dimension; row++) { + for (int col = 0; row < this.dimension; col++) { + next[row][col] = isAlive(row, col, current); + } + } return new int[1][1]; } From 50cc2272c78c17459d4064709fb2d051eba92c36 Mon Sep 17 00:00:00 2001 From: "Meilin Li (Linda)" Date: Mon, 11 Jul 2022 20:52:57 -0400 Subject: [PATCH 07/11] ??? --- src/main/java/com/zipcodeconway/ConwayGameOfLife.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java index 02c7c42..78cc430 100644 --- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java +++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java @@ -47,7 +47,7 @@ public int[][] simulate(Integer maxGenerations) { next[row][col] = isAlive(row, col, current); } } - return new int[1][1]; + return current; } // copy the values of 'next' matrix to 'current' matrix, From 20dccd39b4634ae697fc64c985a07b2bb3c511f7 Mon Sep 17 00:00:00 2001 From: "Meilin Li (Linda)" Date: Mon, 11 Jul 2022 23:04:54 -0400 Subject: [PATCH 08/11] trying to figure out isAlive method --- .idea/workspace.xml | 2 +- .../com/zipcodeconway/ConwayGameOfLife.java | 62 ++++++++++++------- 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index c60ed93..07dffbc 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -352,7 +352,7 @@ - + diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java index 78cc430..50f97cf 100644 --- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java +++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java @@ -14,7 +14,7 @@ public ConwayGameOfLife(Integer dimension) { this.displayWindow = new SimpleWindow(dimension); this.current = createRandomStart(dimension); this.next = new int[dimension][dimension]; - } + } public ConwayGameOfLife(Integer dimension, int[][] startmatrix) { this.dimension = dimension; @@ -36,18 +36,21 @@ private int[][] createRandomStart(Integer dimension) { int[][] randomBoard = new int[dimension][dimension]; for (int i = 0; i < dimension; i++) for (int j = 0; j < dimension; j++) { - randomBoard[i][j] = rand.nextInt(0,2); + randomBoard[i][j] = rand.nextInt(0, 2); } return randomBoard; } public int[][] simulate(Integer maxGenerations) { - for (int row = 0; row < this.dimension; row++) { - for (int col = 0; row < this.dimension; col++) { - next[row][col] = isAlive(row, col, current); + for (int i = 0; i <= maxGenerations; i++) { + for (int row = 0; row < this.dimension; row++) { + for (int col = 0; row < dimension; col++) { + this.next[row][col] = isAlive(row, col, this.current); + } } + copyAndZeroOut(this.next, this.current); } - return current; + return this.current; } // copy the values of 'next' matrix to 'current' matrix, @@ -70,24 +73,41 @@ 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) { - int count = 0; - for (int i = row - 1; i < row + 2; i++) { - for (int j = col - 1; j < col + 2; j++) { - if (!(i == row && j == col) && isLive(row, col, world)) { - count++; - } + int liveCount = 0; + + // row - 1, col - 1 = top right + // row - 1, col = top + // row - 1, col + 1 = top right + // row , col - 1 = left + // row , col + 1 = right + // row + 1, col - 1 = bottom left + // row + 1, col = bottom + // row + 1, col + 1 = bottom right + + if (row - 1 >= 0) { + if (world[row - 1][col] == 1) liveCount++; + if (col - 1 >= 0) { + if (world[row - 1][col - 1] == 1) liveCount++; + } + if (col + 1 < this.dimension) { + if (world[row - 1][col + 1] == 1) liveCount++; } } - return count; - } - - private boolean isLive(int row, int col, int[][] world) { - if (row < 0 || col < 0 || row >= world.length || col >= world[0].length) { - return false; + if (row + 1 < this.dimension) { + if (world[row + 1][col] == 1) liveCount++; + if (col - 1 >= 0) { + if (world[row + 1][col - 1] == 1) liveCount++; + } + if (col + 1 < this.dimension) { + if (world[row + 1][col + 1] == 1) liveCount++; + } + } + if (col - 1 >= 0) { + if (world[row][col - 1] == 1) liveCount++; } - if (world[row][col] >= 1) { - return true; + if (col + 1 < this.dimension) { + if (world[row][col + 1] == 1) liveCount++; } - return false; + return liveCount; } } From cf0e635460d1988ccb1b5f8b67af6e567b49e2f3 Mon Sep 17 00:00:00 2001 From: "Meilin Li (Linda)" Date: Mon, 11 Jul 2022 23:16:12 -0400 Subject: [PATCH 09/11] window added to simulate --- .idea/workspace.xml | 2 +- src/main/java/com/zipcodeconway/ConwayGameOfLife.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 07dffbc..7c33120 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -352,7 +352,7 @@ - + diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java index 50f97cf..3fdcd83 100644 --- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java +++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java @@ -43,6 +43,7 @@ private int[][] createRandomStart(Integer dimension) { public int[][] simulate(Integer maxGenerations) { for (int i = 0; i <= maxGenerations; i++) { + this.displayWindow.display(this.current, i); for (int row = 0; row < this.dimension; row++) { for (int col = 0; row < dimension; col++) { this.next[row][col] = isAlive(row, col, this.current); From 30a9d1bb01b8e46398c60a3cb9b77c1285960c6f Mon Sep 17 00:00:00 2001 From: "Meilin Li (Linda)" Date: Sun, 17 Jul 2022 20:10:50 -0400 Subject: [PATCH 10/11] redoing logic --- .idea/workspace.xml | 5 +- .../com/zipcodeconway/ConwayGameOfLife.java | 51 +++++++------------ 2 files changed, 23 insertions(+), 33 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 7c33120..f561850 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -352,7 +352,10 @@ - + + + + diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java index 3fdcd83..cd6e941 100644 --- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java +++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java @@ -73,42 +73,29 @@ public void copyAndZeroOut(int[][] next, int[][] current) { 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. */ + private int isAlive(int row, int col, int[][] world) { int liveCount = 0; + int status = world[row][col]; - // row - 1, col - 1 = top right - // row - 1, col = top - // row - 1, col + 1 = top right - // row , col - 1 = left - // row , col + 1 = right - // row + 1, col - 1 = bottom left - // row + 1, col = bottom - // row + 1, col + 1 = bottom right - - if (row - 1 >= 0) { - if (world[row - 1][col] == 1) liveCount++; - if (col - 1 >= 0) { - if (world[row - 1][col - 1] == 1) liveCount++; - } - if (col + 1 < this.dimension) { - if (world[row - 1][col + 1] == 1) liveCount++; - } - } - if (row + 1 < this.dimension) { - if (world[row + 1][col] == 1) liveCount++; - if (col - 1 >= 0) { - if (world[row + 1][col - 1] == 1) liveCount++; - } - if (col + 1 < this.dimension) { - if (world[row + 1][col + 1] == 1) liveCount++; + 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 (col - 1 >= 0) { - if (world[row][col - 1] == 1) liveCount++; - } - if (col + 1 < this.dimension) { - if (world[row][col + 1] == 1) liveCount++; - } - return liveCount; + + if (liveCount < 2 || liveCount > 3) status = 0; + else if (status == 1 && liveCount == 2) status = 1; + else if (liveCount == 3) status = 1; + + return status; } } From e11a9d3980bf8110694e7b476b3b892b086a4770 Mon Sep 17 00:00:00 2001 From: "Meilin Li (Linda)" Date: Sun, 17 Jul 2022 21:28:19 -0400 Subject: [PATCH 11/11] passed --- .idea/workspace.xml | 1 + .../com/zipcodeconway/ConwayGameOfLife.java | 59 ++++++++++--------- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f561850..e88138d 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -356,6 +356,7 @@ + diff --git a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java index cd6e941..8c4e79d 100644 --- a/src/main/java/com/zipcodeconway/ConwayGameOfLife.java +++ b/src/main/java/com/zipcodeconway/ConwayGameOfLife.java @@ -3,24 +3,24 @@ import java.util.Random; public class ConwayGameOfLife { - - private SimpleWindow displayWindow; - private Integer dimension; - private int[][] next; private int[][] current; + private int[][] next; + private int dimension; + private SimpleWindow window; + public ConwayGameOfLife(Integer dimension) { this.dimension = dimension; - this.displayWindow = new SimpleWindow(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.displayWindow = new SimpleWindow(dimension); this.current = startmatrix; this.next = new int[dimension][dimension]; + this.window = new SimpleWindow(dimension); } public static void main(String[] args) { @@ -32,24 +32,26 @@ 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) { - Random rand = new Random(); int[][] randomBoard = new int[dimension][dimension]; - for (int i = 0; i < dimension; i++) - for (int j = 0; j < dimension; j++) { - randomBoard[i][j] = rand.nextInt(0, 2); + 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) { - for (int i = 0; i <= maxGenerations; i++) { - this.displayWindow.display(this.current, i); - for (int row = 0; row < this.dimension; row++) { - for (int col = 0; row < dimension; col++) { + 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; } @@ -57,8 +59,9 @@ public int[][] simulate(Integer maxGenerations) { // 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 < this.dimension; row++) { - for (int col = 0; col < this.dimension; col++) { + + 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; } @@ -73,29 +76,29 @@ public void copyAndZeroOut(int[][] next, int[][] current) { 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. */ - private int isAlive(int row, int col, int[][] world) { 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) { + + 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; + 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; + 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