123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- import java.util.Arrays;
-
- //Gruppe: @FrederikHagen, @DenushanJegatheeswaran, @NathanGebre-Michael, @Justus Görgens
- public class Life implements ILife {
-
- static String[] welt = new String[]{
- " ",
- " ",
- " *** ",
- " ",
- " "};
-
- public static void main(String[] args) {
- Life l = new Life(welt);
- l = (Life) l.nextGeneration();
- }
-
-
- public Life() {
- nukeAll();
- }
-
- 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) != ' ')
- setAlive(x, y);
- }
-
- public void printWelt() {
- for (int i = 0; i < welt.length; i++)
- System.out.println(Arrays.toString(new String[]{welt[i]}));
- }
-
- @Override
- public void nukeAll() {
- // 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
- public void setAlive(int x, int y) {
- // TODO Auto-generated method stub
-
- welt[y] = welt[y].substring(0, x) + '*' + welt[y].substring(x + 1);
- }
-
- @Override
- public void setDead(int x, int y) {
- // TODO Auto-generated method stub
-
- welt[y] = welt[y].substring(0, x) + ' ' + welt[y].substring(x + 1);
- }
-
- @Override
- public boolean isAlive(int x, int y) {
- // TODO Auto-generated method stub
-
- if (welt[y].charAt(x) == '*')
- return true;
- else
- return false;
- }
-
- @Override
- public boolean isDead(int x, int y) {
- // TODO Auto-generated method stub
-
- if (welt[y].charAt(x) == ' ')
- return true;
- else
- 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) + '*' + neueWelt[y].substring(x + 1);
- } else if (x == 0) {
- neueWelt[y] = '*' + neueWelt[y].substring(x + 1);
- } else {
- neueWelt[y] = neueWelt[y].substring(0, x) + '*';
- }
- }
-
- //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) {
- neueWelt[y] = neueWelt[y].substring(0, x) + ' ' + neueWelt[y].substring(x + 1);
- } else if (x == 0) {
- neueWelt[y] = ' ' + neueWelt[y].substring(x + 1);
- } else {
- neueWelt[y] = neueWelt[y].substring(0, x) + ' ';
- }
- }
-
- //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) + '*' + neueWelt[y].substring(x + 1);
- } else if (x == 0) {
- neueWelt[y] = '*' + neueWelt[y].substring(x + 1);
- } else {
- neueWelt[y] = neueWelt[y].substring(0, x) + '*';
- }
- }
- }
- 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();
- }
- }
|