27 Commits

Author SHA1 Message Date
Julius Dewender 4ca6b4766a initial placement white rook a1 9 months ago
Julius Dewender c827f07f88 moved newGameInitializationFigurePositionsRook2 into newGameInitializationFigurePositionsRooks 9 months ago
Julius Dewender d0939f3cca transformed newGameInitializationFigurePositionsRooks to ParameterizedTest 9 months ago
Julius Dewender b8a1cbb7c8 initial placement black rook h8 9 months ago
Julius Dewender 95af987307 initial placement black rook a8 9 months ago
Julius Dewender b5aa9415f3 renamed test farmerInvalidDestination 9 months ago
Julius Dewender 7734e50a43 added test for invalid movement of black farmer 9 months ago
Julius Dewender d74706ca41 renamed tests 9 months ago
Julius Dewender 7c78433703 added test for valid movement of black farmer 9 months ago
Julius Dewender 8a499a6489 moved checkMoveWithValidDestinationVar2 into whiteFarmerValidDestination 9 months ago
Julius Dewender db2724722a moved whiteFarmerInvalidDestinationDownVar2 into whiteFarmerInvalidDestinationDown 9 months ago
Julius Dewender edbd9c568c refactored test whiteFarmerInvalidDestinationDown to ParameterizedTest 9 months ago
Julius Dewender 0bda50fef8 refactored test whiteFarmerValidDestination to ParameterizedTest 9 months ago
Julius Dewender 975d2dd3e6 extracted magic numbers 9 months ago
Julius Dewender 030bed6414 optimized annotations 9 months ago
Julius Dewender cc784e9094 refactored magic numbers 9 months ago
Julius Dewender df6c42c806 test initial white farmer move 9 months ago
Julius Dewender 86da9de4c3 corrected wrong row constants 9 months ago
Julius Dewender de43590ca1 refactored color type 9 months ago
Julius Dewender 00b04d6bca added different FigurFarmer colors 9 months ago
Julius Dewender cf9cb03b15 test farmer initial placement 9 months ago
Julius Dewender 2e025f35ad corrected test newGameInitializationFigurePositions 9 months ago
Julius Dewender d8918c6790 removed magic numbers 9 months ago
Julius Dewender a9337af708 initial figure placement on field 9 months ago
Julius Dewender 40501a27b0 removed magic numbers 9 months ago
Julius Dewender 3de907b77e initial creation of "Game" class 9 months ago
Julius Dewender 5d52b6b876 refactored moveAllowed() 9 months ago
  1. 4
      fh.fd.ci.client/src/main/java/de/fd/fh/Figure.java
  2. 51
      fh.fd.ci.client/src/main/java/de/fd/fh/FigureFarmer.java
  3. 5
      fh.fd.ci.client/src/main/java/de/fd/fh/FigureRook.java
  4. 58
      fh.fd.ci.client/src/main/java/de/fd/fh/Game.java
  5. 80
      fh.fd.ci.client/src/test/java/de/fd/fh/FigureFarmerTest.java
  6. 24
      fh.fd.ci.client/src/test/java/de/fd/fh/FigureRookTest.java
  7. 16
      fh.fd.ci.client/src/test/java/de/fd/fh/FigureTest.java
  8. 107
      fh.fd.ci.client/src/test/java/de/fd/fh/GameTest.java

4
fh.fd.ci.client/src/main/java/de/fd/fh/Figure.java

@ -4,7 +4,7 @@ public class Figure
{
public static final int fieldLength = 8;
public boolean moveAllowed(int src, int dst, int[] field)
public boolean moveAllowed(int src, int dst, Figure[] field)
{
if (src == dst)
{
@ -16,6 +16,8 @@ public class Figure
return false;
}
// TODO: im Falle von Schach muss Zug Situation auflösen, sonst nicht möglich
return true;
}
}

51
fh.fd.ci.client/src/main/java/de/fd/fh/FigureFarmer.java

