<component name="libraryTable"> | |||||
<library name="jars"> | |||||
<CLASSES> | |||||
<root url="jar://$PROJECT_DIR$/jars/hamcrest-core-1.3.jar!/" /> | |||||
<root url="jar://$PROJECT_DIR$/jars/junit-4.12.jar!/" /> | |||||
</CLASSES> | |||||
<JAVADOC /> | |||||
<SOURCES /> | |||||
</library> | |||||
</component> |
<?xml version="1.0" encoding="UTF-8"?> | |||||
<project version="4"> | |||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="openjdk-17" project-jdk-type="JavaSDK"> | |||||
<output url="file://$PROJECT_DIR$/out" /> | |||||
</component> | |||||
</project> |
<?xml version="1.0" encoding="UTF-8"?> | |||||
<project version="4"> | |||||
<component name="ProjectModuleManager"> | |||||
<modules> | |||||
<module fileurl="file://$PROJECT_DIR$/GameOfLifeAssignmentNew.iml" filepath="$PROJECT_DIR$/GameOfLifeAssignmentNew.iml" /> | |||||
</modules> | |||||
</component> | |||||
</project> |
<?xml version="1.0" encoding="UTF-8"?> | |||||
<project version="4"> | |||||
<component name="VcsDirectoryMappings"> | |||||
<mapping directory="" vcs="Git" /> | |||||
</component> | |||||
</project> |
<?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> |
//Bearbeitet von: David Papac | |||||
import java.util.Arrays; | |||||
public class Life implements ILife { | public class Life implements ILife { | ||||
String[] gameWorld; | |||||
public static void main(String[] args) { | public static void main(String[] args) { | ||||
Life l = new Life(new String[] { " ", | |||||
" ", | |||||
" *** ", | |||||
" ", | |||||
" " }); | |||||
Life l = new Life( new String[] { " ", | |||||
" ", | |||||
" *** ", | |||||
" ", | |||||
" "}); | |||||
l = (Life) l.nextGeneration(); | l = (Life) l.nextGeneration(); | ||||
} | |||||
} | |||||
public Life() { | public Life() { | ||||
nukeAll(); | nukeAll(); | ||||
} | } | ||||
public Life(String[] setup) { | public Life(String[] setup) { | ||||
for (int x = 0; x < setup[y].length(); x++) | for (int x = 0; x < setup[y].length(); x++) | ||||
if (setup[y].charAt(x) != ' ') | if (setup[y].charAt(x) != ' ') | ||||
setAlive(x, y); | setAlive(x, y); | ||||
} | } | ||||
@Override | @Override | ||||
public void nukeAll() { | public void nukeAll() { | ||||
// TODO Auto-generated method stub | // TODO Auto-generated method stub | ||||
gameWorld = 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 newString =""; | |||||
for(int i=0; i< gameWorld[y].length(); i++){ | |||||
if(i==x) | |||||
newString +='*'; | |||||
else | |||||
newString += gameWorld[y].charAt(i); | |||||
} | |||||
gameWorld[y]=newString; | |||||
} | } | ||||
@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 newString =""; | |||||
for(int i=0; i< gameWorld[y].length(); i++){ | |||||
if(i==x) | |||||
newString +=' '; | |||||
else | |||||
newString += gameWorld[y].charAt(i); | |||||
} | |||||
gameWorld[y]=newString; | |||||
} | } | ||||
@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 | ||||
return false; | |||||
} | |||||
if(gameWorld[y].charAt(x) =='*'){ | |||||
return true;} | |||||
else{ | |||||
return false; | |||||
}} | |||||
@Override | @Override | ||||
public ILife nextGeneration() { | public ILife nextGeneration() { | ||||
// TODO Auto-generated method stub | // TODO Auto-generated method stub | ||||
return null; | |||||
Life newWorld = new Life(); | |||||
for(int y=0; y< gameWorld.length;y++){ | |||||
for(int x= 0; x <gameWorld[y].length(); x++){ | |||||
int neighbor = countNeighbor(x, y); | |||||
if(isAlive(x,y)) { | |||||
if (neighbor == 3 || neighbor == 2) { | |||||
newWorld.setAlive(x, y); | |||||
} | |||||
if (neighbor < 2 || neighbor > 3) { | |||||
newWorld.setDead(x, y); | |||||
} | |||||
} | |||||
if(!isAlive(x,y)) { | |||||
if (neighbor == 3) { | |||||
newWorld.setAlive(x, y); | |||||
} else { | |||||
newWorld.setDead(x, y); | |||||
} | |||||
} | |||||
} | |||||
} | |||||
newWorld.showBoard(); | |||||
return newWorld; | |||||
} | |||||
public int countNeighbor(int x, int y) { | |||||
int counter=0; | |||||
//counts top row | |||||
if(y-1 >=0){ | |||||
for(int i =x-1; i<= x+1;i++){ | |||||
if(i>=0 && i< gameWorld[y-1].length()){ | |||||
if(isAlive(i,y-1)){ | |||||
counter++; | |||||
} | |||||
} | |||||
} | |||||
} | |||||
//counts bottom row | |||||
if(y+1 <gameWorld.length){ | |||||
for(int i =x-1; i<= x+1;i++){ | |||||
if(i>=0 && i< gameWorld[y+1].length()){ | |||||
if(isAlive(i,y+1)){ | |||||
counter++; | |||||
} | |||||
} | |||||
} | |||||
} | |||||
//counts sides | |||||
if(x-1>=0){ | |||||
if(isAlive(x-1,y)){ | |||||
counter++; | |||||
} | |||||
} | |||||
if( x+1< gameWorld[y].length()){ | |||||
if(isAlive(x+1,y)){ | |||||
counter++; | |||||
} | |||||
} | |||||
return counter; | |||||
} | |||||
public void showBoard() { | |||||
for (int i = 0; i < gameWorld.length; i++) | |||||
System.out.println(Arrays.toString(new String[]{gameWorld[i]})); | |||||
} | } | ||||
} | } |
//Bearbeitet von: David Papac | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import static org.junit.Assert.*; | import static org.junit.Assert.*; | ||||
public class LifeTest { | public class LifeTest { | ||||
@Test | |||||
public void nuke() { | |||||
// Arrange: drei lebende Zellen | |||||
Life l = new Life(); | |||||
l.setAlive(0, 0); | |||||
l.setAlive(0, 1); | |||||
l.setAlive(0, 2); | |||||
// Act: Alles wird ausgelöscht | |||||
l.nukeAll(); | |||||
// Assert: Alle Rasterpunkt sterben | |||||
assertFalse(l.isAlive(0, 1)); | |||||
} | |||||
@Test | @Test | ||||
public void createNewCell() { | public void createNewCell() { | ||||
// Arrange: drei lebende Zellen | // Arrange: drei lebende Zellen | ||||
Life l = new Life(); | Life l = new Life(); | ||||
l.setAlive(0, 0); | l.setAlive(0, 0); | ||||
@Test | @Test | ||||
public void destroyLonelyCell() { | public void destroyLonelyCell() { | ||||
// 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 weniger als zwei Nachbarn soll 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 mindestens zwei aber weniger als vier Nachbarn soll 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 mehr als drei Nachbarn soll sterben | |||||
assertFalse(nextGen.isAlive(0, 1)); | |||||
} | } | ||||
} | |||||
} |