Jenkins
4 years ago
8 changed files with 415 additions and 36 deletions
-
19fh.fd.ci.client/src/main/java/de/fd/fh/FigureBishop.java
-
45fh.fd.ci.client/src/main/java/de/fd/fh/FigureFarmer.java
-
25fh.fd.ci.client/src/main/java/de/fd/fh/FigureKing.java
-
59fh.fd.ci.client/src/main/java/de/fd/fh/Game.java
-
54fh.fd.ci.client/src/test/java/de/fd/fh/FigureBishopTest.java
-
86fh.fd.ci.client/src/test/java/de/fd/fh/FigureFarmerTest.java
-
51fh.fd.ci.client/src/test/java/de/fd/fh/FigureKingTest.java
-
108fh.fd.ci.client/src/test/java/de/fd/fh/GameTest.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); |
|||
} |
|||
} |
@ -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); |
|||
} |
|||
} |
@ -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; |
|||
} |
|||
} |
@ -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)); |
|||
} |
|||
} |
@ -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[] field = 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 |
|||
|
|||
"1, 1, 0, 2, 0" // black: a1 -> a2 |
|||
}) |
|||
void farmerValidDestination(int color, int srcRow, int srcCol, int dstRow, int dstCol) |
|||
{ |
|||
Figure f = new FigureFarmer(); |
|||
assertTrue(f.moveAllowed(Figure.fieldLength * 6 + 0, |
|||
Figure.fieldLength * 5 + 0, |
|||
new Figure[Figure.fieldLength * Figure.fieldLength])); |
|||
} |
|||
int src = Figure.fieldLength * srcRow + srcCol; |
|||
int dst = Figure.fieldLength * dstRow + dstCol; |
|||
|
|||
@Test |
|||
void whiteFarmerInvalidDestinationDown() // statt nach "oben" wird nach "unten" gegangen |
|||
Figure f = null; |
|||
|
|||
if (color == 0) |
|||
{ |
|||
Figure f = new FigureFarmer(); |
|||
assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0, |
|||
Figure.fieldLength * 7 + 0, |
|||
new Figure[Figure.fieldLength * Figure.fieldLength])); |
|||
f = new FigureFarmer(FigureFarmer.Color.White); |
|||
} |
|||
|
|||
@Test |
|||
void whiteFarmerInvalidDestinationUp() // mehr als 1 Schritt nach "oben" |
|||
else if (color == 1) |
|||
{ |
|||
Figure f = new FigureFarmer(); |
|||
assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0, |
|||
Figure.fieldLength * 4 + 0, |
|||
new Figure[Figure.fieldLength * Figure.fieldLength])); |
|||
f = new FigureFarmer(FigureFarmer.Color.Black); |
|||
} |
|||
|
|||
@Test |
|||
void checkMoveWithValidDestinationVar2() // Bauer (w) steht in der Mitte vom Feld und geht 1 nach oben |
|||
assertTrue(f.moveAllowed(src, dst, field)); |
|||
} |
|||
|
|||
@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 |
|||
{ |
|||
int src = Figure.fieldLength * srcRow + srcCol; |
|||
int dst = Figure.fieldLength * dstRow + dstCol; |
|||
|
|||
Figure f = null; |
|||
|
|||
if (color == 0) |
|||
{ |
|||
Figure f = new FigureFarmer(); |
|||
assertTrue(f.moveAllowed(Figure.fieldLength * 4 + 4, |
|||
Figure.fieldLength * 3 + 4, |
|||
new Figure[Figure.fieldLength * Figure.fieldLength])); |
|||
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)); |
|||
} |
|||
} |
@ -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])); |
|||
} |
|||
} |
|||
} |
@ -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]); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue