From f11a993212c8052e884ba4afa3539d19e939f21c Mon Sep 17 00:00:00 2001 From: felixdennerlein Date: Mon, 27 Dec 2021 22:39:03 +0100 Subject: [PATCH] Final Version of Life --- .idea/libraries/jars.xml | 10 +++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 +++ .idea/runConfigurations.xml | 10 +++ .idea/vcs.xml | 6 ++ GameOfLifeAssignment.iml | 13 ++++ src/Life.java | 123 ++++++++++++++++++++++++++++++++---- test/LifeTest.java | 38 +++++++++-- 8 files changed, 196 insertions(+), 18 deletions(-) create mode 100644 .idea/libraries/jars.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/runConfigurations.xml create mode 100644 .idea/vcs.xml create mode 100644 GameOfLifeAssignment.iml diff --git a/.idea/libraries/jars.xml b/.idea/libraries/jars.xml new file mode 100644 index 0000000..555bc6d --- /dev/null +++ b/.idea/libraries/jars.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0319d5d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..c4b8036 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/GameOfLifeAssignment.iml b/GameOfLifeAssignment.iml new file mode 100644 index 0000000..506243b --- /dev/null +++ b/GameOfLifeAssignment.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Life.java b/src/Life.java index 84ab855..ad87ac2 100644 --- a/src/Life.java +++ b/src/Life.java @@ -1,14 +1,21 @@ -public class Life implements ILife { - - public static void main(String[] args) { - Life l = new Life(new String[] { " ", - " ", - " *** ", - " ", - " " }); - l = (Life) l.nextGeneration(); - } +import java.util.Arrays; +public class Life implements ILife { + + + Life nextworld; + String[] board; + + + public static void main(String[] args) { + Life l = new Life(new String[]{" ", + " ", + " *** ", + " ", + " "}); + l = (Life) l.nextGeneration(); + + } public Life() { nukeAll(); @@ -26,30 +33,118 @@ public class Life implements ILife { @Override public void nukeAll() { // TODO Auto-generated method stub - + board = new String[]{" ", + " ", + " ", + " ", + " "}; } @Override public void setAlive(int x, int y) { // TODO Auto-generated method stub + String line = ""; + for (int i = 0; i < board[y].length(); i++) { + if (i == x) + line += '*'; + else + line += board[y].charAt(i); + } + board[y] = line; + } @Override public void setDead(int x, int y) { // TODO Auto-generated method stub + String line = ""; + for (int i = 0; i < board[y].length(); i++) { + if (i == x) + line += ' '; + else + line += board[y].charAt(i); + } + board[y] = line; + } @Override public boolean isAlive(int x, int y) { // TODO Auto-generated method stub - return false; + if (board[y].charAt(x) == '*') + return true; + else + return false; } @Override public ILife nextGeneration() { // TODO Auto-generated method stub - return null; + Life nextWorld = new Life(); + for (int y = 0; y < board.length; y++) { + for (int x = 0; x < board[y].length(); x++) { + int neighbourCells = CountNeighbourCells(x, y); + if (isAlive(x, y)) + if (neighbourCells == 3 || neighbourCells == 2) + nextWorld.setAlive(x, y); + if (neighbourCells < 2 || neighbourCells > 3) + nextWorld.setDead(x, y); + if (!isAlive(x, y)) + if (neighbourCells == 3) + nextWorld.setAlive(x, y); + else + nextWorld.setDead(x, y); + } + } + nextWorld.drawWorld(); + return nextWorld; } -} \ No newline at end of file + + public int CountNeighbourCells(int x, int y) { + int pre_y = y - 1; + int pre_x = x - 1; + int after_y = y + 1; + int after_x = x + 1; + int counter = 0; + //UpperSide + if (pre_y >= 0) { + for (int i = pre_x; i <= after_x; i++) { + if (i >= 0 && i < board[pre_y].length()&& isAlive(i,pre_y)){ + counter++; + } + } + } + //DownSide + if (after_y < board.length) { + for (int i = pre_x; i <= after_x; i++) { + if (i >= 0 && i < board[after_y].length()&&isAlive(i,after_y)) { + counter++; + } + } + } + //leftSide + if (pre_x >= 0&& isAlive(pre_x,y)) { + counter++; + } + + //rightSide + if (after_x < board[y].length()&&isAlive(after_x,y)) { + counter++; + } + return counter; + } + + + public void drawWorld() { + for (int i = 0; i < board.length; i++) { + System.out.println(Arrays.toString(new String[]{ + board[i] + })); + + } + } +} + + diff --git a/test/LifeTest.java b/test/LifeTest.java index 3940e66..e9d0c63 100644 --- a/test/LifeTest.java +++ b/test/LifeTest.java @@ -22,17 +22,47 @@ public class LifeTest { @Test public void destroyLonelyCell() { - } + Life l = new Life(); + l.setAlive(0, 0); + l.setAlive(0, 1); + l.setAlive(0, 2); + // Act: Berechnung der Folgegeneration + ILife nextGen = l.nextGeneration(); + + // Assert: Rasterpunkt mit einem Nachbar sollte jetzt sterben + assertFalse(nextGen.isAlive(0, 2)); + } @Test public void keepAliveCell() { - } + // Arrange: drei lebende Zellen + Life l = new Life(); + l.setAlive(0, 0); + l.setAlive(0, 1); + l.setAlive(0, 2); + // Act: Berechnung der Folgegeneration + ILife nextGen = l.nextGeneration(); + + // Assert: Rasterpunkt mit zwei Nachbarn sollte jetzt leben + assertTrue(nextGen.isAlive(0, 1)); + } @Test public void destroyCrowdedCell() { + // Arrange: fünf lebende Zellen + Life l = new Life(); + l.setAlive(0, 0); + l.setAlive(0, 1); + l.setAlive(0, 2); + l.setAlive(1, 1); + l.setAlive(1, 0); + + // Act: Berechnung der Folgegeneration + ILife nextGen = l.nextGeneration(); + + // Assert: Rasterpunkt mit vier Nachbarn sollte jetzt sterben + assertFalse(nextGen.isAlive(0, 1)); } - - }