@ -2,21 +2,66 @@ package de.fd.fh;
public class FigureFarmer extends Figure
{
enum Color { Black, White};
public Color mColor;
public FigureFarmer(Color colorCode)
{
mColor = colorCode;
}
@Override
public boolean moveAllowed(int src, int dst, int[] field)
public boolean moveAllowed(int src, int dst, Figure[] field)
{
int rowDiff = src / fieldLength - dst / fieldLength; // hoch/runter
if (rowDiff > 1) // weißer Bauer nicht mehr als 1 Schritt nach "oben"
if (mColor == Color.Black)
{
rowDiff *= -1;
}
if (rowDiff > 1) // Bauer nicht mehr als 1 Schritt
{
return false;
}
if (rowDiff <= 0) // weißer Bauer kein Schritt "runter"
if (rowDiff <= 0) // Bauer nur Schritte in erlaubte Richtung (weiß -> hoch, schwarz -> runter)
{
return false;
}
// TODO: nur ein Schritt nach oben, wenn auf Ziel keine Figur
// TODO: links, rechts verboten
// TODO: Diagonal links/rechts hoch, wenn Figur geschlagen werden kann (Figur auf Ziel)
return super.moveAllowed(src, dst, field);
}
@Override
public boolean equals(Object o)
{
if (o == null)
{
return false;
}
if (this == o)
{
return true;
}
if (this.getClass() != o.getClass())
{
return false;
}
FigureFarmer otherFigure = (FigureFarmer)o;
if (this.mColor != otherFigure.mColor)
{
return false;
}
return true;
}
}

5
fh.fd.ci.client/src/main/java/de/fd/fh/FigureRook.java

@ -3,7 +3,7 @@ package de.fd.fh;
public class FigureRook extends Figure
{
@Override
public boolean moveAllowed(int src, int dst, int[] field)
public boolean moveAllowed(int src, int dst, Figure[] field)
{
int rowDiff = src / fieldLength - dst / fieldLength; // hoch/runter
int relStep = src % fieldLength - dst % fieldLength; // links//rechts
@ -28,6 +28,9 @@ public class FigureRook extends Figure
return false;
}
// TODO: keine Figuren auf dem Weg "überspringen"
// TODO: falls Figur auf Ziel und keine Figuren auf Weg -> Figur schlagen
return super.moveAllowed(src, dst, field);
}
}

58
fh.fd.ci.client/src/main/java/de/fd/fh/Game.java

@ -0,0 +1,58 @@
package de.fd.fh;
public class Game
{
public Figure[] mField;
public static final int whiteRowOther = 7;
public static final int whiteRowFarmer = 6;
public static final int blackRowOther = 0;
public static final int blackRowFarmer = 1;
public void initNewGame()
{
mField = new Figure[Figure.fieldLength * Figure.fieldLength];
// Figuren platzieren
for (int i = 0; i < Figure.fieldLength; i++) // row
{
if (i == Game.whiteRowOther
|| i == Game.blackRowOther)
{
for (int j = 0; j < Figure.fieldLength; j++) // col
{
mField[i * Figure.fieldLength + j] = new Figure();
}
}
for (int j = 0; j < Figure.fieldLength; j++) // col
{
mField[Game.whiteRowFarmer * Figure.fieldLength + j] = new FigureFarmer(FigureFarmer.Color.White);
}
for (int j = 0; j < Figure.fieldLength; j++) // col
{
mField[Game.blackRowFarmer * Figure.fieldLength + j] = new FigureFarmer(FigureFarmer.Color.Black);
}
}
mField[Figure.fieldLength * 0 + 0] = new FigureRook(); // schwarzer Turm a8
mField[Figure.fieldLength * 0 + 7] = new FigureRook(); // schwarzer Turm h8
mField[Figure.fieldLength * 7 + 0] = new FigureRook(); // weißer Turm a1
}
boolean moveFigure(int src, int dst)
{
Figure f = mField[src];
if (!f.moveAllowed(src, dst, mField))
{
return false;
}
mField[dst] = f;
mField[src] = null;
return true;
}
}

