|
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays; |
|
|
|
|
|
|
|
|
public class Life implements ILife { |
|
|
public class Life implements ILife { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static String[] grid = new String[]{ "* ", |
|
|
|
|
|
"* ", |
|
|
|
|
|
"* ", |
|
|
|
|
|
" ", |
|
|
|
|
|
" " }; |
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
public static void main(String[] args) { |
|
|
Life l = new Life(new String[] { " ", |
|
|
|
|
|
" ", |
|
|
|
|
|
" *** ", |
|
|
|
|
|
" ", |
|
|
|
|
|
" " }); |
|
|
|
|
|
|
|
|
Life l = new Life(grid); |
|
|
l = (Life) l.nextGeneration(); |
|
|
l = (Life) l.nextGeneration(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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++) |
|
|
|
|
|
if (setup[y].charAt(x) != ' ') |
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
printTable(grid); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void printTable(String[] grid) { |
|
|
|
|
|
for(int i = 0; i < grid.length; i++) System.out.println(Arrays.toString(new String[]{grid[i]})); |
|
|
|
|
|
System.out.println("________"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void nukeAll() { |
|
|
public void nukeAll() { |
|
|
// TODO Auto-generated method stub |
|
|
// TODO Auto-generated method stub |
|
|
|
|
|
|
|
|
|
|
|
for (int y = 0; y < grid.length; y++){ |
|
|
|
|
|
for (int x = 0; x < grid[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 |
|
|
|
|
|
|
|
|
|
|
|
grid[y] = grid[y].substring(0, x) + '*' + grid[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 |
|
|
|
|
|
|
|
|
|
|
|
grid[y] = grid[y].substring(0, x) + ' ' + grid[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 (grid[y].charAt(x) == '*'){ |
|
|
|
|
|
return true; |
|
|
|
|
|
} else { |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public ILife nextGeneration() { |
|
|
public ILife nextGeneration() { |
|
|
// TODO Auto-generated method stub |
|
|
// TODO Auto-generated method stub |
|
|
|
|
|
int alive; |
|
|
|
|
|
String[] nextGrid = new String[]{ " ", |
|
|
|
|
|
" ", |
|
|
|
|
|
" ", |
|
|
|
|
|
" ", |
|
|
|
|
|
" " }; |
|
|
|
|
|
|
|
|
|
|
|
for (int y = 0; y < grid.length; y++){ |
|
|
|
|
|
for (int x = 0; x < grid[y].length(); x++){ |
|
|
|
|
|
alive=aliveNeighbourCells(x,y); |
|
|
|
|
|
|
|
|
|
|
|
if(!isAlive(x,y) && alive == 3) { |
|
|
|
|
|
nextGrid[y] = nextGrid[y].substring(0, x) + '*' + nextGrid[y].substring(x+1); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
else if((isAlive(x,y) && (alive < 2)) || (alive > 3)){ |
|
|
|
|
|
nextGrid[y] = nextGrid[y].substring(0, x) + ' ' + nextGrid[y].substring(x+1); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
else if(isAlive(x,y) && (alive == 2 || alive == 3)){ |
|
|
|
|
|
nextGrid[y] = nextGrid[y].substring(0, x) + grid[y].charAt(x) + nextGrid[y].substring(x+1); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
grid = nextGrid; |
|
|
return null; |
|
|
return null; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private int aliveNeighbourCells(int x, int y) { |
|
|
|
|
|
int neighbours = 0; |
|
|
|
|
|
|
|
|
|
|
|
if(x>0 && y>0){ |
|
|
|
|
|
if(grid[y-1].charAt(x-1) == '*'){ |
|
|
|
|
|
neighbours++; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if(x>0){ |
|
|
|
|
|
if(grid[y].charAt(x-1) == '*'){ |
|
|
|
|
|
neighbours++; |
|
|
|
|
|
} |
|
|
|
|
|
if(y<4 ){ |
|
|
|
|
|
if(grid[y+1].charAt(x-1) == '*'){ |
|
|
|
|
|
neighbours++; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if(y>0){ |
|
|
|
|
|
if(grid[y-1].charAt(x) == '*'){ |
|
|
|
|
|
neighbours++; |
|
|
|
|
|
} |
|
|
|
|
|
if(x<4){ |
|
|
|
|
|
if(grid[y-1].charAt(x+1) == '*'){ |
|
|
|
|
|
neighbours++; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if(x<4){ |
|
|
|
|
|
if(grid[y].charAt(x+1) == '*'){ |
|
|
|
|
|
neighbours++; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if(y<4){ |
|
|
|
|
|
if(grid[y+1].charAt(x) == '*'){ |
|
|
|
|
|
neighbours++; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if(x<4 && y<4){ |
|
|
|
|
|
if(grid[y+1].charAt(x+1) == '*'){ |
|
|
|
|
|
neighbours++; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return neighbours; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |