ZUSATZ, abseits der Aufgabenstellung: visuelle Darstellung in der Konsole
This commit is contained in:
parent
ecf0483fb1
commit
1c80d9154d
Binary file not shown.
@ -1,16 +1,33 @@
|
||||
public class Life implements ILife {
|
||||
|
||||
private boolean[][] grid = new boolean[5][5]; // vorläufig 5x5 Raster
|
||||
private boolean[][] grid = new boolean[10][10];
|
||||
|
||||
//
|
||||
// ----- ZUSATZ -----
|
||||
//
|
||||
// Visuelle Darstellung des Spiels in der Konsole, einfach weil ich Lust darauf hatte.
|
||||
// Mir ist bewusst, dass das Spielfeld nicht dynamisch ist und auch keine Überprüfung stattfindet,
|
||||
// ob eine Zelle über das Feld hinaus läuft.
|
||||
//
|
||||
|
||||
public static void main(String[] args) {
|
||||
Life l = new Life(new String[] { " ",
|
||||
" ",
|
||||
" *** ",
|
||||
" ",
|
||||
" " });
|
||||
l = (Life) l.nextGeneration();
|
||||
}
|
||||
Life l = new Life(new String[] {
|
||||
" ",
|
||||
" * ",
|
||||
" *** ",
|
||||
" * ",
|
||||
" " });
|
||||
|
||||
while (true) {
|
||||
l.printGrid();
|
||||
l = (Life) l.nextGeneration();
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Life() {
|
||||
nukeAll();
|
||||
@ -24,7 +41,6 @@ public class Life implements ILife {
|
||||
setAlive(x, y);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void nukeAll() {
|
||||
for (int y = 0; y < grid.length; y++) {
|
||||
@ -55,7 +71,7 @@ public class Life implements ILife {
|
||||
|
||||
for (int y = 0; y < grid.length; y++) {
|
||||
for (int x = 0; x < grid[y].length; x++) {
|
||||
int aliveNeighbours = countAliveNeighbours(x, y); // Nur einmal berechnen
|
||||
int aliveNeighbours = countAliveNeighbours(x, y);
|
||||
|
||||
if (isAlive(x, y) && (aliveNeighbours == 2 || aliveNeighbours == 3)) {
|
||||
next.setAlive(x, y);
|
||||
@ -91,4 +107,14 @@ public class Life implements ILife {
|
||||
private boolean isValid(int x, int y) {
|
||||
return x >= 0 && x < grid.length && y >= 0 && y < grid[x].length;
|
||||
}
|
||||
}
|
||||
|
||||
public void printGrid() {
|
||||
for (int y = 0; y < grid.length; y++) {
|
||||
for (int x = 0; x < grid[y].length; x++) {
|
||||
System.out.print(isAlive(x, y) ? "*" : " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
System.out.println(); // Leerzeile für bessere Lesbarkeit
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user