Jetzt gehen keine Tests mehr irgendwie
This commit is contained in:
parent
b6ed712507
commit
d0a9f6c7d1
@ -1,64 +1,80 @@
|
||||
public class Life implements ILife {
|
||||
|
||||
|
||||
private int width;
|
||||
private int height;
|
||||
private boolean[][] grid;
|
||||
|
||||
public static void main(String[] args) {
|
||||
Life l = new Life(new String[] { " ",
|
||||
" ",
|
||||
" *** ",
|
||||
" ",
|
||||
" " });
|
||||
Life l = new Life(new String[]{
|
||||
" ",
|
||||
" ",
|
||||
" *** ",
|
||||
" ",
|
||||
" "
|
||||
});
|
||||
l = (Life) l.nextGeneration();
|
||||
}
|
||||
|
||||
|
||||
// Standard-Konstruktor -> Erstellt ein leeres Spielfeld mit Standardgröße
|
||||
public Life() {
|
||||
this.width = 5; // Standardbreite
|
||||
this.height = 5; // Standardhöhe
|
||||
this.grid = new boolean[width][height];
|
||||
nukeAll();
|
||||
}
|
||||
|
||||
// Konstruktor mit Setup-String
|
||||
public Life(String[] setup) {
|
||||
this();
|
||||
for (int y = 0; y < setup.length; y++)
|
||||
for (int x = 0; x < setup[y].length(); x++)
|
||||
if (setup[y].charAt(x) != ' ')
|
||||
this.height = setup.length;
|
||||
this.width = setup[0].length();
|
||||
this.grid = new boolean[width][height];
|
||||
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
if (setup[y].charAt(x) == '*') {
|
||||
setAlive(x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Setzt alle Zellen auf "tot"
|
||||
@Override
|
||||
public void nukeAll() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
// Setzt eine Zelle auf "lebendig"
|
||||
@Override
|
||||
public void setAlive(int x, int y) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
// Setzt eine Zelle auf "tot"
|
||||
@Override
|
||||
public void setDead(int x, int y) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAlive(int x, int y) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
return isWithinBounds(x, y) && grid[x][y];
|
||||
}
|
||||
|
||||
// Berechnet die nächste Generation des Spielfelds
|
||||
@Override
|
||||
public ILife nextGeneration() {
|
||||
Life newLife = new Life();
|
||||
|
||||
Life newLife = new Life(new String[height]); // Neues Life-Objekt mit gleicher Größe
|
||||
for (int y = 0; y < height; y++) {
|
||||
newLife.grid[y] = new boolean[width]; // Spalten anlegen
|
||||
for (int x = 0; x < width; x++) {
|
||||
int neighbors = countAliveNeighbors(x, y);
|
||||
|
||||
if (isAlive(x, y)) {
|
||||
if (neighbors < 2 || neighbors > 3) {
|
||||
newLife.setDead(x, y);
|
||||
if (neighbors == 2 || neighbors == 3) {
|
||||
newLife.setAlive(x, y);
|
||||
} else {
|
||||
newLife.setDead(x, y);
|
||||
}
|
||||
} else {
|
||||
if (neighbors == 3) {
|
||||
newLife.setAlive(x, y);
|
||||
}
|
||||
}
|
||||
@ -67,14 +83,18 @@ public class Life implements ILife {
|
||||
return newLife;
|
||||
}
|
||||
|
||||
private int countAliveNeighbors(int x, int y) {
|
||||
int count = 0;
|
||||
for (int dy = -1; dy <= 1; dy++) {
|
||||
for (int dx = -1; dx <= 1; dx++) {
|
||||
if (dx == 0 && dy == 0) continue; // Eigene Zelle ignorieren
|
||||
if (isAlive(x + dx, y + dy)) count++;
|
||||
}
|
||||
private int countAliveNeighbors(int x, int y) {
|
||||
int count = 0;
|
||||
for (int dy = -1; dy <= 1; dy++) {
|
||||
for (int dx = -1; dx <= 1; dx++) {
|
||||
if (dx == 0 && dy == 0) continue;
|
||||
if (isAlive(x + dx, y + dy)) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
private boolean isWithinBounds(int x, int y) {
|
||||
return x >= 0 && x < width && y >= 0 && y < height;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user