Browse Source

LifeTest first step passed

master
Moritz Neumeier 2 years ago
parent
commit
fb34811053
3 changed files with 258 additions and 57 deletions
  1. 139
    54
      src/Life.java
  2. 113
    0
      src/Simulation.java
  3. 6
    3
      test/LifeTest.java

+ 139
- 54
src/Life.java View File

@@ -1,55 +1,140 @@
import java.util.Arrays;

public class Life implements ILife {
public static void main(String[] args) {
Life l = new Life(new String[] { " ",
" ",
" *** ",
" ",
" " });
l = (Life) l.nextGeneration();
}


public Life() {
nukeAll();
}

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) != ' ')
setAlive(x, y);
}


@Override
public void nukeAll() {
// TODO Auto-generated method stub

}

@Override
public void setAlive(int x, int y) {
// TODO Auto-generated method stub

}

@Override
public void setDead(int x, int y) {
// TODO Auto-generated method stub

}

@Override
public boolean isAlive(int x, int y) {
// TODO Auto-generated method stub
return false;
}

@Override
public ILife nextGeneration() {
// TODO Auto-generated method stub
return null;
}
}

static String[] board = new String[]{ "* ",
"* ",
"* ",
" ",
" " };

public static void main(String[] args) {
Life l = new Life(board);
l = (Life) l.nextGeneration();
}


public Life() {
//nukeAll();
}

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) != ' '){
setAlive(x, y);
}
}
//System.out.println(board[y]);
}
printTable(board);
}

private void printTable(String[] board) { //works
for(int i = 0; i < board.length; i++) System.out.println(Arrays.toString(new String[]{board[i]}));
System.out.println("________");
}


@Override
public void nukeAll() { //works
// TODO Auto-generated method stub
for (int y = 0; y < board.length; y++){
for (int x = 0; x < board[y].length(); x++) {
setDead(x, y);
}
}

}

@Override
public void setAlive(int x, int y) { //works
// TODO Auto-generated method stub
board[y] = board[y].substring(0, x) + '*' + board[y].substring(x+1);
}

@Override
public void setDead(int x, int y) { //works
// TODO Auto-generated method stub
board[y] = board[y].substring(0, x) + ' ' + board[y].substring(x+1);
}

@Override
public boolean isAlive(int x, int y) { //works
//char c = ;
if(board[y].charAt(x) == '*'){ //-->Befehl falsch immer false!!!
return true;
}
else{
return false;
}
}

@Override
public ILife nextGeneration() {
System.out.println("next Generation");
int alive;

for (int y = 0; y < board.length; y++){
for (int x = 0; x < board[y].length(); x++){
alive=aliveNeighbours(x,y); //Problem immer 0

if(!isAlive(x,y) && alive == 3) {
setAlive(x, y);
}
}
}
printTable(board);
return null;
}

private int aliveNeighbours(int x, int y) { //-->Problem wahrscheinlich Befehl substring siehe isAlive
int neighbours = 0;

if(x>0 && y>0){
if(board[y-1].charAt(x-1) == '*'){
neighbours++;
}
}
if(x>0){
if(board[y].charAt(x-1) == '*'){
neighbours++;
}
if(y<4 ){
if(board[y+1].charAt(x-1) == '*'){
neighbours++;
}
}
}

if(y>0){
if(board[y-1].charAt(x) == '*'){
neighbours++;
}
if(x<4){
if(board[y-1].charAt(x+1) == '*'){
neighbours++;
}
}
}

if(x<4){
if(board[y].charAt(x+1) == '*'){
neighbours++;
}
}
if(y<4){
if(board[y+1].charAt(x) == '*'){
neighbours++;
}
}
if(x<4 && y<4){
if(board[y+1].charAt(x+1) == '*'){
neighbours++;
}
}
return neighbours;
}
}


+ 113
- 0
src/Simulation.java View File

@@ -0,0 +1,113 @@
public class Simulation {

int width;
int height;
int[][] board;

public Simulation(int width, int height) {
this.width = width;
this.height = height;
this.board = new int[width][height];
}

public void printBoard() {
System.out.println("---");
for (int y = 0; y < height; y++) {
String line = "|";
for (int x = 0; x < width; x++) {
if (this.board[x][y] == 0) {
line += ".";
} else {
line += "*";
}
}
line += "|";
System.out.println(line);
}
System.out.println("---\n");
}

public void setAlive(int x, int y) {
this.board[x][y] = 1;
}

public void setDead(int x, int y) {
this.board[x][y] = 0;
}

public int countAliveNeighbours(int x, int y) {
int count = 0;

count += getState(x - 1, y - 1);
count += getState(x, y - 1);
count += getState(x + 1, y - 1);

count += getState(x - 1, y);
count += getState(x + 1, y);

count += getState(x - 1, y + 1);
count += getState(x, y + 1);
count += getState(x + 1, y + 1);

return count;
}

public int getState(int x, int y) {
if (x < 0 || x >= width) {
return 0;
}

if (y < 0 || y >= height) {
return 0;
}

return this.board[x][y];
}

public void step() {
int[][] newBoard = new int[width][height];

for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int aliveNeighbours = countAliveNeighbours(x, y);

if (getState(x, y) == 1) {
if (aliveNeighbours < 2) {
newBoard[x][y] = 0;
} else if (aliveNeighbours == 2 || aliveNeighbours == 3) {
newBoard[x][y] = 1;
} else if (aliveNeighbours > 3) {
newBoard[x][y] = 0;
}
} else {
if (aliveNeighbours == 3) {
newBoard[x][y] = 1;
}
}

}
}

this.board = newBoard;
}

public static void main(String[] args) {
Simulation simulation = new Simulation(8, 5);

simulation.setAlive(2, 2);
simulation.setAlive(3, 2);
simulation.setAlive(4, 2);

simulation.printBoard();

simulation.step();

simulation.printBoard();

simulation.step();

simulation.printBoard();

}

}

+ 6
- 3
test/LifeTest.java View File

@@ -5,7 +5,8 @@ public class LifeTest {
@Test
public void createNewCell() {


// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(0, 0);
@@ -13,10 +14,12 @@ 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));


}



Loading…
Cancel
Save