Schreiben der Methoden nukeAll & isAlive (Alle Testfälle müssten jetzt funktionieren)
This commit is contained in:
parent
d0a9f6c7d1
commit
f1ceb04f8d
@ -1,5 +1,4 @@
|
|||||||
public class Life implements ILife {
|
public class Life implements ILife {
|
||||||
|
|
||||||
private int width;
|
private int width;
|
||||||
private int height;
|
private int height;
|
||||||
private boolean[][] grid;
|
private boolean[][] grid;
|
||||||
@ -15,15 +14,13 @@ public class Life implements ILife {
|
|||||||
l = (Life) l.nextGeneration();
|
l = (Life) l.nextGeneration();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Standard-Konstruktor -> Erstellt ein leeres Spielfeld mit Standardgröße
|
|
||||||
public Life() {
|
public Life() {
|
||||||
this.width = 5; // Standardbreite
|
this.width = 5;
|
||||||
this.height = 5; // Standardhöhe
|
this.height = 5;
|
||||||
this.grid = new boolean[width][height];
|
this.grid = new boolean[width][height];
|
||||||
nukeAll();
|
nukeAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Konstruktor mit Setup-String
|
|
||||||
public Life(String[] setup) {
|
public Life(String[] setup) {
|
||||||
this.height = setup.length;
|
this.height = setup.length;
|
||||||
this.width = setup[0].length();
|
this.width = setup[0].length();
|
||||||
@ -38,40 +35,44 @@ public class Life implements ILife {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setzt alle Zellen auf "tot"
|
|
||||||
@Override
|
@Override
|
||||||
public void nukeAll() {
|
public void nukeAll() {
|
||||||
|
for (int y = 0; y < height; y++) {
|
||||||
|
for (int x = 0; x < width; x++) {
|
||||||
|
grid[x][y] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setzt eine Zelle auf "lebendig"
|
|
||||||
@Override
|
@Override
|
||||||
public void setAlive(int x, int y) {
|
public void setAlive(int x, int y) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setzt eine Zelle auf "tot"
|
|
||||||
@Override
|
@Override
|
||||||
public void setDead(int x, int y) {
|
public void setDead(int x, int y) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAlive(int x, int y) {
|
public boolean isAlive(int x, int y) {
|
||||||
return isWithinBounds(x, y) && grid[x][y];
|
return isInBounds(x, y) && grid[x][y];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Berechnet die nächste Generation des Spielfelds
|
|
||||||
@Override
|
@Override
|
||||||
public ILife nextGeneration() {
|
public ILife nextGeneration() {
|
||||||
Life newLife = new Life(new String[height]); // Neues Life-Objekt mit gleicher Größe
|
Life newLife = new Life();
|
||||||
|
newLife.width = this.width;
|
||||||
|
newLife.height = this.height;
|
||||||
|
newLife.grid = new boolean[width][height];
|
||||||
|
|
||||||
for (int y = 0; y < height; y++) {
|
for (int y = 0; y < height; y++) {
|
||||||
newLife.grid[y] = new boolean[width]; // Spalten anlegen
|
|
||||||
for (int x = 0; x < width; x++) {
|
for (int x = 0; x < width; x++) {
|
||||||
int neighbors = countAliveNeighbors(x, y);
|
int neighbors = countAliveNeighbors(x, y);
|
||||||
|
|
||||||
if (isAlive(x, y)) {
|
if (isAlive(x, y)) {
|
||||||
if (neighbors == 2 || neighbors == 3) {
|
if (neighbors < 2 || neighbors > 3) {
|
||||||
newLife.setAlive(x, y);
|
|
||||||
} else {
|
|
||||||
newLife.setDead(x, y);
|
newLife.setDead(x, y);
|
||||||
|
} else {
|
||||||
|
newLife.setAlive(x, y);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (neighbors == 3) {
|
if (neighbors == 3) {
|
||||||
@ -93,8 +94,8 @@ public class Life implements ILife {
|
|||||||
}
|
}
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
private boolean isWithinBounds(int x, int y) {
|
|
||||||
|
private boolean isInBounds(int x, int y) {
|
||||||
return x >= 0 && x < width && y >= 0 && y < height;
|
return x >= 0 && x < width && y >= 0 && y < height;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,21 +5,16 @@ 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 l = new Life();
|
Life l = new Life();
|
||||||
@ -30,23 +25,18 @@ public class LifeTest {
|
|||||||
assertFalse(nextGen.isAlive(2, 2));
|
assertFalse(nextGen.isAlive(2, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void keepAliveCell() {
|
public void keepAliveCell() {
|
||||||
// Arrange: Eine Zelle mit zwei Nachbarn
|
|
||||||
Life l = new Life();
|
Life l = new Life();
|
||||||
l.setAlive(1, 1);
|
l.setAlive(1, 1);
|
||||||
l.setAlive(1, 2);
|
l.setAlive(1, 2);
|
||||||
l.setAlive(2, 1);
|
l.setAlive(2, 1);
|
||||||
|
|
||||||
// Act: Nächste Generation berechnen
|
|
||||||
ILife nextGen = l.nextGeneration();
|
ILife nextGen = l.nextGeneration();
|
||||||
|
|
||||||
// Assert: Die Zelle bleibt am Leben
|
|
||||||
assertTrue(nextGen.isAlive(1, 1));
|
assertTrue(nextGen.isAlive(1, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void destroyCrowdedCell() {
|
public void destroyCrowdedCell() {
|
||||||
Life l = new Life();
|
Life l = new Life();
|
||||||
@ -61,4 +51,3 @@ public class LifeTest {
|
|||||||
assertFalse(nextGen.isAlive(1, 1));
|
assertFalse(nextGen.isAlive(1, 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user