|
|
@@ -1,55 +1,140 @@ |
|
|
|
import java.util.Arrays; |
|
|
|
|
|
|
|
public class Life implements ILife { |
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
Life l = new Life(new String[] { " ", |
|
|
|
" ", |
|
|
|
" *** ", |
|
|
|
" ", |
|
|
|
" " }); |
|
|
|
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); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public void nukeAll() { |
|
|
|
// TODO Auto-generated method stub |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void setAlive(int x, int y) { |
|
|
|
// TODO Auto-generated method stub |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void setDead(int x, int y) { |
|
|
|
// TODO Auto-generated method stub |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public boolean isAlive(int x, int y) { |
|
|
|
// TODO Auto-generated method stub |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public ILife nextGeneration() { |
|
|
|
// TODO Auto-generated method stub |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
static String[] board = new String[]{ "* ", |
|
|
|
"* ", |
|
|
|
"* ", |
|
|
|
" ", |
|
|
|
" " }; |
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
Life l = new Life(board); |
|
|
|
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); |
|
|
|
} |
|
|
|
} |
|
|
|
//System.out.println(board[y]); |
|
|
|
} |
|
|
|
printTable(board); |
|
|
|
} |
|
|
|
|
|
|
|
private void printTable(String[] board) { //works |
|
|
|
for(int i = 0; i < board.length; i++) System.out.println(Arrays.toString(new String[]{board[i]})); |
|
|
|
System.out.println("________"); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public void nukeAll() { //works |
|
|
|
// TODO Auto-generated method stub |
|
|
|
for (int y = 0; y < board.length; y++){ |
|
|
|
for (int x = 0; x < board[y].length(); x++) { |
|
|
|
setDead(x, y); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void setAlive(int x, int y) { //works |
|
|
|
// TODO Auto-generated method stub |
|
|
|
board[y] = board[y].substring(0, x) + '*' + board[y].substring(x+1); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void setDead(int x, int y) { //works |
|
|
|
// TODO Auto-generated method stub |
|
|
|
board[y] = board[y].substring(0, x) + ' ' + board[y].substring(x+1); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public boolean isAlive(int x, int y) { //works |
|
|
|
//char c = ; |
|
|
|
if(board[y].charAt(x) == '*'){ //-->Befehl falsch immer false!!! |
|
|
|
return true; |
|
|
|
} |
|
|
|
else{ |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public ILife nextGeneration() { |
|
|
|
System.out.println("next Generation"); |
|
|
|
int alive; |
|
|
|
|
|
|
|
for (int y = 0; y < board.length; y++){ |
|
|
|
for (int x = 0; x < board[y].length(); x++){ |
|
|
|
alive=aliveNeighbours(x,y); //Problem immer 0 |
|
|
|
|
|
|
|
if(!isAlive(x,y) && alive == 3) { |
|
|
|
setAlive(x, y); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
printTable(board); |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
private int aliveNeighbours(int x, int y) { //-->Problem wahrscheinlich Befehl substring siehe isAlive |
|
|
|
int neighbours = 0; |
|
|
|
|
|
|
|
if(x>0 && y>0){ |
|
|
|
if(board[y-1].charAt(x-1) == '*'){ |
|
|
|
neighbours++; |
|
|
|
} |
|
|
|
} |
|
|
|
if(x>0){ |
|
|
|
if(board[y].charAt(x-1) == '*'){ |
|
|
|
neighbours++; |
|
|
|
} |
|
|
|
if(y<4 ){ |
|
|
|
if(board[y+1].charAt(x-1) == '*'){ |
|
|
|
neighbours++; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if(y>0){ |
|
|
|
if(board[y-1].charAt(x) == '*'){ |
|
|
|
neighbours++; |
|
|
|
} |
|
|
|
if(x<4){ |
|
|
|
if(board[y-1].charAt(x+1) == '*'){ |
|
|
|
neighbours++; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if(x<4){ |
|
|
|
if(board[y].charAt(x+1) == '*'){ |
|
|
|
neighbours++; |
|
|
|
} |
|
|
|
} |
|
|
|
if(y<4){ |
|
|
|
if(board[y+1].charAt(x) == '*'){ |
|
|
|
neighbours++; |
|
|
|
} |
|
|
|
} |
|
|
|
if(x<4 && y<4){ |
|
|
|
if(board[y+1].charAt(x+1) == '*'){ |
|
|
|
neighbours++; |
|
|
|
} |
|
|
|
} |
|
|
|
return neighbours; |
|
|
|
} |
|
|
|
} |
|
|
|
|