80
fh.fd.ci.client/src/test/java/de/fd/fh/FigureFarmerTest.java

@ -1,44 +1,76 @@
package de.fd.fh;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.*;
class FigureFarmerTest
{
// weißer Bauer
@Test
void whiteFarmerValidDestination()
{
Figure f = new FigureFarmer();
assertTrue(f.moveAllowed(Figure.fieldLength * 6 + 0, Figure.fieldLength * 5 + 0, new int[Figure.fieldLength * Figure.fieldLength]));
}
Figure[] field = new Figure[Figure.fieldLength * Figure.fieldLength];
@Test
void whiteFarmerInvalidDestinationDown() // statt nach "oben" wird nach "unten" gegangen
{
Figure f = new FigureFarmer();
assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0, Figure.fieldLength * 7 + 0, new int[Figure.fieldLength * Figure.fieldLength]));
}
@ParameterizedTest
// color (0 white/1 black), row (src), col (src), row (dst), col (dst)
@CsvSource({
"0, 6, 0, 5, 0", // white: a2 -> a3
"0, 4, 4, 3, 4", // white: e4 -> e5
@Test
void whiteFarmerInvalidDestinationUp() // mehr als 1 Schritt nach "oben"
"1, 1, 0, 2, 0" // black: a1 -> a2
})
void farmerValidDestination(int color, int srcRow, int srcCol, int dstRow, int dstCol)
{
Figure f = new FigureFarmer();
assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0, Figure.fieldLength * 4 + 0, new int[Figure.fieldLength * Figure.fieldLength]));
int src = Figure.fieldLength * srcRow + srcCol;
int dst = Figure.fieldLength * dstRow + dstCol;
Figure f = null;
if (color == 0)
{
f = new FigureFarmer(FigureFarmer.Color.White);
}
else if (color == 1)
{
f = new FigureFarmer(FigureFarmer.Color.Black);
}
assertTrue(f.moveAllowed(src, dst, field));
}
@Test
void checkMoveWithValidDestinationVar2() // Bauer (w) steht in der Mitte vom Feld und geht 1 nach oben
@ParameterizedTest
// color (0 white/1 black), row (src), col (src), row (dst), col (dst)
@CsvSource({
"0, 6, 0, 7, 0", // white: a2 -> a1
"0, 4, 4, 5, 4", // white: e4 -> e3
"1, 1, 0, 0, 0" // black: a7 -> a8
})
void farmerInvalidDestination(int color, int srcRow, int srcCol, int dstRow, int dstCol) // statt nach "oben" wird nach "unten" gegangen
{
Figure f = new FigureFarmer();
assertTrue(f.moveAllowed(Figure.fieldLength * 4 + 4, Figure.fieldLength * 3 + 4, new int[Figure.fieldLength * Figure.fieldLength]));
int src = Figure.fieldLength * srcRow + srcCol;
int dst = Figure.fieldLength * dstRow + dstCol;
Figure f = null;
if (color == 0)
{
f = new FigureFarmer(FigureFarmer.Color.White);
}
else if (color == 1)
{
f = new FigureFarmer(FigureFarmer.Color.Black);
}
assertFalse(f.moveAllowed(src, dst, field));
}
@Test
void whiteFarmerInvalidDestinationDownVar2() // Bauer (w) steht in der Mitte vom Feld und geht 1 nach unten
void whiteFarmerInvalidDestinationUp() // mehr als 1 Schritt nach "oben"
{
Figure f = new FigureFarmer();
assertFalse(f.moveAllowed(Figure.fieldLength * 4 + 4, Figure.fieldLength * 5 + 4, new int[Figure.fieldLength * Figure.fieldLength]));
int src = Figure.fieldLength * 6 + 0;
int dst = Figure.fieldLength * 4 + 0;
Figure f = new FigureFarmer(FigureFarmer.Color.White);
assertFalse(f.moveAllowed(src, dst, field));
}
}

