Browse Source

Final Version of Life

master
felixdennerlein 1 year ago
parent
commit
f11a993212
8 changed files with 192 additions and 14 deletions
  1. 10
    0
      .idea/libraries/jars.xml
  2. 6
    0
      .idea/misc.xml
  3. 8
    0
      .idea/modules.xml
  4. 10
    0
      .idea/runConfigurations.xml
  5. 6
    0
      .idea/vcs.xml
  6. 13
    0
      GameOfLifeAssignment.iml
  7. 106
    11
      src/Life.java
  8. 33
    3
      test/LifeTest.java

+ 10
- 0
.idea/libraries/jars.xml View File

@@ -0,0 +1,10 @@
<component name="libraryTable">
<library name="jars">
<CLASSES>
<root url="jar://$PROJECT_DIR$/jars/junit-4.12.jar!/" />
<root url="jar://$PROJECT_DIR$/jars/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

+ 6
- 0
.idea/misc.xml View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

+ 8
- 0
.idea/modules.xml View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/GameOfLifeAssignment.iml" filepath="$PROJECT_DIR$/GameOfLifeAssignment.iml" />
</modules>
</component>
</project>

+ 10
- 0
.idea/runConfigurations.xml View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>

+ 6
- 0
.idea/vcs.xml View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

+ 13
- 0
GameOfLifeAssignment.iml View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="jars" level="project" />
</component>
</module>

+ 106
- 11
src/Life.java View File

@@ -1,14 +1,21 @@
import java.util.Arrays;

public class Life implements ILife {


Life nextworld;
String[] board;


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

}

public Life() {
nukeAll();
@@ -26,30 +33,118 @@ public class Life implements ILife {
@Override
public void nukeAll() {
// TODO Auto-generated method stub

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

@Override
public void setAlive(int x, int y) {
// TODO Auto-generated method stub
String line = "";
for (int i = 0; i < board[y].length(); i++) {
if (i == x)
line += '*';
else
line += board[y].charAt(i);
}
board[y] = line;


}

@Override
public void setDead(int x, int y) {
// TODO Auto-generated method stub
String line = "";
for (int i = 0; i < board[y].length(); i++) {
if (i == x)
line += ' ';
else
line += board[y].charAt(i);
}
board[y] = line;


}

@Override
public boolean isAlive(int x, int y) {
// TODO Auto-generated method stub
return false;
if (board[y].charAt(x) == '*')
return true;
else
return false;
}

@Override
public ILife nextGeneration() {
// TODO Auto-generated method stub
return null;
Life nextWorld = new Life();
for (int y = 0; y < board.length; y++) {
for (int x = 0; x < board[y].length(); x++) {
int neighbourCells = CountNeighbourCells(x, y);
if (isAlive(x, y))
if (neighbourCells == 3 || neighbourCells == 2)
nextWorld.setAlive(x, y);
if (neighbourCells < 2 || neighbourCells > 3)
nextWorld.setDead(x, y);
if (!isAlive(x, y))
if (neighbourCells == 3)
nextWorld.setAlive(x, y);
else
nextWorld.setDead(x, y);
}
}
nextWorld.drawWorld();
return nextWorld;
}
}

public int CountNeighbourCells(int x, int y) {
int pre_y = y - 1;
int pre_x = x - 1;
int after_y = y + 1;
int after_x = x + 1;
int counter = 0;
//UpperSide
if (pre_y >= 0) {
for (int i = pre_x; i <= after_x; i++) {
if (i >= 0 && i < board[pre_y].length()&& isAlive(i,pre_y)){
counter++;
}
}
}
//DownSide
if (after_y < board.length) {
for (int i = pre_x; i <= after_x; i++) {
if (i >= 0 && i < board[after_y].length()&&isAlive(i,after_y)) {
counter++;
}
}
}
//leftSide
if (pre_x >= 0&& isAlive(pre_x,y)) {
counter++;
}

//rightSide
if (after_x < board[y].length()&&isAlive(after_x,y)) {
counter++;
}
return counter;
}


public void drawWorld() {
for (int i = 0; i < board.length; i++) {
System.out.println(Arrays.toString(new String[]{
board[i]
}));

}
}
}



+ 33
- 3
test/LifeTest.java View File

@@ -22,17 +22,47 @@ public class LifeTest {

@Test
public void destroyLonelyCell() {
}
Life l = new Life();
l.setAlive(0, 0);
l.setAlive(0, 1);
l.setAlive(0, 2);

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

// Assert: Rasterpunkt mit einem Nachbar sollte jetzt sterben
assertFalse(nextGen.isAlive(0, 2));
}

@Test
public void keepAliveCell() {
}
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(0, 0);
l.setAlive(0, 1);
l.setAlive(0, 2);

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

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

@Test
public void destroyCrowdedCell() {
}
// Arrange: fünf lebende Zellen
Life l = new Life();
l.setAlive(0, 0);
l.setAlive(0, 1);
l.setAlive(0, 2);
l.setAlive(1, 1);
l.setAlive(1, 0);

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

// Assert: Rasterpunkt mit vier Nachbarn sollte jetzt sterben
assertFalse(nextGen.isAlive(0, 1));
}
}

Loading…
Cancel
Save