Browse Source

final final commit, I promise

master
Felix Schmidt 3 years ago
parent
commit
d0d5792197
1 changed files with 58 additions and 44 deletions
  1. 58
    44
      src/Life.java

+ 58
- 44
src/Life.java View File

@@ -5,9 +5,9 @@ public class Life implements ILife {
String[] lifeArray;
public static void main(String[] args) {
Life l = new Life(new String[] { "* ",
"* ",
"* ",
Life l = new Life(new String[] { " ",
" ",
" *** ",
" ",
" " });
l = (Life) l.nextGeneration();
@@ -39,15 +39,10 @@ public class Life implements ILife {
public void setAlive(int x, int y) {
// TODO Auto-generated method stub
String newString = "";
for(int i = 0; i < lifeArray[y].length(); i++) {
if(i == x) {
newString += '*';
} else {
newString += lifeArray[y].charAt(i);
}
}

lifeArray[y] = newString;
newString = lifeArray[y].substring(0, x) + "*" + lifeArray[y].substring(x);

lifeArray[y] = newString;


}
@@ -56,15 +51,10 @@ public class Life implements ILife {
public void setDead(int x, int y) {
// TODO Auto-generated method stub
String newString = "";
for(int i = 0; i < lifeArray[y].length(); i++) {
if(i == x) {
newString += ' ';
} else {
newString += lifeArray[y].charAt(i);
}
}

lifeArray[y] = newString;
newString = lifeArray[y].substring(0, x) + " " + lifeArray[y].substring(x);

lifeArray[y] = newString;

}

@@ -79,28 +69,43 @@ public class Life implements ILife {
@Override
public ILife nextGeneration() {
// TODO Auto-generated method stub
Life newWorld = new Life();
Life newLife = new Life();
for(int y = 0; y < lifeArray.length; y++) {
for(int x = 0; x < lifeArray[y].length(); x++) {
int counter = countNeighbors(x, y);

if(isAlive(x, y)) {
if (counter > 3 || counter < 2)
newWorld.setDead(x, y);
if (counter == 3 || counter == 2)
newWorld.setAlive(x, y);
}
if(!isAlive(x, y)) {
if (counter == 3) {
newWorld.setAlive(x, y);
} else {
newWorld.setDead(x, y);
}
}
int neighbors = countNeighbors(x, y);

writeNewGen(neighbors, newLife, x, y);
}
}

return newWorld;
return newLife;
}

private void writeNewGen(int neighbors, Life newLife, int x, int y) {

//if the space is alive...
if(isAlive(x, y)) {

//...and has more than three or less than two neighbors, it dies
if (neighbors > 3 || neighbors < 2)
newLife.setDead(x, y);

//...and has 2 to 3 neighbors, it survives
if (neighbors == 3 || neighbors == 2)
newLife.setAlive(x, y);
}

//if the space is dead...
if(!isAlive(x, y)) {

//and has 3 neighbors, it comes to life
if (neighbors == 3) {
newLife.setAlive(x, y);
} else {
//and has anything but 3 neighbors, it stays dead
newLife.setDead(x, y);
}
}
}

private int countNeighbors(int x, int y) {
@@ -120,21 +125,27 @@ public class Life implements ILife {

private int countSides(int x, int y) {
int counter = 0;
if(x-1 >= 0)
if(isAlive(x-1, y))

//is the space left us within the playing field and alive?
if(x-1 >= 0 && isAlive(x-1, y))
counter++;
if(x+1 < lifeArray[y].length())
if(isAlive(x+1, y))

//is the space right us within the playing field and alive?
if(x+1 < lifeArray[y].length() && isAlive(x+1, y))
counter++;

return counter;
}

private int countBelow(int x, int y) {
int counter = 0;

//is the line below us within the playing field?
if(y+1 < lifeArray.length) {
for (int i = x-1; i <= x+1; i++) {
if (i >= 0 && i < lifeArray[y+1].length())
if(isAlive(i, y + 1))

//are the spaces on our left, middle and right bottom within the playing field alive?
if (i >= 0 && i < lifeArray[y+1].length() && isAlive(i, y + 1))
counter++;
}
}
@@ -143,10 +154,13 @@ public class Life implements ILife {

private int countAbove(int x, int y) {
int counter = 0;

//is the line above us within the playing field?
if(y-1 >= 0) {
for (int i = x-1; i <= x+1; i++) {
if (i >= 0 && i < lifeArray[y-1].length())
if(isAlive(i, y - 1))

//are the spaces on our left, middle and right top within the playing field alive?
if (i >= 0 && i < lifeArray[y-1].length() && isAlive(i, y - 1))
counter++;
}
}

Loading…
Cancel
Save