Browse Source

Fertige Abgabe mit mehreren Tests

master
juleslepitschka 2 years ago
parent
commit
f4e71a0f16
3 changed files with 123 additions and 20 deletions
  1. 1
    0
      src/ILife.java
  2. 82
    10
      src/Life.java
  3. 40
    10
      test/LifeTest.java

+ 1
- 0
src/ILife.java 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();

+ 82
- 10
src/Life.java View File

@@ -1,23 +1,31 @@
public class Life implements ILife {

private final int x_value = 10;
private final int y_value = 10;

public enum CellValue {Dead, Alive}
private final CellValue[][] cells = new CellValue[x_value][y_value];

public static void main(String[] args) {
Life l = new Life(new String[] { " ",
" ",
" *** ",
" ",
" " });
" " }
);
l.print();
l = (Life) l.nextGeneration();
l.print();
}


public Life() {
nukeAll();
}

public Life(String[] setup) {
this();
for (int y = 0; y < setup.length; y++)
for (int x = 0; x < setup[y].length(); x++)
for (int y = 0; y < y_value; y ++)
for (int x = 0; x < x_value; x ++)
if (setup[y].charAt(x) != ' ')
setAlive(x, y);
}
@@ -26,30 +34,94 @@ public class Life implements ILife {
@Override
public void nukeAll() {
// TODO Auto-generated method stub

for( int y = 0; y < y_value; y ++){
for( int x = 0; x < x_value; x ++){
setDead(x,y);
}
}
}

@Override
public void setAlive(int x, int y) {
// TODO Auto-generated method stub
cells[x][y] = CellValue.Alive;
}

@Override
public void setDead(int x, int y) {
// TODO Auto-generated method stub
cells[x][y] = CellValue.Dead;
}

@Override
public boolean isAlive(int x, int y) {
// TODO Auto-generated method stub
return false;
return cells[x][y] == CellValue.Alive;
}

@Override
public boolean isDead(int x, int y) {
return cells[x][y] == CellValue.Dead;
}

@Override
public ILife nextGeneration() {
// TODO Auto-generated method stub
return null;
Life newGeneration = new Life();
for (int y = 0; y < y_value; y ++) {
for(int x = 0; x < x_value; x ++) {
int neighborsAlive = calculateNeighborsAlive(x,y);
if( neighborsAlive == 3) {
newGeneration.setAlive(x,y);
}
if( neighborsAlive < 2){
newGeneration.setDead(x,y);
}
if( isAlive(x,y) && (neighborsAlive == 2 || neighborsAlive == 3)){
newGeneration.setAlive(x,y);
}
if( isAlive(x,y) && neighborsAlive > 3) {
newGeneration.setDead(x,y);
}
}
}
return newGeneration;
}

public void print() {
for( int x= 0; x < x_value; x ++) {
for( int y = 0; y < y_value; y ++) {
if( cells[x][y] == CellValue.Alive) {
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.print("\n");
}
}

public int calculateNeighborsAlive( int x, int y) {
int min_X = x - 1;
int max_X = x + 1;
int min_Y = y - 1;
int max_Y = y + 1;

if( min_X < 0) min_X = 0;
if( max_X >= x_value) max_X = x_value - 1;
if( min_Y < 0) min_Y = 0;
if( max_Y >= x_value) max_Y = y_value - 1;

int sum = 0;
for( int dx = min_X; dx <= max_X; dx++){
for( int dy = min_Y; dy <= max_Y; dy++){
if( dx != x || dy != y) {
if( cells [dx][dy] == CellValue.Alive ){
sum++;
}
}
}
}
return sum;
}
}

+ 40
- 10
test/LifeTest.java View File

@@ -2,37 +2,67 @@ import org.junit.Test;
import static org.junit.Assert.*;

public class LifeTest {
@Test
public void createNewCell() {
// Arrange: drei lebende Zellen
// Arrange (drei lebende Zellen)
Life l = new Life();
l.setAlive(0, 0);
l.setAlive(0, 1);
l.setAlive(0, 2);

// Act: Berechnung der Folgegeneration
// Act (Berechnung der Folgegeneration)
ILife nextGen = l.nextGeneration();

// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
// Assert (Rasterpunkt mit drei Nachbarn sollte jetzt leben)
assertTrue(nextGen.isAlive(1, 1));
}


@Test
public void destroyLonelyCell() {
}
// Arrange (vier lebende Zellen)
Life firstGen = new Life();
firstGen.setAlive(0, 0);
firstGen.setAlive(0, 1);

// Act (Berechnung der Folgegeneration)
ILife nextGen = firstGen.nextGeneration();

// Assert (Rasterpunkt ohne Nachbarn sollte jetzt sterben)
assertTrue(nextGen.isDead(0, 0));
assertTrue(nextGen.isDead(0, 1));
}

@Test
public void keepAliveCell() {
}
// Arrange (drei lebende Zellen)
Life firstGen = new Life();
firstGen.setAlive(1, 1);
firstGen.setAlive(2, 2);
firstGen.setAlive(3, 3);

// Act (Berechnung der Folgegeneration)
ILife nextGen = firstGen.nextGeneration();

// Assert (Rasterpunkt bleibt am Leben)
assertTrue(nextGen.isAlive(2, 2));
}

@Test
public void destroyCrowdedCell() {
}
// Arrange (drei lebende Zellen)
Life firstGen = new Life();
firstGen.setAlive(0, 0);
firstGen.setAlive(0, 1);
firstGen.setAlive(0, 2);
firstGen.setAlive(1, 0);
firstGen.setAlive(1, 1);

// Act (Berechnung der Folgegeneration)
ILife nextGen = firstGen.nextGeneration();

}
// Assert (Rasterpunkt stirbt wegen Überbevölkerung)
assertTrue(nextGen.isDead(0, 1));
assertTrue(nextGen.isDead(1, 1));
}
}

Loading…
Cancel
Save