diff --git a/.idea/libraries/jars.xml b/.idea/libraries/jars.xml new file mode 100644 index 0000000..a29960d --- /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..1029788 --- /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..790f130 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ 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/GameOfLifeAssignmentNew.iml b/GameOfLifeAssignmentNew.iml new file mode 100644 index 0000000..506243b --- /dev/null +++ b/GameOfLifeAssignmentNew.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Life.java b/src/Life.java index 84ab855..1008e88 100644 --- a/src/Life.java +++ b/src/Life.java @@ -1,17 +1,22 @@ +//Bearbeitet von: David Papac +import java.util.Arrays; public class Life implements ILife { - - public static void main(String[] args) { - Life l = new Life(new String[] { " ", - " ", - " *** ", - " ", - " " }); - l = (Life) l.nextGeneration(); - } + String[] gameWorld; + + public static void main(String[] args) { + Life l = new Life( new String[] { " ", + " ", + " *** ", + " ", + " "}); + l = (Life) l.nextGeneration(); + + } public Life() { nukeAll(); + } public Life(String[] setup) { @@ -20,36 +25,124 @@ public class Life implements ILife { for (int x = 0; x < setup[y].length(); x++) if (setup[y].charAt(x) != ' ') setAlive(x, y); + } @Override public void nukeAll() { // TODO Auto-generated method stub + gameWorld = new String[]{" ", + " ", + " ", + " ", + " "}; } @Override public void setAlive(int x, int y) { // TODO Auto-generated method stub + String newString =""; + for(int i=0; i< gameWorld[y].length(); i++){ + if(i==x) + newString +='*'; + else + newString += gameWorld[y].charAt(i); + } + gameWorld[y]=newString; } @Override public void setDead(int x, int y) { // TODO Auto-generated method stub + String newString =""; + for(int i=0; i< gameWorld[y].length(); i++){ + if(i==x) + newString +=' '; + else + newString += gameWorld[y].charAt(i); + } + gameWorld[y]=newString; } @Override public boolean isAlive(int x, int y) { // TODO Auto-generated method stub - return false; - } + if(gameWorld[y].charAt(x) =='*'){ + return true;} + else{ + return false; + }} @Override public ILife nextGeneration() { // TODO Auto-generated method stub - return null; + Life newWorld = new Life(); + for(int y=0; y< gameWorld.length;y++){ + for(int x= 0; x 3) { + newWorld.setDead(x, y); + } + } + if(!isAlive(x,y)) { + if (neighbor == 3) { + newWorld.setAlive(x, y); + } else { + newWorld.setDead(x, y); + } + } + } + } + newWorld.showBoard(); + return newWorld; + } + + public int countNeighbor(int x, int y) { + int counter=0; + //counts top row + if(y-1 >=0){ + for(int i =x-1; i<= x+1;i++){ + if(i>=0 && i< gameWorld[y-1].length()){ + if(isAlive(i,y-1)){ + counter++; + } + } + } + } + //counts bottom row + if(y+1 =0 && i< gameWorld[y+1].length()){ + if(isAlive(i,y+1)){ + counter++; + } + } + } + } + //counts sides + if(x-1>=0){ + if(isAlive(x-1,y)){ + counter++; + } + } + if( x+1< gameWorld[y].length()){ + if(isAlive(x+1,y)){ + counter++; + } + } + return counter; + } + + public void showBoard() { + for (int i = 0; i < gameWorld.length; i++) + System.out.println(Arrays.toString(new String[]{gameWorld[i]})); } } \ No newline at end of file diff --git a/test/LifeTest.java b/test/LifeTest.java index 3940e66..6a95c08 100644 --- a/test/LifeTest.java +++ b/test/LifeTest.java @@ -1,11 +1,26 @@ +//Bearbeitet von: David Papac import org.junit.Test; import static org.junit.Assert.*; public class LifeTest { - + + @Test + public void nuke() { + // Arrange: drei lebende Zellen + Life l = new Life(); + l.setAlive(0, 0); + l.setAlive(0, 1); + l.setAlive(0, 2); + + // Act: Alles wird ausgelöscht + l.nukeAll(); + + // Assert: Alle Rasterpunkt sterben + assertFalse(l.isAlive(0, 1)); + } @Test public void createNewCell() { - + // Arrange: drei lebende Zellen Life l = new Life(); l.setAlive(0, 0); @@ -22,17 +37,52 @@ public class LifeTest { @Test public void destroyLonelyCell() { + // 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 weniger als zwei Nachbarn soll 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 mindestens zwei aber weniger als vier Nachbarn soll 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 mehr als drei Nachbarn soll sterben + assertFalse(nextGen.isAlive(0, 1)); } -} +} \ No newline at end of file