Final Version of Life

This commit is contained in:
felixdennerlein 2021-12-27 22:39:03 +01:00
parent 09a995eccf
commit f11a993212
8 changed files with 196 additions and 18 deletions

10
.idea/libraries/jars.xml generated Normal file
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
.idea/misc.xml generated Normal file
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
.idea/modules.xml generated Normal file
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
.idea/runConfigurations.xml generated Normal file
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
.idea/vcs.xml generated Normal file
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
GameOfLifeAssignment.iml Normal file
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>

View File

@ -1,5 +1,12 @@
import java.util.Arrays;
public class Life implements ILife { public class Life implements ILife {
Life nextworld;
String[] board;
public static void main(String[] args) { public static void main(String[] args) {
Life l = new Life(new String[]{" ", Life l = new Life(new String[]{" ",
" ", " ",
@ -7,8 +14,8 @@ public class Life implements ILife {
" ", " ",
" "}); " "});
l = (Life) l.nextGeneration(); l = (Life) l.nextGeneration();
}
}
public Life() { public Life() {
nukeAll(); nukeAll();
@ -26,30 +33,118 @@ public class Life implements ILife {
@Override @Override
public void nukeAll() { public void nukeAll() {
// TODO Auto-generated method stub // TODO Auto-generated method stub
board = new String[]{" ",
" ",
" ",
" ",
" "};
} }
@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
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 @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
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 @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
if (board[y].charAt(x) == '*')
return true;
else
return false; return false;
} }
@Override @Override
public ILife nextGeneration() { public ILife nextGeneration() {
// TODO Auto-generated method stub // 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]
}));
}
}
}

View File

@ -22,17 +22,47 @@ public class LifeTest {
@Test @Test
public void destroyLonelyCell() { 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 @Test
public void keepAliveCell() { 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 @Test
public void destroyCrowdedCell() { 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));
}
} }