Compare commits
4 Commits
09a995eccf
...
12e086ed53
Author | SHA1 | Date | |
---|---|---|---|
![]() |
12e086ed53 | ||
![]() |
892cd3870f | ||
![]() |
cdabef59bb | ||
![]() |
2095a206e6 |
48
README.md
48
README.md
@ -1,48 +0,0 @@
|
|||||||
# Programmieren 3
|
|
||||||
|
|
||||||
## Assignment "Game Of Life"
|
|
||||||
|
|
||||||
### Einführung
|
|
||||||
|
|
||||||
#### Game of Life
|
|
||||||
|
|
||||||
Ihre Aufgabe ist es, eine Klasse <code>Life</code> zu implementieren,
|
|
||||||
die das *Game of Life* simuliert. Beim *Game of Life*
|
|
||||||
werden Zellen in einem zweidimensionelen Raster angeordnet.
|
|
||||||
An jedem Rasterpunkt kann entweder eine lebende Zelle oder ein
|
|
||||||
Leerraum positioniert werden.
|
|
||||||
|
|
||||||
Für das Entstehen, Überleben oder Absterben einer Zelle
|
|
||||||
ist die Anzahl der unmittelbaren Nachbarn entscheidend.
|
|
||||||
Jeder Rasterpunkt hat 8 benachbarte Rasterpunkte
|
|
||||||
(links, rechts, oben, unten, oben links, oben rechts, unten links, unten rechts).
|
|
||||||
Für die Berechnung der nachfolgenden Generation sind auf die
|
|
||||||
aktuelle Generation folgende Regeln anzuwenden:
|
|
||||||
|
|
||||||
- Eine neue Zelle an einem bisher unbelebten Rasterpunkt entsteht,
|
|
||||||
wenn genau drei benachbarte Rasterpunkte mit lebenden Zellen
|
|
||||||
besetzt sind.
|
|
||||||
|
|
||||||
- Lebende Zellen mit weniger als zwei lebenden Nachbarn sterben in der Folgegeneration an Einsamkeit.
|
|
||||||
|
|
||||||
- Eine lebende Zelle mit zwei oder drei lebenden Nachbarn bleibt in der Folgegeneration am Leben.
|
|
||||||
|
|
||||||
- Lebende Zellen mit mehr als drei lebenden Nachbarn sterben in der Folgegeneration an Überbevölkerung.
|
|
||||||
|
|
||||||
|
|
||||||
#### Vorgaben
|
|
||||||
|
|
||||||
Im Assignment werden Ihnen drei Code-Dateien vorgegeben:
|
|
||||||
|
|
||||||
- <code>Life</code> soll später den Produktivcode enthalten.
|
|
||||||
- <code>LifeTest</code> soll für den Testcode benutzt werden.
|
|
||||||
- <code>ILife</code> ist ein Interface, das von der <code>Life</code>-Klasse
|
|
||||||
implementiert werden soll.
|
|
||||||
|
|
||||||
### Aufgabenstellung
|
|
||||||
|
|
||||||
Implementieren Sie die Klasse <code>Life</code> unter Nutzung der TDD-Vorgehensweise.
|
|
||||||
Erstellen Sie für die oben genannten Regeln jeweils mindestens einen Testfall (für
|
|
||||||
die erste Regel wurde bereits ein Testfall mitgeliefert).
|
|
||||||
|
|
||||||
|
|
@ -11,4 +11,7 @@ public interface ILife {
|
|||||||
// Methoden zum Fortschreiben der Generationen
|
// Methoden zum Fortschreiben der Generationen
|
||||||
public ILife nextGeneration();
|
public ILife nextGeneration();
|
||||||
|
|
||||||
|
//Methode zum ausgeben der Welt
|
||||||
|
public void printWelt();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
214
src/Life.java
214
src/Life.java
@ -1,55 +1,187 @@
|
|||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class Life implements ILife {
|
public class Life implements ILife {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
static String[] welt = new String[]{
|
||||||
Life l = new Life(new String[] { " ",
|
" ",
|
||||||
" ",
|
" ",
|
||||||
" *** ",
|
" *** ",
|
||||||
" ",
|
" ",
|
||||||
" " });
|
" "};
|
||||||
l = (Life) l.nextGeneration();
|
|
||||||
}
|
public static void main(String[] args) {
|
||||||
|
Life l = new Life(welt);
|
||||||
|
l = (Life) l.nextGeneration();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public Life() {
|
public Life() {
|
||||||
nukeAll();
|
nukeAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Life(String[] setup) {
|
public Life(String[] setup) {
|
||||||
this();
|
this();
|
||||||
for (int y = 0; y < setup.length; y++)
|
for (int y = 0; y < setup.length; y++)
|
||||||
for (int x = 0; x < setup[y].length(); x++)
|
for (int x = 0; x < setup[y].length(); x++)
|
||||||
if (setup[y].charAt(x) != ' ')
|
if (setup[y].charAt(x) != ' ')
|
||||||
setAlive(x, y);
|
setAlive(x, y);
|
||||||
}
|
printWelt();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void printWelt() {
|
||||||
|
for (int i = 0; i < welt.length; i++)
|
||||||
|
System.out.println(Arrays.toString(new String[]{welt[i]}));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void nukeAll() {
|
public void nukeAll() {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
}
|
for (int y = 0; y < welt.length; y++)
|
||||||
|
for (int x = 0; x < welt[y].length(); x++)
|
||||||
|
setDead(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setAlive(int x, int y) {
|
public void setAlive(int x, int y) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
}
|
welt[y] = welt[y].substring(0, x) + '*' + welt[y].substring(x + 1);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setDead(int x, int y) {
|
public void setDead(int x, int y) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
}
|
welt[y] = welt[y].substring(0, x) + ' ' + welt[y].substring(x + 1);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAlive(int x, int y) {
|
public boolean isAlive(int x, int y) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
if (welt[y].charAt(x) == '*')
|
||||||
public ILife nextGeneration() {
|
return true;
|
||||||
// TODO Auto-generated method stub
|
else
|
||||||
return null;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ILife nextGeneration() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
String[] neueWelt = new String[]{
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" "};
|
||||||
|
|
||||||
|
//Zellen einzeln betrachten
|
||||||
|
for (int y = 0; y < welt.length; y++)
|
||||||
|
for (int x = 0; x < welt[y].length(); x++) {
|
||||||
|
|
||||||
|
//anzahl der lebenden nachbarn
|
||||||
|
int nachbarn = lebendeNachbarn(y, x);
|
||||||
|
|
||||||
|
//neue Zelle
|
||||||
|
//TODO fehler wenn x = 0
|
||||||
|
if (nachbarn == 3) {
|
||||||
|
if (x > 0 && x < welt[0].length() - 1) {
|
||||||
|
neueWelt[y] = neueWelt[y].substring(0, x - 1) + '*' + neueWelt[y].substring(x);
|
||||||
|
} else if (x == 0) {
|
||||||
|
neueWelt[y] = '*' + neueWelt[y].substring(x);
|
||||||
|
} else {
|
||||||
|
neueWelt[y] = neueWelt[y].substring(0, x - 1) + '*';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//eine Zelle stirbt an überpopulation oder einsamkeit
|
||||||
|
//TODO fehler wenn x = 0
|
||||||
|
if (nachbarn < 2 || nachbarn > 3) {
|
||||||
|
if (x > 0 && x < welt[0].length() - 1) {
|
||||||
|
System.out.println(welt[0].length());
|
||||||
|
System.out.print(nachbarn);
|
||||||
|
System.out.print(", " + x);
|
||||||
|
System.out.println(", " + y);
|
||||||
|
neueWelt[y] = neueWelt[y].substring(0, x - 1) + ' ' + neueWelt[y].substring(x);
|
||||||
|
} else if (x == 0) {
|
||||||
|
neueWelt[y] = ' ' + neueWelt[y].substring(x);
|
||||||
|
} else {
|
||||||
|
neueWelt[y] = neueWelt[y].substring(0, x - 1) + ' ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//eine Zelle bleibt am Leben
|
||||||
|
//TODO fehler wenn x = 0
|
||||||
|
if ((nachbarn == 2) && (welt[y].charAt(x) == '*')) {
|
||||||
|
if (x > 0 && x < welt[0].length() - 1) {
|
||||||
|
neueWelt[y] = neueWelt[y].substring(0, x - 1) + '*' + neueWelt[y].substring(x);
|
||||||
|
} else if (x == 0) {
|
||||||
|
neueWelt[y] = '*' + neueWelt[y].substring(x);
|
||||||
|
} else {
|
||||||
|
neueWelt[y] = neueWelt[y].substring(0, x - 1) + '*';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Life(neueWelt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int lebendeNachbarn(int y, int x) { // 1, 1
|
||||||
|
|
||||||
|
int nachbarn = 0;
|
||||||
|
|
||||||
|
//linke nachbars-spalte
|
||||||
|
if (x > 0) {
|
||||||
|
if (y > 0) {
|
||||||
|
if (welt[y - 1].charAt(x - 1) == '*') {
|
||||||
|
nachbarn++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (welt[y].charAt(x - 1) == '*') {
|
||||||
|
nachbarn++;
|
||||||
|
}
|
||||||
|
if (y < welt.length - 1) {
|
||||||
|
if (welt[y + 1].charAt(x - 1) == '*') {
|
||||||
|
nachbarn++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//rechte nachbars-spalte
|
||||||
|
if (x < welt[0].length() - 1) {
|
||||||
|
if (y > 0) {
|
||||||
|
if (welt[y - 1].charAt(x + 1) == '*') {
|
||||||
|
nachbarn++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (welt[y].charAt(x + 1) == '*') {
|
||||||
|
nachbarn++;
|
||||||
|
}
|
||||||
|
if (y < welt.length - 1) {
|
||||||
|
if (welt[y + 1].charAt(x + 1) == '*') {
|
||||||
|
nachbarn++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// mittlere Spalte
|
||||||
|
if (y > 0) {
|
||||||
|
if (welt[y - 1].charAt(x) == '*') {
|
||||||
|
nachbarn++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (y < welt.length - 1) {
|
||||||
|
if (welt[y + 1].charAt(x) == '*') {
|
||||||
|
nachbarn++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nachbarn;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return super.toString();
|
||||||
|
}
|
||||||
}
|
}
|
@ -8,15 +8,17 @@ public class LifeTest {
|
|||||||
|
|
||||||
// Arrange: drei lebende Zellen
|
// Arrange: drei lebende Zellen
|
||||||
Life l = new Life();
|
Life l = new Life();
|
||||||
|
l.printWelt();
|
||||||
l.setAlive(0, 0);
|
l.setAlive(0, 0);
|
||||||
l.setAlive(0, 1);
|
l.setAlive(0, 1);
|
||||||
l.setAlive(0, 2);
|
l.setAlive(0, 2);
|
||||||
|
l.printWelt();
|
||||||
|
|
||||||
// Act: Berechnung der Folgegeneration
|
// Act: Berechnung der Folgegeneration
|
||||||
ILife nextGen = l.nextGeneration();
|
ILife nextGen = l.nextGeneration();
|
||||||
|
|
||||||
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
|
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
|
||||||
assertTrue(nextGen.isAlive(1, 1));
|
assertTrue(nextGen.isAlive(0, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user