Browse Source

GameOfLifeAssignment final changes -no private repository

master
Felix Enhuber 2 years ago
parent
commit
ff5643d9c0
2 changed files with 145 additions and 17 deletions
  1. 109
    13
      src/Life.java
  2. 36
    4
      test/LifeTest.java

+ 109
- 13
src/Life.java View File

@@ -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;
}

}

+ 36
- 4
test/LifeTest.java View File

@@ -2,10 +2,10 @@ import org.junit.Test;
import static org.junit.Assert.*;

public class LifeTest {
@Test
public void createNewCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(0, 0);
@@ -13,25 +13,57 @@ public class LifeTest {
l.setAlive(0, 2);

// Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration();
l.nextGeneration();

// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
assertTrue(nextGen.isAlive(1, 1));
assertTrue(l.isAlive(1, 1));
}


@Test
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
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
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…
Cancel
Save