|
|
|
|
|
|
|
|
public class Life implements ILife { |
|
|
public class Life implements ILife { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final int x_value = 10; |
|
|
|
|
|
private final int y_value = 10; |
|
|
|
|
|
|
|
|
|
|
|
public enum CellValue {Dead, Alive} |
|
|
|
|
|
private final CellValue[][] cells = new CellValue[x_value][y_value]; |
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
public static void main(String[] args) { |
|
|
Life l = new Life(new String[] { " ", |
|
|
Life l = new Life(new String[] { " ", |
|
|
" ", |
|
|
|
|
|
" *** ", |
|
|
|
|
|
" ", |
|
|
|
|
|
" " }); |
|
|
|
|
|
|
|
|
" ", |
|
|
|
|
|
" *** ", |
|
|
|
|
|
" ", |
|
|
|
|
|
" " } |
|
|
|
|
|
); |
|
|
|
|
|
l.print(); |
|
|
l = (Life) l.nextGeneration(); |
|
|
l = (Life) l.nextGeneration(); |
|
|
|
|
|
l.print(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Life() { |
|
|
public Life() { |
|
|
nukeAll(); |
|
|
nukeAll(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public Life(String[] setup) { |
|
|
public Life(String[] setup) { |
|
|
this(); |
|
|
this(); |
|
|
for (int y = 0; y < setup.length; y++) |
|
|
|
|
|
for (int x = 0; x < setup[y].length(); x++) |
|
|
|
|
|
|
|
|
for (int y = 0; y < y_value; y ++) |
|
|
|
|
|
for (int x = 0; x < x_value; x ++) |
|
|
if (setup[y].charAt(x) != ' ') |
|
|
if (setup[y].charAt(x) != ' ') |
|
|
setAlive(x, y); |
|
|
setAlive(x, y); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void nukeAll() { |
|
|
public void nukeAll() { |
|
|
// TODO Auto-generated method stub |
|
|
// TODO Auto-generated method stub |
|
|
|
|
|
|
|
|
|
|
|
for( int y = 0; y < y_value; y ++){ |
|
|
|
|
|
for( int x = 0; x < x_value; 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 |
|
|
|
|
|
|
|
|
|
|
|
cells[x][y] = CellValue.Alive; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@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 |
|
|
|
|
|
|
|
|
|
|
|
cells[x][y] = CellValue.Dead; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@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; |
|
|
|
|
|
|
|
|
return cells[x][y] == CellValue.Alive; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public boolean isDead(int x, int y) { |
|
|
|
|
|
return cells[x][y] == CellValue.Dead; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public ILife nextGeneration() { |
|
|
public ILife nextGeneration() { |
|
|
// TODO Auto-generated method stub |
|
|
// TODO Auto-generated method stub |
|
|
return null; |
|
|
|
|
|
|
|
|
Life newGeneration = new Life(); |
|
|
|
|
|
for (int y = 0; y < y_value; y ++) { |
|
|
|
|
|
for(int x = 0; x < x_value; x ++) { |
|
|
|
|
|
int neighborsAlive = calculateNeighborsAlive(x,y); |
|
|
|
|
|
if(neighborsAlive == 3) { |
|
|
|
|
|
newGeneration.setAlive(x,y); |
|
|
|
|
|
} |
|
|
|
|
|
if(neighborsAlive < 2){ |
|
|
|
|
|
newGeneration.setDead(x,y); |
|
|
|
|
|
} |
|
|
|
|
|
if(isAlive(x,y) && (neighborsAlive == 2 || neighborsAlive == 3)){ |
|
|
|
|
|
newGeneration.setAlive(x,y); |
|
|
|
|
|
} |
|
|
|
|
|
if(isAlive(x,y) && neighborsAlive > 3) { |
|
|
|
|
|
newGeneration.setDead(x,y); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return newGeneration; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void print() { |
|
|
|
|
|
for(int x= 0; x < x_value; x ++) { |
|
|
|
|
|
for(int y = 0; y < y_value; y ++) { |
|
|
|
|
|
if(cells[x][y] == CellValue.Alive) { |
|
|
|
|
|
System.out.print("*"); |
|
|
|
|
|
}else{ |
|
|
|
|
|
System.out.print(" "); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
System.out.print("\n"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public int calculateNeighborsAlive( int x, int y) { |
|
|
|
|
|
int min_X = x - 1; |
|
|
|
|
|
int max_X = x + 1; |
|
|
|
|
|
int min_Y = y - 1; |
|
|
|
|
|
int max_Y = y + 1; |
|
|
|
|
|
|
|
|
|
|
|
if( min_X < 0) min_X = 0; |
|
|
|
|
|
if( max_X >= x_value) max_X = x_value - 1; |
|
|
|
|
|
if( min_Y < 0) min_Y = 0; |
|
|
|
|
|
if( max_Y >= x_value) max_Y = y_value - 1; |
|
|
|
|
|
|
|
|
|
|
|
int sum = 0; |
|
|
|
|
|
for(int dx = min_X; dx <= max_X; dx++){ |
|
|
|
|
|
for(int dy = min_Y; dy <= max_Y; dy++){ |
|
|
|
|
|
if(dx != x || dy != y) { |
|
|
|
|
|
if(cells[dx][dy] == CellValue.Alive ){ |
|
|
|
|
|
sum++; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return sum; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |