40 Commits

Author SHA1 Message Date
Jenkins 5220f40876 Merge commit 'd9b9c2ac1bd9c3731d68d38219865a37f1a947cf' into HEAD 4 years ago
JD d9b9c2ac1b refactoring: optimized bishop move validation 4 years ago
JD 9fc4119045 test: bishop invalid step 3 4 years ago
JD 90baf93b14 refactoring: FigureBishopTest 4 years ago
JD 6e5c52fe37 test: bishop invalid step 2 4 years ago
JD 0ac4d17999 refactoring: removed magic numbers 4 years ago
JD 7d9510ccb9 test: bishop 1 step right 4 years ago
JD e77ffb6f8a refactoring: removed magic numbers 4 years ago
JD 5fc5761e06 test: invalid bishop move upward 4 years ago
JD cc0a4dd5e2 refactoring: combined tests checkInvalidKingMoves/checkValidKingMoves, renamed test checkInvalidKingMoves 4 years ago
JD 39759cf01d test: king movement -> valid moves (based on checkInvalidKingMoves()) 4 years ago
JD a3ce3541d7 refactoring: removed magic numbers 4 years ago
JD 16f4d9bbe4 test: initial king movement 4 years ago
JD 33f141832d initial placement white rook h1 4 years ago
JD 4ca6b4766a initial placement white rook a1 4 years ago
JD c827f07f88 moved newGameInitializationFigurePositionsRook2 into newGameInitializationFigurePositionsRooks 4 years ago
JD d0939f3cca transformed newGameInitializationFigurePositionsRooks to ParameterizedTest 4 years ago
JD b8a1cbb7c8 initial placement black rook h8 4 years ago
JD 95af987307 initial placement black rook a8 4 years ago
JD b5aa9415f3 renamed test farmerInvalidDestination 4 years ago
JD 7734e50a43 added test for invalid movement of black farmer 4 years ago
JD d74706ca41 renamed tests 4 years ago
JD 7c78433703 added test for valid movement of black farmer 4 years ago
JD 8a499a6489 moved checkMoveWithValidDestinationVar2 into whiteFarmerValidDestination 4 years ago
JD db2724722a moved whiteFarmerInvalidDestinationDownVar2 into whiteFarmerInvalidDestinationDown 4 years ago
JD edbd9c568c refactored test whiteFarmerInvalidDestinationDown to ParameterizedTest 4 years ago
JD 0bda50fef8 refactored test whiteFarmerValidDestination to ParameterizedTest 4 years ago
JD 975d2dd3e6 extracted magic numbers 4 years ago
JD 030bed6414 optimized annotations 4 years ago
JD cc784e9094 refactored magic numbers 4 years ago
JD df6c42c806 test initial white farmer move 4 years ago
JD 86da9de4c3 corrected wrong row constants 4 years ago
JD de43590ca1 refactored color type 4 years ago
JD 00b04d6bca added different FigurFarmer colors 4 years ago
JD cf9cb03b15 test farmer initial placement 4 years ago
JD 2e025f35ad corrected test newGameInitializationFigurePositions 4 years ago
JD d8918c6790 removed magic numbers 4 years ago
JD a9337af708 initial figure placement on field 4 years ago
JD 40501a27b0 removed magic numbers 4 years ago
JD 3de907b77e initial creation of "Game" class 4 years ago
  1. 19
      fh.fd.ci.client/src/main/java/de/fd/fh/FigureBishop.java
  2. 45
      fh.fd.ci.client/src/main/java/de/fd/fh/FigureFarmer.java
  3. 25
      fh.fd.ci.client/src/main/java/de/fd/fh/FigureKing.java
  4. 59
      fh.fd.ci.client/src/main/java/de/fd/fh/Game.java
  5. 54
      fh.fd.ci.client/src/test/java/de/fd/fh/FigureBishopTest.java
  6. 90
      fh.fd.ci.client/src/test/java/de/fd/fh/FigureFarmerTest.java
  7. 51
      fh.fd.ci.client/src/test/java/de/fd/fh/FigureKingTest.java
  8. 108
      fh.fd.ci.client/src/test/java/de/fd/fh/GameTest.java

19
fh.fd.ci.client/src/main/java/de/fd/fh/FigureBishop.java

