final commit

This commit is contained in:
Justus Görgens 2021-12-22 16:47:34 +01:00
parent 12e086ed53
commit d6beaa7277
8 changed files with 431 additions and 54 deletions

View File

@ -7,6 +7,7 @@ public interface ILife {
// Methoden zum Abfragen der aktuellen Situation
public boolean isAlive(int x, int y);
public boolean isDead(int x, int y);
// Methoden zum Fortschreiben der Generationen
public ILife nextGeneration();

View File

@ -25,7 +25,6 @@ public class Life implements ILife {
for (int x = 0; x < setup[y].length(); x++)
if (setup[y].charAt(x) != ' ')
setAlive(x, y);
printWelt();
}
public void printWelt() {
@ -66,6 +65,16 @@ public class Life implements ILife {
return false;
}
@Override
public boolean isDead(int x, int y) {
// TODO Auto-generated method stub
if (welt[y].charAt(x) == ' ')
return true;
else
return false;
}
@Override
public ILife nextGeneration() {
// TODO Auto-generated method stub
@ -88,11 +97,11 @@ public class Life implements ILife {
//TODO fehler wenn x = 0
if (nachbarn == 3) {
if (x > 0 && x < welt[0].length() - 1) {
neueWelt[y] = neueWelt[y].substring(0, x - 1) + '*' + neueWelt[y].substring(x);
neueWelt[y] = neueWelt[y].substring(0, x) + '*' + neueWelt[y].substring(x + 1);
} else if (x == 0) {
neueWelt[y] = '*' + neueWelt[y].substring(x);
neueWelt[y] = '*' + neueWelt[y].substring(x + 1);
} else {
neueWelt[y] = neueWelt[y].substring(0, x - 1) + '*';
neueWelt[y] = neueWelt[y].substring(0, x) + '*';
}
}
@ -100,15 +109,11 @@ public class Life implements ILife {
//TODO fehler wenn x = 0
if (nachbarn < 2 || nachbarn > 3) {
if (x > 0 && x < welt[0].length() - 1) {
System.out.println(welt[0].length());
System.out.print(nachbarn);
System.out.print(", " + x);
System.out.println(", " + y);
neueWelt[y] = neueWelt[y].substring(0, x - 1) + ' ' + neueWelt[y].substring(x);
neueWelt[y] = neueWelt[y].substring(0, x) + ' ' + neueWelt[y].substring(x + 1);
} else if (x == 0) {
neueWelt[y] = ' ' + neueWelt[y].substring(x);
neueWelt[y] = ' ' + neueWelt[y].substring(x + 1);
} else {
neueWelt[y] = neueWelt[y].substring(0, x - 1) + ' ';
neueWelt[y] = neueWelt[y].substring(0, x) + ' ';
}
}
@ -116,11 +121,11 @@ public class Life implements ILife {
//TODO fehler wenn x = 0
if ((nachbarn == 2) && (welt[y].charAt(x) == '*')) {
if (x > 0 && x < welt[0].length() - 1) {
neueWelt[y] = neueWelt[y].substring(0, x - 1) + '*' + neueWelt[y].substring(x);
neueWelt[y] = neueWelt[y].substring(0, x) + '*' + neueWelt[y].substring(x + 1);
} else if (x == 0) {
neueWelt[y] = '*' + neueWelt[y].substring(x);
neueWelt[y] = '*' + neueWelt[y].substring(x + 1);
} else {
neueWelt[y] = neueWelt[y].substring(0, x - 1) + '*';
neueWelt[y] = neueWelt[y].substring(0, x) + '*';
}
}
}

View File

@ -1,40 +0,0 @@
import org.junit.Test;
import static org.junit.Assert.*;
public class LifeTest {
@Test
public void createNewCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.printWelt();
l.setAlive(0, 0);
l.setAlive(0, 1);
l.setAlive(0, 2);
l.printWelt();
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
assertTrue(nextGen.isAlive(0, 1));
}
@Test
public void destroyLonelyCell() {
}
@Test
public void keepAliveCell() {
}
@Test
public void destroyCrowdedCell() {
}
}

View File

@ -0,0 +1,82 @@
import org.junit.Test;
import static org.junit.Assert.*;
public class LifeTestLinkerRand {
@Test
public void createNewCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(0, 0);
l.setAlive(0, 1);
l.setAlive(0, 2);
l.printWelt();
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
nextGen.printWelt();
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
assertTrue(nextGen.isAlive(0, 1));
}
@Test
public void destroyLonelyCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(0, 1);
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
nextGen.printWelt();
// Assert: Rasterpunkt ohne Nachbarn sollte jetzt tot sein
assertTrue(nextGen.isDead(0, 1));
}
@Test
public void keepAliveCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(0, 0);
l.setAlive(0, 1);
l.setAlive(0, 2);
l.printWelt();
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
nextGen.printWelt();
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
assertTrue(nextGen.isAlive(0, 1));
}
@Test
public void destroyCrowdedCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(0, 0);
l.setAlive(1, 0);
l.setAlive(0, 1);
l.setAlive(0, 2);
l.setAlive(1, 1);
l.printWelt();
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
nextGen.printWelt();
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
assertTrue(nextGen.isDead(0, 1));
assertTrue(nextGen.isDead(1, 1));
}
}