24
fh.fd.ci.client/src/test/java/de/fd/fh/FigureRookTest.java

@ -11,41 +11,53 @@ class FigureRookTest
void whiteRookValidMoveUp()
{
Figure f = new FigureRook();
assertTrue(f.moveAllowed(Figure.fieldLength * 4 + 4, Figure.fieldLength * 0 + 4, new int[Figure.fieldLength * Figure.fieldLength]));
assertTrue(f.moveAllowed(Figure.fieldLength * 4 + 4,
Figure.fieldLength * 0 + 4,
new Figure[Figure.fieldLength * Figure.fieldLength]));
}
@Test
void whiteRookValidMoveDown()
{
Figure f = new FigureRook();
assertTrue(f.moveAllowed(Figure.fieldLength * 4 + 4, Figure.fieldLength * 7 + 4, new int[Figure.fieldLength * Figure.fieldLength]));
assertTrue(f.moveAllowed(Figure.fieldLength * 4 + 4,
Figure.fieldLength * 7 + 4,
new Figure[Figure.fieldLength * Figure.fieldLength]));
}
@Test
void whiteRookInvalidMoveLeftUp() // diagonal nach links oben
{
Figure f = new FigureRook();
assertFalse(f.moveAllowed(Figure.fieldLength * 4 + 4, Figure.fieldLength * 3 + 3, new int[Figure.fieldLength * Figure.fieldLength]));
assertFalse(f.moveAllowed(Figure.fieldLength * 4 + 4,
Figure.fieldLength * 3 + 3,
new Figure[Figure.fieldLength * Figure.fieldLength]));
}
@Test
void whiteRookInvalidMoveRightUp() // diagonal nach rechts oben
{
Figure f = new FigureRook();
assertFalse(f.moveAllowed(Figure.fieldLength * 4 + 4, Figure.fieldLength * 3 + 5, new int[Figure.fieldLength * Figure.fieldLength]));
assertFalse(f.moveAllowed(Figure.fieldLength * 4 + 4,
Figure.fieldLength * 3 + 5,
new Figure[Figure.fieldLength * Figure.fieldLength]));
}
@Test
void whiteRookInvalidMoveLeftDown() // diagonal nach links unten
{
Figure f = new FigureRook();
assertFalse(f.moveAllowed(Figure.fieldLength * 4 + 4, Figure.fieldLength * 5 + 3, new int[Figure.fieldLength * Figure.fieldLength]));
assertFalse(f.moveAllowed(Figure.fieldLength * 4 + 4,
Figure.fieldLength * 5 + 3,
new Figure[Figure.fieldLength * Figure.fieldLength]));
}
@Test
void whiteRookInvalidMoveRightDown() // diagonal nach rechts unten
{
Figure f = new FigureRook();
assertFalse(f.moveAllowed(Figure.fieldLength * 4 + 4, Figure.fieldLength * 5 + 5, new int[Figure.fieldLength * Figure.fieldLength]));
assertFalse(f.moveAllowed(Figure.fieldLength * 4 + 4,
Figure.fieldLength * 5 + 5,
new Figure[Figure.fieldLength * Figure.fieldLength]));
}
}

16
fh.fd.ci.client/src/test/java/de/fd/fh/FigureTest.java