@ -0,0 +1,19 @@
package de.fd.fh;
public class FigureBishop extends Figure
{
@Override
public boolean moveAllowed(int src, int dst, Figure[] field)
{
if ((dst-src) % Figure.fieldLength == 0) // nach oben/unten nicht erlaubt
{
return false;
}
if ((dst-src) % 7 != 0
&& (dst-src) % 9 != 0)
return false;
return super.moveAllowed(src, dst, field);
}
}

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

@ -2,17 +2,30 @@ 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, 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;
}
@ -23,4 +36,32 @@ public class FigureFarmer extends Figure
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;
}
}

25
fh.fd.ci.client/src/main/java/de/fd/fh/FigureKing.java

@ -0,0 +1,25 @@
package de.fd.fh;
public class FigureKing extends Figure
{
@Override
public boolean moveAllowed(int src, int dst, Figure[] field)
{
// TODO: Positionierung in einer Ecke beachten
if (dst != src-1
&& dst != src +1
&& dst != src -8
&& dst != src -8 -1
&& dst != src -8 +1
&& dst != src +8
&& dst != src +8 -1
&& dst != src +8 +1)
{
return false;
}
return super.moveAllowed(src, dst, field);
}
}

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

@ -0,0 +1,59 @@
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
mField[Figure.fieldLength * 7 + 7] = new FigureRook(); // weißer Turm h1
}
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;
}
}

54
fh.fd.ci.client/src/test/java/de/fd/fh/FigureBishopTest.java

@ -0,0 +1,54 @@
package de.fd.fh;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
class FigureBishopTest
{
Figure[] field = new Figure[Figure.fieldLength * Figure.fieldLength];
@Test
void checkInvalidMovesBishopUpward()
{
Figure f = new FigureBishop();
int src = Figure.fieldLength * 5 + 5;
int dst = src - Figure.fieldLength;
assertFalse(f.moveAllowed(src, dst, field));
}
@Test
void checkInvalidMovesBishopRight()
{
Figure f = new FigureBishop();
int src = Figure.fieldLength * 5 + 5;
int dst = src + 1;
assertFalse(f.moveAllowed(src, dst, field));
}
@Test
void checkInvalidMovesBishopRight2()
{
Figure f = new FigureBishop();
int src = Figure.fieldLength * 5 + 5;
int dst = src + 6;
assertFalse(f.moveAllowed(src, dst, field));
}
@Test
void checkInvalidMovesBishopRight3()
{
Figure f = new FigureBishop();
int src = Figure.fieldLength * 5 + 5;
int dst = src + 10;
assertFalse(f.moveAllowed(src, dst, field));
}
}

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

@ -1,54 +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 Figure[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 Figure[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 Figure[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 Figure[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 Figure[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));
}
}

51
fh.fd.ci.client/src/test/java/de/fd/fh/FigureKingTest.java

@ -0,0 +1,51 @@
package de.fd.fh;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import java.lang.reflect.Array;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*;
class FigureKingTest
{
// K = Position (19), x = gültig (10, 11, 12, 18, 20, 26, 27, 28), rest nicht
// x x x
// x K x
// x x x
@ParameterizedTest
@CsvSource({
"19",
"45"
})
void checkKingMovesSimple(int pos)
{
Figure f = new FigureKing();
ArrayList<Integer> possibleMoves = new ArrayList<>();
possibleMoves.add(pos - Figure.fieldLength - 1);
possibleMoves.add(pos - Figure.fieldLength);
possibleMoves.add(pos - Figure.fieldLength + 1);
possibleMoves.add(pos -1);
possibleMoves.add(pos + 1);
possibleMoves.add(pos + Figure.fieldLength - 1);
possibleMoves.add(pos + Figure.fieldLength);
possibleMoves.add(pos + Figure.fieldLength + 1);
for (int i = 0; i < Figure.fieldLength * Figure.fieldLength; i++)
{
if (possibleMoves.contains(i))
{
assertTrue(f.moveAllowed(pos, i, new Figure[64]));
continue;
}
assertFalse(f.moveAllowed(pos, i, new Figure[64]));
}
}
}

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

@ -0,0 +1,108 @@
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
"7, 7" // weißer Turm h1
})
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