Browse Source

Abgabe fertig

master
Sarah Baumann 2 years ago
parent
commit
2236d2bd00
3 changed files with 115 additions and 24 deletions
  1. 2
    1
      src/ILife.java
  2. 85
    13
      src/Life.java
  3. 28
    10
      test/LifeTest.java

+ 2
- 1
src/ILife.java View File



// Methoden zum Abfragen der aktuellen Situation // Methoden zum Abfragen der aktuellen Situation
public boolean isAlive(int x, int y); public boolean isAlive(int x, int y);
public boolean isDead(int x, int y);


// Methoden zum Fortschreiben der Generationen // Methoden zum Fortschreiben der Generationen
public ILife nextGeneration(); public ILife nextGeneration();


}
}

+ 85
- 13
src/Life.java View File

public class Life implements ILife { 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) { public static void main(String[] args) {
Life l = new Life(new String[] { " ", Life l = new Life(new String[] { " ",
" ",
" *** ",
" ",
" " });
" ",
" *** ",
" ",
" " }
);
l.print();
l = (Life) l.nextGeneration(); l = (Life) l.nextGeneration();
l.print();
} }



public Life() { public Life() {
nukeAll(); nukeAll();
} }


public Life(String[] setup) { public Life(String[] setup) {
this(); 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) != ' ') if (setup[y].charAt(x) != ' ')
setAlive(x, y); setAlive(x, y);
} }
@Override @Override
public void nukeAll() { public void nukeAll() {
// TODO Auto-generated method stub // 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 @Override
public void setAlive(int x, int y) { public void setAlive(int x, int y) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
cells[x][y] = CellValue.Alive;
} }


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


@Override @Override
public boolean isAlive(int x, int y) { public boolean isAlive(int x, int y) {
// TODO Auto-generated method stub // 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 @Override
public ILife nextGeneration() { public ILife nextGeneration() {
// TODO Auto-generated method stub // 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;
} }
} }

+ 28
- 10
test/LifeTest.java View File

import static org.junit.Assert.*; import static org.junit.Assert.*;


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


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


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



@Test @Test
public void destroyLonelyCell() { public void destroyLonelyCell() {
}
Life firstGen = new Life();
firstGen.setAlive(0, 0);
firstGen.setAlive(0, 2);


ILife nextGen = firstGen.nextGeneration();

assertTrue(nextGen.isDead(0, 0));
assertTrue(nextGen.isDead(0, 2));
}


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

ILife nextGen = firstGen.nextGeneration();


assertTrue(nextGen.isAlive(2, 2));
}


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


ILife nextGen = firstGen.nextGeneration();


}
assertTrue(nextGen.isDead(0, 1));
assertTrue(nextGen.isDead(1, 1));
}
}

Loading…
Cancel
Save