83
test/LifeTestMitte.java Normal file
View File

@ -0,0 +1,83 @@
import org.junit.Test;
import static org.junit.Assert.*;
public class LifeTestMitte {
@Test
public void createNewCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(2, 0);
l.setAlive(2, 1);
l.setAlive(2, 2);
l.printWelt();
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
nextGen.printWelt();
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
assertTrue(nextGen.isAlive(1, 1));
assertTrue(nextGen.isAlive(2, 1));
assertTrue(nextGen.isAlive(3, 1));
}
@Test
public void destroyLonelyCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(2, 1);
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
nextGen.printWelt();
// Assert: Rasterpunkt ohne Nachbarn sollte jetzt tot sein
assertTrue(nextGen.isDead(2, 1));
}
@Test
public void keepAliveCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(2, 0);
l.setAlive(2, 1);
l.setAlive(2, 2);
l.printWelt();
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
nextGen.printWelt();
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
assertTrue(nextGen.isAlive(2, 1));
}
@Test
public void destroyCrowdedCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(2, 0);
l.setAlive(1, 0);
l.setAlive(2, 1);
l.setAlive(2, 2);
l.setAlive(1, 1);
l.printWelt();
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
nextGen.printWelt();
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
assertTrue(nextGen.isDead(2, 1));
assertTrue(nextGen.isDead(1, 1));
}
}

View File

@ -0,0 +1,82 @@
import org.junit.Test;
import static org.junit.Assert.*;
public class LifeTestObererRand {
@Test
public void createNewCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(0, 0);
l.setAlive(1, 0);
l.setAlive(2, 0);
l.printWelt();
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
nextGen.printWelt();
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
assertTrue(nextGen.isAlive(1, 0));
}
@Test
public void destroyLonelyCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(1, 0);
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
nextGen.printWelt();
// Assert: Rasterpunkt ohne Nachbarn sollte jetzt tot sein
assertTrue(nextGen.isDead(1, 0));
}
@Test
public void keepAliveCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(0, 0);
l.setAlive(1, 0);
l.setAlive(2, 0);
l.printWelt();
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
nextGen.printWelt();
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
assertTrue(nextGen.isAlive(1, 0));
}
@Test
public void destroyCrowdedCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(0, 0);
l.setAlive(0, 1);
l.setAlive(1, 0);
l.setAlive(2, 0);
l.setAlive(1, 1);
l.printWelt();
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
nextGen.printWelt();
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
assertTrue(nextGen.isDead(1, 0));
assertTrue(nextGen.isDead(1, 1));
}
}

View File

@ -0,0 +1,82 @@
import org.junit.Test;
import static org.junit.Assert.*;
public class LifeTestRechterRand {
@Test
public void createNewCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(4, 0);
l.setAlive(4, 1);
l.setAlive(4, 2);
l.printWelt();
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
nextGen.printWelt();
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
assertTrue(nextGen.isAlive(4, 1));
}
@Test
public void destroyLonelyCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(4, 1);
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
nextGen.printWelt();
// Assert: Rasterpunkt ohne Nachbarn sollte jetzt tot sein
assertTrue(nextGen.isDead(4, 1));
}
@Test
public void keepAliveCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(4, 0);
l.setAlive(4, 1);
l.setAlive(4, 2);
l.printWelt();
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
nextGen.printWelt();
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
assertTrue(nextGen.isAlive(4, 1));
}
@Test
public void destroyCrowdedCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(4, 0);
l.setAlive(3, 0);
l.setAlive(4, 1);
l.setAlive(4, 2);
l.setAlive(3, 1);
l.printWelt();
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
nextGen.printWelt();
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
assertTrue(nextGen.isDead(4, 1));
assertTrue(nextGen.isDead(3, 1));
}
}

View File

@ -0,0 +1,82 @@
import org.junit.Test;
import static org.junit.Assert.*;
public class LifeTestUntererRand {
@Test
public void createNewCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(0, 4);
l.setAlive(1, 4);
l.setAlive(2, 4);
l.printWelt();
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
nextGen.printWelt();
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
assertTrue(nextGen.isAlive(1, 4));
}
@Test
public void destroyLonelyCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(1, 4);
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
nextGen.printWelt();
// Assert: Rasterpunkt ohne Nachbarn sollte jetzt tot sein
assertTrue(nextGen.isDead(1, 4));
}
@Test
public void keepAliveCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(0, 4);
l.setAlive(1, 4);
l.setAlive(2, 4);
l.printWelt();
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
nextGen.printWelt();
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
assertTrue(nextGen.isAlive(1, 4));
}
@Test
public void destroyCrowdedCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(0, 4);
l.setAlive(0, 3);
l.setAlive(1, 4);
l.setAlive(2, 4);
l.setAlive(1, 3);
l.printWelt();
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
nextGen.printWelt();
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
assertTrue(nextGen.isDead(1, 4));
assertTrue(nextGen.isDead(1, 3));
}
}