-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fc0f341
Showing
21 changed files
with
1,062 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>de.oc.tpd.artikel.eclipsemagazin</groupId> | ||
<artifactId>unittest-und-testbarer-code-01</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>LATEST</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-all</artifactId> | ||
<version>1.10.19</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<inherited>true</inherited> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>2.5.1</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
<encoding>UTF-8</encoding> | ||
<showWarnings>true</showWarnings> | ||
<showDeprecation>true</showDeprecation> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class Spiel { | ||
String berechneGeneration(String zellenMap) { | ||
StringBuilder neueMap = new StringBuilder(); | ||
String[] mapRows = zellenMap.split("[\r\n]+"); | ||
int rowCount = mapRows.length; | ||
int colCount = mapRows[0].length(); | ||
for (int row = 0; row < rowCount; row++) { | ||
for (int col = 0; col < colCount; col++) { | ||
int lebendeNachbarn = 0; | ||
for (int nCol = -1; nCol < 2; nCol++) | ||
for (int nRow = -1; nRow < 2; nRow++) | ||
if (!(nRow == 0 && nCol == 0)) { | ||
int rowIndex = (row + nRow + rowCount) | ||
% rowCount; | ||
int colIndex = (col + nCol + colCount) | ||
% colCount; | ||
char nachbar = mapRows[rowIndex].charAt( | ||
colIndex); | ||
if ('X' == nachbar) | ||
lebendeNachbarn++; | ||
} | ||
char alterZustand = mapRows[row].charAt(col); | ||
String neuerZustand = "."; | ||
if ('X' == alterZustand) | ||
if (2 == lebendeNachbarn || 3 == lebendeNachbarn) | ||
neuerZustand = "X"; | ||
if ('.' == alterZustand) | ||
if (3 == lebendeNachbarn) | ||
neuerZustand = "X"; | ||
neueMap.append(neuerZustand); | ||
} | ||
neueMap.append("\n"); | ||
} | ||
return neueMap.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Test; | ||
|
||
public class SpielTest { | ||
|
||
@Test | ||
public void | ||
berechneGeneration__feld5x5_3lebend__eineLebend() { | ||
Spiel spiel = new Spiel(); | ||
|
||
String neueGeneration = spiel.berechneGeneration( | ||
"" | ||
+ ".....\n" | ||
+ "..X..\n" | ||
+ ".....\n" | ||
+ ".X.X.\n" | ||
+ ".....\n"); | ||
|
||
assertEquals("eine erwaeckte Zelle", | ||
"" | ||
+ ".....\n" | ||
+ ".....\n" | ||
+ "..X..\n" | ||
+ ".....\n" | ||
+ ".....\n", | ||
neueGeneration); | ||
} | ||
|
||
@Test | ||
public void | ||
berechneGeneration__feld5x5_2lebend__alleTod() { | ||
Spiel spiel = new Spiel(); | ||
|
||
String neueGeneration = spiel.berechneGeneration( | ||
"" | ||
+ ".....\n" | ||
+ ".....\n" | ||
+ ".....\n" | ||
+ ".X.X.\n" | ||
+ ".....\n"); | ||
|
||
assertEquals("alle tot", | ||
"" | ||
+ ".....\n" | ||
+ ".....\n" | ||
+ ".....\n" | ||
+ ".....\n" | ||
+ ".....\n", | ||
neueGeneration); | ||
} | ||
|
||
@Test | ||
public void | ||
berechneGeneration__feld5x5_1lebendMit4nachbarm__stibt() { | ||
Spiel spiel = new Spiel(); | ||
|
||
String neueGeneration = spiel.berechneGeneration( | ||
"" | ||
+ ".....\n" | ||
+ ".X.X.\n" | ||
+ "..X..\n" | ||
+ ".X.X.\n" | ||
+ ".....\n"); | ||
|
||
assertEquals("alleSterben aber 4 erwaeckt", | ||
"" | ||
+ ".....\n" | ||
+ "..X..\n" | ||
+ ".X.X.\n" | ||
+ "..X..\n" | ||
+ ".....\n", | ||
neueGeneration); | ||
} | ||
|
||
@Test | ||
public void | ||
berechneGeneration__feld5x5_1lebend2Nachbarn__zweiLebend() { | ||
Spiel spiel = new Spiel(); | ||
|
||
String neueGeneration = spiel.berechneGeneration( | ||
".....\n.....\n.XX..\n...X.\n.....\n"); | ||
|
||
assertEquals("zwei lebende Zellen", | ||
".....\n.....\n..X..\n..X..\n.....\n", | ||
neueGeneration); | ||
} | ||
|
||
@Test | ||
public void | ||
berechneGeneration__feld5x5_1lebend3Nachbarn__dreiLebend() { | ||
Spiel spiel = new Spiel(); | ||
|
||
String neueGeneration = spiel.berechneGeneration( | ||
".....\n...X.\n.XX..\n...X.\n.....\n"); | ||
|
||
assertEquals("vier lebende Zellen", | ||
".....\n..X..\n..XX.\n..X..\n.....\n", | ||
neueGeneration); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>de.oc.tpd.artikel.eclipsemagazin</groupId> | ||
<artifactId>unittest-und-testbarer-code-02</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>LATEST</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-all</artifactId> | ||
<version>1.10.19</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<inherited>true</inherited> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>2.5.1</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
<encoding>UTF-8</encoding> | ||
<showWarnings>true</showWarnings> | ||
<showDeprecation>true</showDeprecation> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
public interface FeldGeometrie { | ||
|
||
void setzeKarte(String zellenMap); | ||
|
||
Iterable<ZelleMitNachbarn> zellen(); | ||
|
||
void aendereAktuellesFeld(String string); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import java.util.Iterator; | ||
|
||
public class FeldGeometrieQuadrat implements FeldGeometrie { | ||
|
||
private String zellenMap; | ||
private StringBuilder neueKarte; | ||
|
||
@Override | ||
public void setzeKarte(String zellenMap) { | ||
this.zellenMap = zellenMap; | ||
} | ||
|
||
@Override | ||
public Iterable<ZelleMitNachbarn> zellen() { | ||
neueKarte = new StringBuilder(); | ||
return new Iterable<ZelleMitNachbarn>() { | ||
@Override | ||
public Iterator<ZelleMitNachbarn> iterator() { | ||
return new ZellenIterator(zellenMap); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public void aendereAktuellesFeld(String string) { | ||
neueKarte.append(string); | ||
if ('\n' == zellenMap.charAt(neueKarte.length())) { | ||
neueKarte.append("\n"); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return neueKarte.toString(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
class Spiel { | ||
private final FeldGeometrie feldGeometrie; | ||
private ZustandsFinder zustandsFinder; | ||
|
||
public Spiel(FeldGeometrie feldGeometrie, | ||
ZustandsFinder zustandsFinder) { | ||
this.feldGeometrie = feldGeometrie; | ||
this.zustandsFinder = zustandsFinder; | ||
} | ||
|
||
public String neueGeneration(String zellenMap) { | ||
feldGeometrie.setzeKarte(zellenMap); | ||
for (ZelleMitNachbarn zelle : feldGeometrie.zellen()) { | ||
String neuerZustand = neuerZustand(zelle); | ||
feldGeometrie.aendereAktuellesFeld(neuerZustand); | ||
} | ||
return feldGeometrie.toString(); | ||
} | ||
|
||
private String neuerZustand(ZelleMitNachbarn zelle) { | ||
List<Zustand> nachbarn = nachbarZustaende(zelle); | ||
Zustand alterZustand = zustandsFinder.find( | ||
zelle.zustand()); | ||
Zustand neuerZustand = alterZustand.aendern(nachbarn); | ||
return neuerZustand.toString(); | ||
} | ||
|
||
private List<Zustand> nachbarZustaende( | ||
ZelleMitNachbarn zelle) { | ||
List<String> nachbarn = zelle.nachbarn(); | ||
List<Zustand> nachbarZustaende = new ArrayList<>(); | ||
for (String nachbarZustand : nachbarn) { | ||
nachbarZustaende.add(zustandsFinder.find( | ||
nachbarZustand)); | ||
} | ||
return nachbarZustaende; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import java.util.List; | ||
|
||
public class ZelleMitNachbarn { | ||
|
||
private final String statusString; | ||
private final List<String> nachbarn; | ||
|
||
public ZelleMitNachbarn(String statusString, List<String> nachbarn) { | ||
this.statusString = statusString; | ||
this.nachbarn = nachbarn; | ||
} | ||
|
||
public String zustand() { | ||
return statusString; | ||
} | ||
|
||
public List<String> nachbarn() { | ||
return nachbarn; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
class ZellenIterator implements | ||
Iterator<ZelleMitNachbarn> { | ||
final String[] rows; | ||
int row = 0; | ||
int col = 0; | ||
|
||
public ZellenIterator(String zellenMap) { | ||
rows = zellenMap.split("[\r\n]+"); | ||
} | ||
|
||
@Override | ||
public ZelleMitNachbarn next() { | ||
List<String> nachbarn = new ArrayList<>(); | ||
for (int iRow = -1; iRow < 2; iRow++) | ||
for (int iCol = -1; iCol < 2; iCol++) | ||
if (!(iRow == 0 && iCol == 0)) { | ||
int rowIndex = calulateIndex(row, iRow, | ||
rows.length); | ||
int colIndex = calulateIndex(col, iCol, | ||
rows[0].length()); | ||
nachbarn.add(String.valueOf( | ||
rows[rowIndex].charAt(colIndex))); | ||
} | ||
ZelleMitNachbarn zelleMitNachban = new ZelleMitNachbarn( | ||
String.valueOf(rows[row].charAt(col)), | ||
nachbarn); | ||
col++; | ||
if (0 == col % rows[0].length()) { | ||
col = 0; | ||
row++; | ||
} | ||
return zelleMitNachban; | ||
} | ||
|
||
private int calulateIndex(int iteratorIndex, | ||
int nachbarRelativIndex, | ||
int anzahlInRichtung) { | ||
return (iteratorIndex - nachbarRelativIndex | ||
+ anzahlInRichtung) % anzahlInRichtung; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return row < rows.length && col < rows[row].length(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import java.util.List; | ||
|
||
public interface Zustand { | ||
|
||
Zustand aendern(List<Zustand> nachbarZustände); | ||
|
||
} |
Oops, something went wrong.