Browse Source

first commit

master
Justus Görgens 2 years ago
parent
commit
cdabef59bb
1 changed files with 90 additions and 6 deletions
  1. 90
    6
      src/Life.java

+ 90
- 6
src/Life.java View File

import java.util.Arrays;

public class Life implements ILife { public class Life implements ILife {

static String[] zelle =new String[]{ " ",
" ",
" *** ",
" ",
" " };
public static void main(String[] args) { public static void main(String[] args) {
Life l = new Life(new String[] { " ",
" ",
" *** ",
" ",
" " });
Life l = new Life(zelle);
l = (Life) l.nextGeneration(); l = (Life) l.nextGeneration();
} }


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);
printZelle();
} }


private void printZelle() {
for (int i = 0; i < zelle.length; i++)
System.out.println(Arrays.toString(new String[]{zelle[i]}));
}


@Override @Override
public void nukeAll() { public void nukeAll() {
// TODO Auto-generated method stub // TODO Auto-generated method stub


for (int y = 0; y < zelle.length; y++)
for (int x = 0; x < zelle[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


zelle[y] = zelle[y].substring(0, x) + '*' + zelle[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


zelle[y] = zelle[y].substring(0, x) + ' ' + zelle[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;

if(zelle[x].charAt(y) == '*')
return true;
else
return false;
} }


@Override @Override
public ILife nextGeneration() { public ILife nextGeneration() {
// TODO Auto-generated method stub // TODO Auto-generated method stub

String[] nextZelle =new String[]{ " ",
" ",
" ",
" ",
" " };

for (int y = 0; y < zelle.length; y++)
for (int x = 0; x < zelle[y].length(); x++){

//anzahl der lebenden nachbarn
float nachbarn = lebendeNachbarn(y, x);

//neuer *
if(nachbarn == 3)
zelle[y] = zelle[y].substring(0, x) + '*' + zelle[y].substring(x+1);

//ein * stirbt an überpopulation
if(nachbarn < 2 || nachbarn > 3)
zelle[y] = zelle[y].substring(0, x) + ' ' + zelle[y].substring(x+1);
}
return null; return null;
} }

private float lebendeNachbarn(int y, int x) {

float nachbarn = 0;

if(y == 0) {
if (zelle[y + 1].charAt(x - 1) == '*') {
nachbarn++;
}
if (zelle[y].charAt(x - 1) == '*') {
nachbarn++;
}
}

if(x == 4 && y == 4) {
if (zelle[y - 1].charAt(x - 1) == '*') {
nachbarn++;
}
if (zelle[y - 1].charAt(x) == '*') {
nachbarn++;
}
if (zelle[y - 1].charAt(x + 1) == '*') {
nachbarn++;
}
}

if(!(x == 0 && y== 0)) {
if (zelle[y].charAt(x + 1) == '*') {
nachbarn++;
}
if (zelle[y + 1].charAt(x + 1) == '*') {
nachbarn++;
}
if (zelle[y + 1].charAt(x) == '*') {
nachbarn++;
}
}

return nachbarn;
}

@Override
public String toString() {
return super.toString();
}
} }

Loading…
Cancel
Save