Abgabe fertig

This commit is contained in:
Sarah Baumann 2021-12-25 14:06:15 +01:00
parent 09a995eccf
commit 2236d2bd00
3 changed files with 116 additions and 25 deletions

View File

@ -7,8 +7,9 @@ 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

@ -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;
}
}

View File

@ -2,37 +2,55 @@ 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.setAlive(0, 0);
l.setAlive(0, 1);
l.setAlive(0, 2);
// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
assertTrue(nextGen.isAlive(1, 1));
}
@Test
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
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
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));
}
}
}