From ff5643d9c02dd5a0a55e71f6246fb105ec8ab89f Mon Sep 17 00:00:00 2001 From: enhuberfe85917 Date: Thu, 23 Dec 2021 21:49:39 +0100 Subject: [PATCH] GameOfLifeAssignment final changes -no private repository --- src/Life.java | 122 ++++++++++++++++++++++++++++++++++++++++----- test/LifeTest.java | 40 +++++++++++++-- 2 files changed, 145 insertions(+), 17 deletions(-) diff --git a/src/Life.java b/src/Life.java index 84ab855..d56e071 100644 --- a/src/Life.java +++ b/src/Life.java @@ -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; + } + } \ No newline at end of file diff --git a/test/LifeTest.java b/test/LifeTest.java index 3940e66..93d65b4 100644 --- a/test/LifeTest.java +++ b/test/LifeTest.java @@ -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)); + }