|
|
@@ -1,11 +1,15 @@ |
|
|
|
import java.util.Arrays; |
|
|
|
|
|
|
|
public class Life implements ILife { |
|
|
|
|
|
|
|
|
|
|
|
static String[] grid = new String[]{ "* ", |
|
|
|
"* ", |
|
|
|
"* ", |
|
|
|
" ", |
|
|
|
" " }; |
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
Life l = new Life(new String[] { " ", |
|
|
|
" ", |
|
|
|
" *** ", |
|
|
|
" ", |
|
|
|
" " }); |
|
|
|
Life l = new Life(grid); |
|
|
|
l = (Life) l.nextGeneration(); |
|
|
|
} |
|
|
|
|
|
|
@@ -16,40 +20,132 @@ public class Life implements ILife { |
|
|
|
|
|
|
|
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) != ' ') |
|
|
|
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); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
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 |
|
|
|
public void nukeAll() { |
|
|
|
// 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 |
|
|
|
public void setAlive(int x, int y) { |
|
|
|
// TODO Auto-generated method stub |
|
|
|
|
|
|
|
grid[y] = grid[y].substring(0, x) + '*' + grid[y].substring(x+1); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void setDead(int x, int y) { |
|
|
|
// TODO Auto-generated method stub |
|
|
|
|
|
|
|
grid[y] = grid[y].substring(0, x) + ' ' + grid[y].substring(x+1); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public boolean isAlive(int x, int y) { |
|
|
|
// TODO Auto-generated method stub |
|
|
|
return false; |
|
|
|
if (grid[y].charAt(x) == '*'){ |
|
|
|
return true; |
|
|
|
} else { |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public ILife nextGeneration() { |
|
|
|
// 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; |
|
|
|
} |
|
|
|
|
|
|
|
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; |
|
|
|
} |
|
|
|
|
|
|
|
} |