|
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays; |
|
|
|
|
|
|
|
|
public class Life implements ILife { |
|
|
public class Life implements ILife { |
|
|
|
|
|
|
|
|
|
|
|
String[] lifeArray; |
|
|
|
|
|
|
|
|
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 = (Life) l.nextGeneration(); |
|
|
l = (Life) l.nextGeneration(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Life() { |
|
|
|
|
|
nukeAll(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public Life() { nukeAll(); } |
|
|
|
|
|
|
|
|
public Life(String[] setup) { |
|
|
public Life(String[] setup) { |
|
|
this(); |
|
|
this(); |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void nukeAll() { |
|
|
public void nukeAll() { |
|
|
// TODO Auto-generated method stub |
|
|
// TODO Auto-generated method stub |
|
|
|
|
|
|
|
|
|
|
|
lifeArray = new String[] { " ", |
|
|
|
|
|
" ", |
|
|
|
|
|
" ", |
|
|
|
|
|
" ", |
|
|
|
|
|
" "}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@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 |
|
|
|
|
|
String newString = ""; |
|
|
|
|
|
for(int i = 0; i < lifeArray[y].length(); i++) { |
|
|
|
|
|
if(i == x) { |
|
|
|
|
|
newString += '*'; |
|
|
|
|
|
} else { |
|
|
|
|
|
newString += lifeArray[y].charAt(i); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
lifeArray[y] = newString; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@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 |
|
|
|
|
|
String newString = ""; |
|
|
|
|
|
for(int i = 0; i < lifeArray[y].length(); i++) { |
|
|
|
|
|
if(i == x) { |
|
|
|
|
|
newString += ' '; |
|
|
|
|
|
} else { |
|
|
|
|
|
newString += lifeArray[y].charAt(i); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
lifeArray[y] = newString; |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@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 |
|
|
|
|
|
if(lifeArray[y].charAt(x) == '*') |
|
|
|
|
|
return true; |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public ILife nextGeneration() { |
|
|
public ILife nextGeneration() { |
|
|
// TODO Auto-generated method stub |
|
|
// TODO Auto-generated method stub |
|
|
return null; |
|
|
|
|
|
|
|
|
Life newWorld = new Life(); |
|
|
|
|
|
for(int y = 0; y < lifeArray.length; y++) { |
|
|
|
|
|
for(int x = 0; x < lifeArray[y].length(); x++) { |
|
|
|
|
|
int counter = countNeighbors(x, y); |
|
|
|
|
|
|
|
|
|
|
|
if(isAlive(x, y)) { |
|
|
|
|
|
if (counter > 3 || counter < 2) |
|
|
|
|
|
newWorld.setDead(x, y); |
|
|
|
|
|
if (counter == 3 || counter == 2) |
|
|
|
|
|
newWorld.setAlive(x, y); |
|
|
|
|
|
} |
|
|
|
|
|
if(!isAlive(x, y)) { |
|
|
|
|
|
if (counter == 3) { |
|
|
|
|
|
newWorld.setAlive(x, y); |
|
|
|
|
|
} else { |
|
|
|
|
|
newWorld.setDead(x, y); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return newWorld; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private int countNeighbors(int x, int y) { |
|
|
|
|
|
int counter = 0; |
|
|
|
|
|
|
|
|
|
|
|
//count three above |
|
|
|
|
|
counter += countAbove(x, y); |
|
|
|
|
|
|
|
|
|
|
|
//count three below |
|
|
|
|
|
counter += countBelow(x, y); |
|
|
|
|
|
|
|
|
|
|
|
//count two sides |
|
|
|
|
|
counter += countSides(x, y); |
|
|
|
|
|
|
|
|
|
|
|
return counter; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private int countSides(int x, int y) { |
|
|
|
|
|
int counter = 0; |
|
|
|
|
|
if(x-1 >= 0) |
|
|
|
|
|
if(isAlive(x-1, y)) |
|
|
|
|
|
counter++; |
|
|
|
|
|
if(x+1 < lifeArray[y].length()) |
|
|
|
|
|
if(isAlive(x+1, y)) |
|
|
|
|
|
counter++; |
|
|
|
|
|
return counter; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private int countBelow(int x, int y) { |
|
|
|
|
|
int counter = 0; |
|
|
|
|
|
if(y+1 < lifeArray.length) { |
|
|
|
|
|
for (int i = x-1; i <= x+1; i++) { |
|
|
|
|
|
if (i >= 0 && i < lifeArray[y+1].length()) |
|
|
|
|
|
if(isAlive(i, y + 1)) |
|
|
|
|
|
counter++; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return counter; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private int countAbove(int x, int y) { |
|
|
|
|
|
int counter = 0; |
|
|
|
|
|
if(y-1 >= 0) { |
|
|
|
|
|
for (int i = x-1; i <= x+1; i++) { |
|
|
|
|
|
if (i >= 0 && i < lifeArray[y-1].length()) |
|
|
|
|
|
if(isAlive(i, y - 1)) |
|
|
|
|
|
counter++; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return counter; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void showBoard() { |
|
|
|
|
|
for(int y = 0; y < lifeArray.length-1; y++) |
|
|
|
|
|
System.out.println(Arrays.toString(new String[] {lifeArray[y]})); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |