GameOfLifeAssignment final changes -no private repository
This commit is contained in:
parent
09a995eccf
commit
ff5643d9c0
122
src/Life.java
122
src/Life.java
@ -1,11 +1,15 @@
|
|||||||
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -16,40 +20,132 @@ public class Life implements ILife {
|
|||||||
|
|
||||||
public Life(String[] setup) {
|
public Life(String[] setup) {
|
||||||
this();
|
this();
|
||||||
for (int y = 0; y < setup.length; y++)
|
for (int y = 0; y < setup.length; y++) {
|
||||||
for (int x = 0; x < setup[y].length(); x++)
|
for (int x = 0; x < setup[y].length(); x++) {
|
||||||
if (setup[y].charAt(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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -2,10 +2,10 @@ import org.junit.Test;
|
|||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class LifeTest {
|
public class LifeTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createNewCell() {
|
public void createNewCell() {
|
||||||
|
|
||||||
// Arrange: drei lebende Zellen
|
// Arrange: drei lebende Zellen
|
||||||
Life l = new Life();
|
Life l = new Life();
|
||||||
l.setAlive(0, 0);
|
l.setAlive(0, 0);
|
||||||
@ -13,25 +13,57 @@ public class LifeTest {
|
|||||||
l.setAlive(0, 2);
|
l.setAlive(0, 2);
|
||||||
|
|
||||||
// Act: Berechnung der Folgegeneration
|
// Act: Berechnung der Folgegeneration
|
||||||
ILife nextGen = l.nextGeneration();
|
l.nextGeneration();
|
||||||
|
|
||||||
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
|
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
|
||||||
assertTrue(nextGen.isAlive(1, 1));
|
assertTrue(l.isAlive(1, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void destroyLonelyCell() {
|
public void destroyLonelyCell() {
|
||||||
|
|
||||||
|
Life l = new Life();
|
||||||
|
l.setAlive(0, 0);
|
||||||
|
l.setAlive(0, 1);
|
||||||
|
l.setAlive(0, 2);
|
||||||
|
|
||||||
|
l.nextGeneration();
|
||||||
|
|
||||||
|
assertFalse(l.isAlive(0, 0));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void keepAliveCell() {
|
public void keepAliveCell() {
|
||||||
|
|
||||||
|
Life l = new Life();
|
||||||
|
l.setAlive(0, 0);
|
||||||
|
l.setAlive(0, 1);
|
||||||
|
l.setAlive(0, 2);
|
||||||
|
|
||||||
|
l.nextGeneration();
|
||||||
|
|
||||||
|
assertTrue(l.isAlive(0, 1));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void destroyCrowdedCell() {
|
public void destroyCrowdedCell() {
|
||||||
|
|
||||||
|
Life l = new Life();
|
||||||
|
l.setAlive(0, 0);
|
||||||
|
l.setAlive(0, 1);
|
||||||
|
l.setAlive(0, 2);
|
||||||
|
l.setAlive(1, 1);
|
||||||
|
l.setAlive(1, 0);
|
||||||
|
|
||||||
|
l.nextGeneration();
|
||||||
|
|
||||||
|
assertFalse(l.isAlive(0, 1));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user