Jetzt gehen keine Tests mehr irgendwie
This commit is contained in:
parent
b6ed712507
commit
d0a9f6c7d1
@ -1,64 +1,80 @@
|
|||||||
public class Life implements ILife {
|
public class Life implements ILife {
|
||||||
|
|
||||||
|
private int width;
|
||||||
|
private int height;
|
||||||
|
private boolean[][] grid;
|
||||||
|
|
||||||
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 = (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.height = 5; // Standardhöhe
|
||||||
|
this.grid = new boolean[width][height];
|
||||||
nukeAll();
|
nukeAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Konstruktor mit Setup-String
|
||||||
public Life(String[] setup) {
|
public Life(String[] setup) {
|
||||||
this();
|
this.height = setup.length;
|
||||||
for (int y = 0; y < setup.length; y++)
|
this.width = setup[0].length();
|
||||||
for (int x = 0; x < setup[y].length(); x++)
|
this.grid = new boolean[width][height];
|
||||||
if (setup[y].charAt(x) != ' ')
|
|
||||||
|
for (int y = 0; y < height; y++) {
|
||||||
|
for (int x = 0; x < width; x++) {
|
||||||
|
if (setup[y].charAt(x) == '*') {
|
||||||
setAlive(x, y);
|
setAlive(x, y);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setzt alle Zellen auf "tot"
|
||||||
@Override
|
@Override
|
||||||
public void nukeAll() {
|
public void nukeAll() {
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Setzt eine Zelle auf "lebendig"
|
||||||
@Override
|
@Override
|
||||||
public void setAlive(int x, int y) {
|
public void setAlive(int x, int y) {
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Setzt eine Zelle auf "tot"
|
||||||
@Override
|
@Override
|
||||||
public void setDead(int x, int y) {
|
public void setDead(int x, int y) {
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAlive(int x, int y) {
|
public boolean isAlive(int x, int y) {
|
||||||
// TODO Auto-generated method stub
|
return isWithinBounds(x, y) && grid[x][y];
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Berechnet die nächste Generation des Spielfelds
|
||||||
@Override
|
@Override
|
||||||
public ILife nextGeneration() {
|
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++) {
|
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.setDead(x, y);
|
newLife.setAlive(x, y);
|
||||||
} else {
|
} else {
|
||||||
|
newLife.setDead(x, y);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (neighbors == 3) {
|
||||||
newLife.setAlive(x, y);
|
newLife.setAlive(x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -71,10 +87,14 @@ public class Life implements ILife {
|
|||||||
int count = 0;
|
int count = 0;
|
||||||
for (int dy = -1; dy <= 1; dy++) {
|
for (int dy = -1; dy <= 1; dy++) {
|
||||||
for (int dx = -1; dx <= 1; dx++) {
|
for (int dx = -1; dx <= 1; dx++) {
|
||||||
if (dx == 0 && dy == 0) continue; // Eigene Zelle ignorieren
|
if (dx == 0 && dy == 0) continue;
|
||||||
if (isAlive(x + dx, y + dy)) count++;
|
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