Browse Source

refactored moveAllowed()

feat-figureMovement
Julius Dewender 9 months ago
parent
commit
5d52b6b876
  1. 4
      fh.fd.ci.client/src/main/java/de/fd/fh/Figure.java
  2. 6
      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. 20
      fh.fd.ci.client/src/test/java/de/fd/fh/FigureFarmerTest.java
  5. 24
      fh.fd.ci.client/src/test/java/de/fd/fh/FigureRookTest.java
  6. 16
      fh.fd.ci.client/src/test/java/de/fd/fh/FigureTest.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;
}
}

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

@ -3,7 +3,7 @@ package de.fd.fh;
public class FigureFarmer 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
@ -17,6 +17,10 @@ public class FigureFarmer extends Figure
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);
}
}

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);
}
}

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

@ -11,34 +11,44 @@ class FigureFarmerTest
void whiteFarmerValidDestination()
{
Figure f = new FigureFarmer();
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 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]));
assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0,
Figure.fieldLength * 7 + 0,
new Figure[Figure.fieldLength * Figure.fieldLength]));
}
@Test
void whiteFarmerInvalidDestinationUp() // mehr als 1 Schritt nach "oben"
{
Figure f = new FigureFarmer();
assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0, Figure.fieldLength * 4 + 0, new int[Figure.fieldLength * Figure.fieldLength]));
assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0,
Figure.fieldLength * 4 + 0,
new Figure[Figure.fieldLength * Figure.fieldLength]));
}
@Test
void checkMoveWithValidDestinationVar2() // Bauer (w) steht in der Mitte vom Feld und geht 1 nach oben
{
Figure f = new FigureFarmer();
assertTrue(f.moveAllowed(Figure.fieldLength * 4 + 4, Figure.fieldLength * 3 + 4, new int[Figure.fieldLength * Figure.fieldLength]));
assertTrue(f.moveAllowed(Figure.fieldLength * 4 + 4,
Figure.fieldLength * 3 + 4,
new Figure[Figure.fieldLength * Figure.fieldLength]));
}
@Test
void whiteFarmerInvalidDestinationDownVar2() // Bauer (w) steht in der Mitte vom Feld und geht 1 nach unten
{
Figure f = new FigureFarmer();
assertFalse(f.moveAllowed(Figure.fieldLength * 4 + 4, Figure.fieldLength * 5 + 4, new int[Figure.fieldLength * Figure.fieldLength]));
assertFalse(f.moveAllowed(Figure.fieldLength * 4 + 4,
Figure.fieldLength * 5 + 4,
new Figure[Figure.fieldLength * Figure.fieldLength]));
}
}

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]));
}
}
Loading…
Cancel
Save