@ -10,27 +10,35 @@ class FigureTest
void checkMoveWithInvalidFieldSmall() // Feldgröße sollte 8*8 betragen
{
Figure f = new Figure();
assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0, Figure.fieldLength * 5 + 0, new int[0]));
assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0,
Figure.fieldLength * 5 + 0,
new Figure[0]));
}
@Test
void checkMoveWithInvalidFieldBigger() // Feldgröße sollte 8*8 betragen
{
Figure f = new Figure();
assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0, Figure.fieldLength * 5 + 0, new int[8*9]));
assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0,
Figure.fieldLength * 5 + 0,
new Figure[8*9]));
}
@Test
void checkMoveWithInvalidFieldCorrect() // Feldgröße sollte 8*8 betragen
{
Figure f = new Figure();
assertTrue(f.moveAllowed(Figure.fieldLength * 6 + 0, Figure.fieldLength * 5 + 0, new int[Figure.fieldLength * Figure.fieldLength]));
assertTrue(f.moveAllowed(Figure.fieldLength * 6 + 0,
Figure.fieldLength * 5 + 0,
new Figure[Figure.fieldLength * Figure.fieldLength]));
}
@Test
void checkMoveWithoutNewDestination()
{
Figure f = new Figure();
assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0, Figure.fieldLength * 6 + 0, new int[Figure.fieldLength * Figure.fieldLength]));
assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0,
Figure.fieldLength * 6 + 0,
new Figure[Figure.fieldLength * Figure.fieldLength]));
}
}

107
fh.fd.ci.client/src/test/java/de/fd/fh/GameTest.java

@ -0,0 +1,107 @@
package de.fd.fh;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
class GameTest
{
private Game g = new Game();
@BeforeEach
void setupTest()
{
g.initNewGame();
}
@Test
void newGameInitializationCorrectField()
{
assertEquals(Figure.fieldLength * Figure.fieldLength, g.mField.length);
}
// prüft, ob Figuren nach Initialisierung nur an vorgesehenden Positionen stehen
@Test
void newGameInitializationFigurePositions()
{
for (int i = 0; i < Figure.fieldLength; i++) // row
{
for (int j = 0; j < Figure.fieldLength; j++) // col
{
if (i == Game.whiteRowOther
|| i == Game.whiteRowFarmer
|| i == Game.blackRowOther
|| i == Game.blackRowFarmer)
{
assertNotEquals(null, g.mField[i * Figure.fieldLength + j]);
}
else
{
assertEquals(null, g.mField[i * Figure.fieldLength + j]);
}
}
}
}
@Test
void newGameInitializationFigurePositionsFarmer()
{
// weiße Bauern
for (int j = 0; j < Figure.fieldLength; j++) // col
{
assertEquals(new FigureFarmer(FigureFarmer.Color.White).getClass(),
g.mField[Game.whiteRowFarmer * Figure.fieldLength + j].getClass());
assertEquals(new FigureFarmer(FigureFarmer.Color.White),
g.mField[Game.whiteRowFarmer * Figure.fieldLength + j]);
}
// schwarze Bauern
for (int j = 0; j < Figure.fieldLength; j++) // col
{
assertEquals(new FigureFarmer(FigureFarmer.Color.Black).getClass(),
g.mField[Game.blackRowFarmer * Figure.fieldLength + j].getClass());
assertEquals(new FigureFarmer(FigureFarmer.Color.Black),
g.mField[Game.blackRowFarmer * Figure.fieldLength + j]);
}
}
@ParameterizedTest
// row, col
@CsvSource({
"0, 0", // schwarzer Turm a8
"0, 7", // schwarzer Turm h8
"7, 0" // weißer Turm a1
})
void newGameInitializationFigurePositionsRooks(int row, int col)
{
// schwarzer Turm a8
assertEquals(new FigureRook().getClass(),
g.mField[Figure.fieldLength * row + col].getClass());
}
@Test
void moveFarmerWhite() // weißer Bauer ganz links um ein Feld noch vorne bewegen
{
int src = Game.whiteRowFarmer * Figure.fieldLength + 0;
int dst = (Game.whiteRowFarmer - 1) * Figure.fieldLength + 0;
// zu bewegende Figur
Figure f = g.mField[src];
// Ziel sollte leer sein
assertNull(g.mField[dst]);
// Figur bewegen
assertTrue(g.moveFigure(src, dst));
// Figur sollte nun verschoben sein
assertNull(g.mField[src]);
assertSame(f, g.mField[dst]);
}
}
Loading…
Cancel
Save