|
|
@@ -1,17 +1,22 @@ |
|
|
|
//Bearbeitet von: Marie Trexler |
|
|
|
import java.util.Arrays; |
|
|
|
public class Life implements ILife { |
|
|
|
|
|
|
|
String[] gameWorld; |
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
Life l = new Life(new String[] { " ", |
|
|
|
Life l = new Life( new String[] { " ", |
|
|
|
" ", |
|
|
|
" *** ", |
|
|
|
" ", |
|
|
|
" " }); |
|
|
|
" "}); |
|
|
|
l = (Life) l.nextGeneration(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public Life() { |
|
|
|
public Life() { |
|
|
|
nukeAll(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public Life(String[] setup) { |
|
|
@@ -19,37 +24,123 @@ public class Life implements ILife { |
|
|
|
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); |
|
|
|
setAlive(x, y); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public void nukeAll() { |
|
|
|
// TODO Auto-generated method stub |
|
|
|
gameWorld = new String[]{" ", |
|
|
|
" ", |
|
|
|
" ", |
|
|
|
" ", |
|
|
|
" "}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void setAlive(int x, int y) { |
|
|
|
// TODO Auto-generated method stub |
|
|
|
String newString =""; |
|
|
|
for(int i=0; i< gameWorld[y].length(); i++){ |
|
|
|
if(i==x) |
|
|
|
newString +='*'; |
|
|
|
else |
|
|
|
newString += gameWorld[y].charAt(i); |
|
|
|
|
|
|
|
} |
|
|
|
gameWorld[y]=newString; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void setDead(int x, int y) { |
|
|
|
// TODO Auto-generated method stub |
|
|
|
String newString =""; |
|
|
|
for(int i=0; i< gameWorld[y].length(); i++){ |
|
|
|
if(i==x) |
|
|
|
newString +=' '; |
|
|
|
else |
|
|
|
newString += gameWorld[y].charAt(i); |
|
|
|
|
|
|
|
} |
|
|
|
gameWorld[y]=newString; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public boolean isAlive(int x, int y) { |
|
|
|
// TODO Auto-generated method stub |
|
|
|
if(gameWorld[y].charAt(x) =='*'){ |
|
|
|
return true;} |
|
|
|
else{ |
|
|
|
return false; |
|
|
|
} |
|
|
|
}} |
|
|
|
|
|
|
|
@Override |
|
|
|
public ILife nextGeneration() { |
|
|
|
// TODO Auto-generated method stub |
|
|
|
return null; |
|
|
|
Life newWorld = new Life(); |
|
|
|
for(int y=0; y< gameWorld.length;y++){ |
|
|
|
for(int x= 0; x <gameWorld[y].length(); x++){ |
|
|
|
int neighbor = countNeighbor(x, y); |
|
|
|
if(isAlive(x,y)) { |
|
|
|
if (neighbor == 3 || neighbor == 2) { |
|
|
|
newWorld.setAlive(x, y); |
|
|
|
} |
|
|
|
if (neighbor < 2 || neighbor > 3) { |
|
|
|
newWorld.setDead(x, y); |
|
|
|
} |
|
|
|
} |
|
|
|
if(!isAlive(x,y)) { |
|
|
|
if (neighbor == 3) { |
|
|
|
newWorld.setAlive(x, y); |
|
|
|
} else { |
|
|
|
newWorld.setDead(x, y); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
newWorld.showBoard(); |
|
|
|
return newWorld; |
|
|
|
} |
|
|
|
|
|
|
|
public int countNeighbor(int x, int y) { |
|
|
|
int counter=0; |
|
|
|
if(y-1 >=0){ |
|
|
|
for(int i =x-1; i<= x+1;i++){ |
|
|
|
if(i>=0 && i< gameWorld[y-1].length()){ |
|
|
|
if(isAlive(i,y-1)){ |
|
|
|
counter++; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if(y+1 <gameWorld.length){ |
|
|
|
for(int i =x-1; i<= x+1;i++){ |
|
|
|
if(i>=0 && i< gameWorld[y+1].length()){ |
|
|
|
if(isAlive(i,y+1)){ |
|
|
|
counter++; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if(x-1>=0){ |
|
|
|
if(isAlive(x-1,y)){ |
|
|
|
counter++; |
|
|
|
} |
|
|
|
} |
|
|
|
if( x+1< gameWorld[y].length()){ |
|
|
|
if(isAlive(x+1,y)){ |
|
|
|
counter++; |
|
|
|
} |
|
|
|
} |
|
|
|
return counter; |
|
|
|
} |
|
|
|
|
|
|
|
public void showBoard() { |
|
|
|
for (int i = 0; i < gameWorld.length; i++) |
|
|
|
System.out.println(Arrays.toString(new String[]{gameWorld[i]})); |
|
|
|
} |
|
|
|
|
|
|
|
} |